我正在尝试使用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)
有人能告诉我这个函数有什么问题吗?我试着改编机器学习手册中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
注意:我不太清楚为什么它需要两个隐藏层。但显然它需要让它在这种环境下工作。你知道吗
相关问题 更多 >
编程相关推荐