Q:

Tensorflow apply_gradients throws error

I get the error: 
TypeError: Input 'ref' of 'AssignAdd' Op requires l-value input 
on the line apply_gradient_op = opt.apply_gradients(grads, global_step=stepNum) of the function train below. 
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)

Any pointers on what might be going wrong? I took snippets of code from te method train in cifar10.py example file. 

A:

Oops! I was passing an integer into stepNum instead of tf.Variable. Its resolved now. It would be great if the error messages would be more intuitive. 

