我写了一个简单的TensorFlow程序,它不起作用。这是我正在努力解决的问题。给定x作为输入,我想粗略地评估一个函数,如果x在< 0,0.33>中,则返回值0.0。或< 0.66,1.0>间隔,如果x在(0.33,0.66)间隔内,则为1.0。
以下是代码:
import tensorflow as tf
import numpy
import scipy
# input and output
x = tf.placeholder(tf.float32, shape=[None, 1])
y_true = tf.placeholder(tf.float32, shape=[None, 1])
# vars
weights = tf.Variable(tf.zeros([1, 1]))
biases = tf.Variable(tf.zeros([1]))
logits = tf.matmul(x, weights) + biases
y_pred = tf.nn.softmax(logits)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_true)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
x_train = [ [0.1], [0.2], [0.3], [0.4], [0.5], [0.6], [0.7], [0.8], [0.9] ]
y_train = [ [0.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [0.0] ]
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(optimizer, {x: x_train, y_true: y_train})
we, bi = sess.run([weights, biases])
print("we: %s bi: %s"%(we, bi))
answer = sess.run(y_pred, feed_dict={x: x_train})
print(answer)
训练后,重量和偏差的值完全错误。即使在第一次迭代之后它们都是1,并且在之后不会改变。
我编写的代码基于一些用于数字识别的代码,我认为我会将问题“最小化”为一个数字/“像素”。
除了更改迭代计数或学习率之外,还有什么想法可以尝试?
编辑: 所以我设法使用sigmoid来解决我的问题,如下所示,并使用更多层。这是工作代码:
import tensorflow as tf
import numpy
# consts
input_num_units = 1
hidden1_num_units = 8
hidden2_num_units = 16
output_num_units = 1
# input and output
x = tf.placeholder(tf.float32, shape=[None, 1])
y_true = tf.placeholder(tf.float32, shape=[None, 1])
# vars
weights = {
'hidden1': tf.Variable(tf.random_normal([input_num_units, hidden1_num_units])),
'hidden2': tf.Variable(tf.random_normal([hidden1_num_units, hidden2_num_units])),
'output': tf.Variable(tf.random_normal([hidden2_num_units, output_num_units]))
}
biases = {
'hidden1': tf.Variable(tf.random_normal([hidden1_num_units])),
'hidden2': tf.Variable(tf.random_normal([hidden2_num_units])),
'output': tf.Variable(tf.random_normal([output_num_units]))
}
hidden_layer_1 = tf.add(tf.matmul(x, weights['hidden1']), biases['hidden1'])
hidden_layer_1 = tf.nn.sigmoid(hidden_layer_1)
hidden_layer_2 = tf.add(tf.matmul(hidden_layer_1, weights['hidden2']), biases['hidden2'])
hidden_layer_2 = tf.nn.sigmoid(hidden_layer_2)
output_layer = tf.matmul(hidden_layer_2, weights['output']) + biases['output']
output_value = tf.nn.sigmoid(output_layer)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output_layer, labels=y_true))
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
x_train = [ [0.1], [0.2], [0.3], [0.4], [0.5], [0.6], [0.7], [0.8], [0.9] ]
y_train = [ [0.75], [0.0], [0.0], [1.0], [0.5], [1.0], [0.0], [0.0], [0.0] ]
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(10000):
sess.run(optimizer, {x: x_train, y_true: y_train})
answer = sess.run(output_value, feed_dict={x: x_train})
print(answer)
为了看我的模型是否运作良好,我实际上在< 0,1>中绘制了一整套值。在通过网络之后,他们产生了很多我期待的东西。这可以摆弄。我注意到,例如,我执行的迭代越多,函数变得越“陡峭”,并且如果进行一些迭代并执行则更加平滑。
答案 0 :(得分:2)
权重不会改变,因为输出永远不会改变,它总是1.0,这是因为你将softmax应用于单个输出,而不是向量。在这种情况下你应该使用sigmoid激活。
只需将softmax_cross_entropy_with_logits
替换为sigmoid_cross_entropy_with_logits
即可。您还应该使用非零值初始化权重,理想情况下是小范围内的随机值。