我想在tensorflow中使用get_slot_names从Momentum优化器中获取插槽的名称,因为在tensorflow网页中对其进行了解释here。我在我的代码中使用以下行来获取它们:
slots=tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,).get_slot_names()
我运行了我的图表然后当我打印插槽时它只返回一个空列表。任何帮助将不胜感激。
顺便说一下,我的网络在减少损失或其他事情方面做得很好。 此外,我尝试与其他优化器,但它有同样的问题。
我在ubuntu 14中使用tf 1.3。 谢谢,
答案 0 :(得分:0)
Slot_names
是特定于优化程序的。如果您想为每个可训练变量获取插槽,可以使用get_slot
方法和正确的slot_name
。 momentum_optimizer
创建的插槽名称(默认情况下)为momentum
。下面是一个简单的例子来说明这些要点。
x_input = np.linspace(0, 30, 200)
y_input = 4 * x_input + 6
W = tf.Variable(0.0, name="weight")
b = tf.Variable(0.0, name="bias")
X = tf.placeholder(tf.float32, name='InputX')
Y = tf.placeholder(tf.float32, name='InputY')
Y_pred = X * W + b
loss = tf.reduce_mean(tf.square(Y_pred - Y))
# define the optimizer
optimizer = tf.train.MomentumOptimizer(learning_rate=0.001, momentum=.9)
# training op
train_op = optimizer.minimize(loss)
# print the optimizer slot name
print(optimizer.get_slot_names())
# Results:['momentum']
# print out slot created for each trainable variables using the slot_name from above result
for v in tf.trainable_variables():
print(optimizer.get_slot(v, 'momentum'))
# Results: <tf.Variable 'weight/Momentum:0' shape=() dtype=float32_ref>
<tf.Variable 'bias/Momentum:0' shape=() dtype=float32_ref>
答案 1 :(得分:0)
代码中唯一的问题是slot_variables是在最小化调用(actually in apply_gradients)期间创建的。因为你在之前调用了get_slot_variables() - 它们都是空的。
所以而不是
slots=tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,).get_slot_names()
你应该做
opt = tf.train.MomentumOptimizer(learning_rate=1e-3, momentum=0.9,)
train_op = opt.minimize(your_loss) # or anything else
slota = opt.get_slot_names() # this will work
原因很简单 - 许多插槽都是特定的变量,例如Adam这样的方法会为每个优化变量创建一个插槽,并且在调用.minimize之前 - optimiser不知道哪些变量它会优化。
特别是,对于MomentumOptimizer,每个变量都保持累积渐变。因此,在调用最小化之前无法计算它们。这些累积的渐变存储在“动量”槽中(对于名称来说是非常糟糕的选择,但这是它们在TF中的位置)。