当预期的浮点值介于0和1之间时,TensorFlow MLP始终返回0或1

时间:2018-02-14 05:19:45

标签: python tensorflow

我是TensorFlow的初学者。我实现了tensorFlow MLP网络来预测介于0和-1之间的值。输入值是介于0和1之间的浮点值,权重是0到1之间的随机浮点数。但是输出总是返回0或1,我希望在它之间返回浮点值0和1.代码如下。

import tensorflow as tf
import numpy as np
from sklearn import datasets`enter code here`
from sklearn.model_selection import train_test_split
from scipy.io import loadmat
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

RANDOM_SEED = 42
tf.set_random_seed(RANDOM_SEED)


def init_weights(shape):
    """ Weight initialization """
    weights = tf.random_normal(shape, stddev=0.01)
    return tf.Variable(weights)

def forwardprop(X, w_1, w_2):
    h    = tf.nn.sigmoid(tf.matmul(X, w_1))  # The \sigma function
    yhat = tf.matmul(h, w_2)  # The \varphi function
    return yhat

def load_data():
        dw = loadmat('dw.mat')
        dv = loadmat('dv.mat')
        dw_2 = loadmat('test_dw.mat')
        dv_2 = loadmat('test_dv.mat')

        train_dw = dw['dw']
        train_dv = dv['dv']
        test_dw = dw_2['test_dw']
        test_dv = dv_2['test_dv']

        scaler = MinMaxScaler()
        train_dw = scaler.fit_transform(train_dw)
        train_dv = scaler.fit_transform(train_dv)
        test_dw = scaler.fit_transform(test_dw)
        test_dv = scaler.fit_transform(test_dv)

        rows = len(train_dw)
        # train_input = dv(t),dw(t),dw(-1),dw(t-2),dw_pred_neighbor1(t),dw_pred_neighbor2(t)
        train_input = np.column_stack((np.ones((rows-3)),train_dv[2:(rows-1),1],train_dw[2:(rows-1),1],train_dw[1:(rows-2),1],train_dw[0:(rows-3),1],train_dw[2:(rows-1),2],train_dw[2:(rows-1),4]))
        # train target = dw(t+1)
        train_target = np.column_stack((np.ones((rows-3)),train_dw[3:rows,1]))

        test_rows = len(test_dw)
        # test_input = dv(t),dw(t),dw(-1),dw(t-2),dw_pred_neighbor1(t),dw_pred_neighbor2(t)
        test_input = np.column_stack((np.ones((test_rows-3)),test_dv[2:(test_rows-1),1],test_dw[2:(test_rows-1),1],test_dw[1:(test_rows-2),1],test_dw[0:(test_rows-3),1],test_dw[2:(test_rows-1),2],test_dw[2:(test_rows-1),4]))
        # test target = dw(t+1)
        test_target =  np.column_stack((np.ones((test_rows-3)),test_dw[3:test_rows,1]))

        return train_input, test_input, train_target, test_target

def main():
    train_X, test_X, train_y, test_y = load_data()

    # Layer's sizes
    x_size = train_X.shape[1]   # Number of input nodes
    h_size = 10                 # Number of hidden nodes
    y_size = train_y.shape[1]           # Number of outputs

    # Symbols
    X = tf.placeholder(dtype = tf.float32, shape=[None, x_size])
    y = tf.placeholder(dtype = tf.float32, shape=[None, y_size])

    # Weight initializations
    w_1 = init_weights((x_size, h_size))
    w_2 = init_weights((h_size, y_size))

    # Forward propagation
    yhat    = forwardprop(X, w_1, w_2)
    predict = tf.argmax(yhat, axis=1)

    # Backward propagation
    #cost    = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=yhat))
    cost = tf.losses.mean_squared_error(y,yhat)
    updates = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)

    # Run SGD
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)

    cost_history = np.empty(shape=[1],dtype=float)

    for epoch in range(1000):
        #Train with each example
        for i in range(len(train_X)):
            sess.run(updates, feed_dict={X: train_X, y: train_y})


        pred = sess.run(predict,feed_dict={X: train_X})
        print(pred)
        plt.plot(range(len(pred)),pred)
        plt.show()
        cost_history = np.append(cost_history, sess.run(cost, feed_dict={X: train_X, y: train_y}))
        train_accuracy = np.mean(np.argmax(train_y, axis=0) ==
                                 sess.run(predict, feed_dict={X: train_X, y: train_y}))

        test_accuracy  = np.mean(np.argmax(test_y, axis=0) ==
                                 sess.run(predict, feed_dict={X: test_X, y: test_y}))

        print("Epoch = %d, train accuracy = %.2f%%, test accuracy = %.2f%%"
              % (epoch + 1, 100. * train_accuracy, 100. * test_accuracy))

    plt.plot(range(len(cost_history)),cost_history)
    plt.axis([0,epoch,0,np.max(cost_history)])
    plt.show()
    sess.close()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:3)

tf.argmax返回向量中具有最大值的索引。

如果要查找确切的类概率,可以使用tf.max

来获取