【发布时间】:2018-08-12 23:25:55
【问题描述】:
我正在尝试使用 vanilla tensorflow 编写 XOR MLP,并且一直在试图弄清楚如何编写 eval 函数。
我收到了错误 InvalidArgumentError (see above for traceback): targets[1] is out of range。注释掉 accuracy.eval 行时不会产生错误。这是我的代码:
import numpy as np
import tensorflow as tf
n_inputs = 2
n_hidden = 3
n_outputs = 1
def reset_graph(seed=42):
tf.reset_default_graph()
tf.set_random_seed(seed)
np.random.seed(seed)
reset_graph()
X = tf.placeholder(tf.float32, shape=(None, n_inputs), name='X')
y = tf.placeholder(tf.float32, shape=(None), name='y')
def neuron_layer(X, n_neurons, name, activation=None):
with tf.name_scope(name):
n_inputs = int(X.get_shape()[1])
stddev = 2 / np.sqrt(n_inputs)
init = tf.truncated_normal((n_inputs, n_neurons), stddev=stddev)
W = tf.Variable(init, name="weights")
b = tf.Variable(tf.zeros([n_neurons]), name="bias")
Z = tf.matmul(X, W) + b
if activation is not None:
return activation(Z)
else: return Z
with tf.name_scope('dnn'):
hidden = neuron_layer(X, n_hidden, name='hidden', activation=tf.nn.sigmoid)
logits = neuron_layer(hidden, n_outputs, name='outputs')
with tf.name_scope('loss'):
bin_xentropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(bin_xentropy, name='loss')
learning_rate = 0.1
with tf.name_scope('train'):
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
with tf.name_scope('eval'):
correct = tf.nn.in_top_k(logits, tf.cast(y,tf.int32), 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
accuracy_summary = tf.summary.scalar('accuracy', accuracy)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
n_epochs = 100
batch_size = 4
def shuffle_batch(X, y, batch_size): # not really needed for XOR
rnd_idx = np.random.permutation(len(X))
n_batches = len(X) // batch_size
for batch_idx in np.array_split(rnd_idx, n_batches):
X_batch, y_batch = X[batch_idx], y[batch_idx]
yield X_batch, y_batch
X_train = [
(0, 0),
(0, 1),
(1, 0),
(1, 1)
]
y_train = [0,1,1,0]
X_train = np.array(X_train)
y_train = np.array(y_train)
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
acc = accuracy.eval(feed_dict={X: X_train, y: y_train})
print(acc)
有人可以告诉我我在使用此功能时做错了什么吗?我尝试从 Hands-On Machine Learning 书中的 MNIST 示例改编 XOR。
【问题讨论】:
标签: python tensorflow runtime-error eval invalid-argument