我一直在尝试在tensorflow中实现一个基本的神经网络,输入只是(x,y,z)中的1/0的随机数据,但是我希望我的网络在x = 1时输出1并且否则输出0。
这是我的网络代码
import tensorflow as tf
import numpy as np
x_data = np.array([[0,0,1],
[0,1,1],
[1,0,0],
[0,1,0],
[1,1,1],
[0,1,1],
[1,1,1]])
x_test = np.array([[1,1,1], [0,1,0], [0,0,0]])
y_data = np.array([0,0,1,0,1,0,1])
iters = 1000
learning_rate = 0.1
weights = {
'w1': tf.Variable(tf.random_normal([3, 5])),
'w2': tf.Variable(tf.random_normal([5, 1])),
}
bias = {
'b1': tf.Variable(tf.random_normal([5])),
'b2': tf.Variable(tf.random_normal([1])),
}
def predict(x, weights, bias):
l1 = tf.add(tf.matmul(x, weights['w1']), bias['b1'])
l1 = tf.nn.sigmoid(l1)
out = tf.add(tf.matmul(l1, weights['w2']), bias['b2'])
return out
x = tf.placeholder(tf.float32, shape=(None,3))
y = tf.placeholder(tf.float32, shape=(None))
pred = predict(x, weights, bias)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
# graph
with tf.Session() as sess:
sess.run(init)
for i in range(0, iters):
_, c = sess.run([optimizer, cost], feed_dict={x: x_data, y: y_data})
if i % 100 == 0:
print("cost: " + str(c))
print(sess.run(weights['w1']))
print(sess.run(pred, feed_dict={x: x_test}))
哪些输出
[-0.37119362]
[-0.23264697]
[-0.14701667]
但是我的测试数据应输出[1,0,0],我真的不确定这里有什么问题。我尝试过使用超参数并查看stackoverflow。我也尝试使用softmax_cross_entropy作为成本函数,虽然它给出了一个错误,说logits与标签的形状不同。
有谁知道为什么这不会输出我期待的东西?
答案 0 :(得分:1)
首先,您需要在输出之前通过激活函数(即tf.nn.sigmoid
)。
确保tf.nn.sigmoid_cross_entropy_with_logits
获取logits(在sigmoid激活之前)。
您的输入y_data
的形状问题也是(7)
而不是(7, 1)
以下是您的代码的工作版本:
import tensorflow as tf
import numpy as np
x_data = np.array([[0,0,1],
[0,1,1],
[1,0,0],
[0,1,0],
[1,1,1],
[0,1,1],
[1,1,1]])
x_test = np.array([[1,1,1], [0,1,0], [0,0,0]])
y_data = np.array([[0],[0],[1],[0],[1],[0],[1]])
iters = 1000
learning_rate = 0.1
weights = {
'w1': tf.Variable(tf.random_normal([3, 5])),
'w2': tf.Variable(tf.random_normal([5, 1])),
}
bias = {
'b1': tf.Variable(tf.random_normal([5])),
'b2': tf.Variable(tf.random_normal([1])),
}
def predict(x, weights, bias):
l1 = tf.add(tf.matmul(x, weights['w1']), bias['b1'])
l1 = tf.nn.sigmoid(l1)
out = tf.add(tf.matmul(l1, weights['w2']), bias['b2'])
return out
x = tf.placeholder(tf.float32, shape=(None,3))
y = tf.placeholder(tf.float32, shape=(None,1))
pred = predict(x, weights, bias)
pred_postactivation = tf.nn.sigmoid(pred)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
# graph
with tf.Session() as sess:
sess.run(init)
for i in range(0, iters):
_, c = sess.run([optimizer, cost], feed_dict={x: x_data, y: y_data})
if i % 100 == 0:
print("cost: " + str(c))
print(sess.run(weights['w1']))
print(sess.run(pred_postactivation, feed_dict={x: x_test}))
哪个输出:
cost: 1.23954
cost: 0.583582
cost: 0.455403
cost: 0.327644
cost: 0.230051
cost: 0.165296
cost: 0.123712
cost: 0.0962315
cost: 0.0772587
cost: 0.0636141
[[ 0.94488049 0.78105074 0.81608331 1.75763154 -4.47565413]
[-2.61545444 0.26020721 0.151407 1.33066297 1.00578034]
[-1.2027328 0.05413296 -0.13530347 -0.39841765 0.16014417]]
[[ 0.92521071]
[ 0.05481482]
[ 0.07227208]]