TensorFlow神经网络的输出不会改变

时间:2016-11-09 18:18:01

标签: python machine-learning neural-network tensorflow

我想做一个神经网络的基本应用。我有一个值列表。我选择了30个值的序列,我想猜31( 希望有关系)

我的问题是我的NN输出始终为1。

import tensorflow as tf

INPUT_NUMBER = 30

filename_queue = tf.train.string_input_producer(["googl.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
closeValue_ = tf.decode_csv(value, record_defaults=[[1.]])

# Neural Network

tf_in = tf.placeholder(tf.float32, [None, INPUT_NUMBER])
tf_weight = tf.Variable(tf.random_normal([INPUT_NUMBER, 1], stddev=0.35), name="tf_weight")
tf_bias = tf.Variable(tf.zeros([1]))

y = tf.nn.softmax(tf.matmul(tf_in, tf_weight) + tf_bias)
y_ = tf.placeholder(tf.float32, [None, 1])

cost = tf.abs(tf.sub(y, y_))
train_step = tf.train.AdamOptimizer().minimize(cost)

init = tf.initialize_all_variables()


with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    data = []
    for i in range(3000):
        closeValue = sess.run([closeValue_])[0][0]
        data.append(closeValue/10000)

    sess.run(init)

    for i in range(2000):
        batch_xs = []
        batch_ys = []
        for j in range(i, i+30):
            batch_xs.append(data[j])
        batch_ys.append(data[j+1])
        _, predictionValue, realValue = sess.run([train_step, y, y_], feed_dict={tf_in: [batch_xs], y_: [batch_ys]})
        print("predictionValue: ")
        print(predictionValue)
        print("realValue: ")
        print(realValue)
    coord.request_stop()
    coord.join(threads)

输出:

   predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01313463]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01299099]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01402302]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01488589]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01502102]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01551652]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01561962]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01456456]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01558859]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01595495]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01657357]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01657758]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01712913]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01836537]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01734184]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01798599]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.0180025]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01791792]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01845596]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01649099]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01698148]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01763363]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.0178013]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01815565]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01898498]]
  predictionValue: 
  [[ 1.]]
  realValue: 
  [[ 0.01864565]]

1 个答案:

答案 0 :(得分:1)

您正在进行回归,因此您的模型不应该最后有softmax。 Softmax是用于生成概率估计的归一化元素,因此如果您只有一个输出,则此估计将为...始终为1.