2016-04-27 56 views
0

我得到錯誤:Tensorflow apply_gradients拋出錯誤

類型錯誤:「AssignAdd」運算的輸入「REF」需要下面的功能train的線apply_gradient_op = opt.apply_gradients(grads, global_step=stepNum)升值輸入

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) 

任何可能出錯的指針?我在cifar10.py示例文件中摘取了方法train中的代碼片段。

回答

3

糟糕!我正在將一個整數傳遞給stepNum而不是tf.Variable。現在解決了。如果錯誤信息會更直觀,那將會很棒。