Tensorflow apply_gradients引发错误

时间:2016-04-27 18:34:32

标签: python tensorflow

我收到错误:

TypeError:'AssignAdd'Op的输入'ref'需要l值输入

位于下方函数apply_gradient_op = opt.apply_gradients(grads, global_step=stepNum)的{​​{1}}行。

train

关于可能出错的指示?我从def x1_x2_diff_net_v0(): x = tf.placeholder(tf.float32, [None, 4]) lb = tf.placeholder(tf.float32, [None, 2]) #First fc layer with tf.variable_scope('fc1') as scope: w = tfu.get_weights([4,100], name='fc1_w') b = tfu.get_bias([1,100], name='fc1_b') fc1 = tf.nn.relu(tf.matmul(x, w) + b) #Prediction layer with tf.variable_scope('pred') as scope: w = tfu.get_weights([100,2], name='pred_w') b = tfu.get_bias([1, 2], name='pred_b') pred = tf.nn.relu(tf.matmul(fc1, w) + b) #Define the loss loss = tf.nn.l2_loss(pred - lb, name='loss') return loss def train(stepNum, initLr=0.01): g = tf.Graph() with g.as_default(): loss = x1_x2_diff_net_v0() lr = tf.train.exponential_decay(initLr, stepNum, 100, 0.1, staircase=True) for tv in tf.trainable_variables(): print (tv.name) # Compute gradients. opt = tf.train.GradientDescentOptimizer(lr) grads = opt.compute_gradients(loss) # Apply gradients. apply_gradient_op = opt.apply_gradients(grads, global_step=stepNum) 示例文件中的te方法train中获取了代码片段。

1 个答案:

答案 0 :(得分:3)

糟糕!我将整数传递给stepNum而不是tf.Variable。它现在解决了。如果错误消息更直观,那将是很好的。