我正在尝试使用自适应学习率和基于Adam梯度的优化来实现卷积神经网络。我有以下代码:
# learning rate schedule
schedule = np.array([0.0005, 0.0005,
0.0002, 0.0002, 0.0002,
0.0001, 0.0001, 0.0001,
0.00005, 0.00005, 0.00005, 0.00005,
0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001])
# define placeholder for variable learning rate
learning_rates = tf.placeholder(tf.float32, (None),name='learning_rate')
# training operation
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
labels=one_hot_y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rates)
training_operation = optimizer.minimize(loss_operation)
用于运行会话的代码:
.
.
.
_, loss = sess.run([training_operation, loss_operation],
feed_dict={x: batch_x, y: batch_y, learning_rate: schedule[i]})
.
.
.
i代表纪元计数,该纪元计数初始化为0,因此从技术上讲,它应使用时间表中的第一个值。
每当我尝试运行此命令时,都会出现以下错误:
InvalidArgumentError:必须使用dtype float输入占位符张量'learning_rate_2'的值
[[节点:learning_rate_2 = Placeholderdtype = DT_FLOAT,形状= [],_ device =“ / job:localhost /副本:0 /任务:0 / cpu:0”]]
有人遇到过同样的问题吗?我尝试重新初始化会话,重命名变量,但无济于事。
最佳答案
找到了一个中间解决方案。
.
.
.
for i in range(EPOCHS):
XX_train, yy_train = shuffle(X_train, y_train)
# code for adaptive rate
optimizer = tf.train.AdamOptimizer(learning_rate = schedule[i])
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x, batch_y = XX_train[offset:end], yy_train[offset:end]
_, loss = sess.run([training_operation, loss_operation], feed_dict={x: batch_x, y: batch_y})
.
.
.
不是很优雅,但至少可以正常工作。
关于python-3.x - 固定时间表的自适应学习率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43527131/