在我看到的关于 tfe 的所有教程(包括 tf 官方文档)中,该示例使用渐变磁带,并手动将所有渐变添加到计算渐变列表中,例如
variables = [w1, b1, w2, b2] <--- manually store all the variables
optimizer = tf.train.AdamOptimizer()
with tf.GradientTape() as tape:
y_pred = model.predict(x, variables)
loss = model.compute_loss(y_pred, y)
grads = tape.gradient(loss, variables) < ---- send them to tape.gradient
optimizer.apply_gradients(zip(grads, variables))
但这是唯一的方法吗?即使对于大型模型,我们也需要累积所有参数,或者我们可以以某种方式访问默认图形变量列表
尝试访问tf.get_default_graph().get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
或trainable_variables
在 tfe 会话中给出空列表。