TensorFlow XOR NN eval函数错误
TensorFlow XOR NN eval function error
我正在尝试使用 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。
我不太清楚你想用
实现什么
correct = tf.nn.in_top_k(logits, tf.cast(y,tf.int32), 1)
我建议使用
correct = tf.equal(
tf.reshape(
tf.greater_equal(tf.nn.sigmoid(logits),0.5),[-1]
),
tf.cast(y,tf.bool)
)
已编辑:我注意到在给定的解决方案中准确度停留在 0.5。通过进行以下更改,我能够使该解决方案起作用(准确度:100.0)。
将网络更改为以下。 (使用tanh,使用两个隐藏层)
with tf.name_scope('dnn'):
hidden1 = neuron_layer(X, n_hidden, name='hidden1', activation=tf.nn.tanh)
hidden2 = neuron_layer(hidden1, n_hidden, name='hidden2', activation=tf.nn.tanh)
logits = neuron_layer(hidden2, n_outputs, name='outputs')
和n_hidden = 7
、n_epochs = 5
注意:我不太确定为什么它需要两个隐藏层。但显然它需要让它在这个设置下工作。
我正在尝试使用 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。
我不太清楚你想用
实现什么correct = tf.nn.in_top_k(logits, tf.cast(y,tf.int32), 1)
我建议使用
correct = tf.equal(
tf.reshape(
tf.greater_equal(tf.nn.sigmoid(logits),0.5),[-1]
),
tf.cast(y,tf.bool)
)
已编辑:我注意到在给定的解决方案中准确度停留在 0.5。通过进行以下更改,我能够使该解决方案起作用(准确度:100.0)。
将网络更改为以下。 (使用tanh,使用两个隐藏层)
with tf.name_scope('dnn'):
hidden1 = neuron_layer(X, n_hidden, name='hidden1', activation=tf.nn.tanh)
hidden2 = neuron_layer(hidden1, n_hidden, name='hidden2', activation=tf.nn.tanh)
logits = neuron_layer(hidden2, n_outputs, name='outputs')
和n_hidden = 7
、n_epochs = 5
注意:我不太确定为什么它需要两个隐藏层。但显然它需要让它在这个设置下工作。