1
我做了一个前馈单神经元网络。预测打印0.5,而打印0.0。我对tensorflow很陌生。请帮帮我。这是我的代码:张量流单神经元前馈网络
"""
O---(w1)-\
\
O---(w2)-->Sum ---> Sigmoid ---> O 3 inputs and 1 output
/
O---(w3)-/
| Input | Output
Example 1 | 0 0 1 | 0
Example 2 | 1 1 1 | 1
Example 3 | 1 0 1 | 1
Exmaple 4 | 0 1 1 | 0
"""
import tensorflow as tf
features = tf.placeholder(tf.float32, [None, 3])
labels = tf.placeholder(tf.float32, [None])
#Random weights
W = tf.Variable([[-0.16595599], [0.44064899], [-0.99977125]], tf.float32)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
predict = tf.nn.sigmoid(tf.matmul(features, W))
error = labels - predict
# Training
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(error)
for i in range(10000):
sess.run(train, feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels: [0, 1, 1, 0]})
training_cost = sess.run(error, feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels: [0, 1, 1, 0]})
print('Training cost = ', training_cost, 'W = ', sess.run(W))
print(sess.run(predict, feed_dict={features:[[0, 1, 1]]}))
我也手动使这个模型只使用numpy,效果很好。
编辑:我已经尝试了所有类型的成本函数,包括tf.reduce_mean(预测的标签)** 2)
标签取得了理想的结果 - 预测是不是一个有效的错误,也许你的意思tf.reduce_mean((标签 - 预测)** 2)? – lejlot