张量流中的成本不变

时间:2018-07-25 21:10:46

标签: python tensorflow

我正在尝试使用张量流训练神经网络。

这是我的输入内容:

[44,114,51,113,57,113,61,115,65,114,71,115,79,116,71,119,64,121,60,121,56,120,50,118,47,114,57,117,61,117,65,117,76,116,65,116,60,116,56,116,26,58,32,54,40,54,48,56,55,60,75,60,84,58,92,57,100,59,106,64,36,66,41,64,47,64,52,69,46,69,40,69,78,70,84,67,90,68,94,71,89,73,84,72,64,68,63,78,62,87,61,97,52,99,56,102,61,104,66,103,17,59,18,73,20,87,21,100,23,113,29,125,37,135,47,143,59,146,72,145,84,139,95,130,104,119,109,107,112,93,115,80,118,66,42,119,50,119,58,117,63,118,68,117,75,116,84,115,77,123,69,128,64,129,59,129,51,126,46,120,58,121,63,122,68,120,81,117,69,120,63,121,58,121,14,70,20,65,28,63,37,64,45,66,67,63,75,59,84,57,93,57,102,60,26,75,32,71,38,71,45,76,39,77,32,77,72,73,78,67,85,66,92,69,86,72,79,72,57,72,58,83,59,93,60,103,51,107,56,109,61,110,67,108,9,74,11,87,15,100,19,112,25,123,34,133,44,141,56,148,68,149,80,145,90,137,100,127,107,117,112,105,112,92,112,79,110,66]

我的输出看起来像这样:

[0,1]

我的训练集只有750个案例,但是如果我可以使我的代码正常工作并且看到成本的小幅下降,我可以生成更多的案例。在收集更多训练数据之前,我想先修复我的代码。

这是我写的代码:

import tensorflow as tf
import json

with open('data/features-data/tensorflow-train.json') as t:
  train = json.load(t)
with open('data/features-data/tensorflow-test.json') as te:
  test = json.load(te)

# Python optimisation variables
learning_rate = 0.1
epochs = 10000
batch_size = 30

x = tf.placeholder(tf.float32, [None, 268])
y = tf.placeholder(tf.float32, [None, 2])

W1 = tf.Variable(tf.random_normal([268, 100], stddev=0.03), name='W1')
b1 = tf.Variable(tf.random_normal([100]), name='b1')

W2 = tf.Variable(tf.random_normal([100, 2], stddev=0.03), name='W2')
b2 = tf.Variable(tf.random_normal([2]), name='b2')

# calculate the output of the hidden layer
hidden_out = tf.nn.relu(tf.add(tf.matmul(x, W1), b1))
y_ = tf.nn.softmax(tf.add(tf.matmul(hidden_out, W2), b2))

y_clipped = tf.clip_by_value(y_, 1e-10, 0.9999999)
cross_entropy = -tf.reduce_mean(tf.reduce_sum(y * tf.log(y_clipped) + (1 - y) * tf.log(1 - y_clipped), axis=1))

optimiser = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)

# finally setup the initialisation operator
init_op = tf.global_variables_initializer()

# define an accuracy assessment operation
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# start the session
with tf.Session() as sess:
  # initialise the variables
  sess.run(init_op)
  total_batch = int(len(train["outputs"]) / batch_size)
  for epoch in range(epochs):
    avg_cost = 0
    for i in range(total_batch):
      batch_x = train["inputs"][(i*batch_size):((i+1)*batch_size)]
      batch_y = train["outputs"][(i*batch_size):((i+1)*batch_size)]
      _, c = sess.run([optimiser, cross_entropy], feed_dict={x: batch_x, y: batch_y})
      avg_cost += c / total_batch
    print("Epoch:", (epoch + 1), "cost =", "{:.3f}".format(avg_cost))

  print(sess.run(accuracy, feed_dict={x: test["inputs"], y: test["outputs"]}))

这是我得到的输出:

Epoch: 1 cost = 18.710
Epoch: 2 cost = 19.484
Epoch: 3 cost = 19.484
Epoch: 4 cost = 19.484
Epoch: 5 cost = 19.484
Epoch: 6 cost = 19.484
Epoch: 7 cost = 19.484
Epoch: 8 cost = 19.484
Epoch: 9 cost = 19.484
Epoch: 10 cost = 19.484
Epoch: 11 cost = 19.484
Epoch: 12 cost = 19.484
Epoch: 13 cost = 19.484
Epoch: 14 cost = 19.484
Epoch: 15 cost = 19.484
Epoch: 16 cost = 19.484
Epoch: 17 cost = 19.484
Epoch: 18 cost = 19.484
Epoch: 19 cost = 19.484

我真的不明白为什么费用不会改变。是因为我的数据未规范化吗?我看到的每个示例都将输入数据作为浮点数。

0 个答案:

没有答案
相关问题