Pra*_*att 6 python machine-learning deep-learning tensorflow
我想知道选项trainable=False和之间的区别tf.stop_gradient().如果我做出trainable选项,False我的优化器会不会考虑变量进行培训吗?在整个培训过程中,此选项是否使其成为恒定值?
Ish*_*nal 10
可训练=假
在这里,变量值在整个训练过程中将保持不变。优化器不会考虑这个变量进行训练,没有梯度更新操作。
停止梯度
在某些情况下,你想要计算一个 op 相对于一些变量的梯度,保持一些其他变量不变;但是对于其他操作,您也可以使用这些变量来计算梯度。所以在这里你不能使用trinable=False,因为你需要这些变量来与其他操作员一起训练。
stop_gradient对运维非常有用;您可以选择性地优化操作,以选择少数变量,同时保持其他不变。
y1 = tf.stop_gradient(W1x+b1)
y2 = W2y1+b2
cost = cost_function(y2, y)
# this following op wont optimize the cost with respect to W1 and b1
train_op_w2_b2 = tf.train.MomentumOptimizer(0.001, 0.9).minimize(cost)
W1 = tf.get_variable('w1', trainable=False)
y1 = W1x+b1
y2 = W2y1+b2
cost = cost_function(y2, y)
# this following op wont optimize the cost with respect to W1
train_op = tf.train.MomentumOptimizer(0.001, 0.9).minimize(cost)
Run Code Online (Sandbox Code Playgroud)