此Tensorflow脚本训练了逻辑回归模型,但出现错误。 此代码段摘自《用于深度学习的Tensorflow》(第74页)一书。
with tf.name_scope("placeholders"):
x = tf.compat.v1.placeholder(tf.float32, (N,2))
y = tf.compat.v1.placeholder(tf.float32, (N,))
with tf.name_scope("weights"):
W = tf.Variable(tf.random.normal((2,1)))
b = tf.Variable(tf.random.normal((1,)))
with tf.name_scope("prediction"):
y_logit = tf.squeeze(tf.matmul(x,W)+b)
y_one_prob = tf.sigmoid(y_logit)
y_pred = tf.round(y_one_prob)
with tf.name_scope("loss"):
entropy = tf.compat.v1.nn.sigmoid_cross_entropy_with_logits(logits=y_logit, labels=y)
l = tf.reduce_sum(entropy)
with tf.name_scope("optim"):
train_op = tf.compat.v1.train.AdamOptimizer(.01).minimize(l)
merged = tf.compat.v1.summary.merge_all()
train_writer = tf.compat.v1.summary.FileWriter("/tmp/logistic-train", tf.compat.v1.get_default_graph())
n_steps = 10000
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
for i in range(n_steps):
feed_dict = {x: x_np, y: y_np}
_, summary, loss = sess.run([train_op, merged, l], feed_dict=feed_dict)
print("Step %d, loss: %f" % (i, loss))
train_writer.add_summary(summary, i)
错误消息是:
...
File "/Users/wkgreat/Library/Python/3.6/lib/python/site-packages/tensorflow_core/python/client/session.py", line 263, in for_fetch
(fetch, type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>
错误原因是什么?