【发布时间】:2020-02-04 14:23:05
【问题描述】:
我有 TensorFlow 2.0 和 Python 3.7.5。
我编写了以下代码来执行小批量梯度下降:
@tf.function
def train_one_step(model, mask_model, optimizer, x, y):
'''
Function to compute one step of gradient descent optimization
'''
with tf.GradientTape() as tape:
# Make predictions using defined model-
y_pred = model(x)
# Compute loss-
loss = loss_fn(y, y_pred)
# Compute gradients wrt defined loss and weights and biases-
grads = tape.gradient(loss, model.trainable_variables)
# type(grads)
# list
# List to hold element-wise multiplication between-
# computed gradient and masks-
grad_mask_mul = []
# Perform element-wise multiplication between computed gradients and masks-
for grad_layer, mask in zip(grads, mask_model.trainable_weights):
grad_mask_mul.append(tf.math.multiply(grad_layer, mask))
# Apply computed gradients to model's weights and biases-
optimizer.apply_gradients(zip(grad_mask_mul, model.trainable_variables))
# Compute accuracy-
train_loss(loss)
train_accuracy(y, y_pred)
return None
在代码中,“mask_model”是一个掩码,可以为 0 或 1。“mask_model”的用途是控制训练哪些参数(因为,0 * 梯度下降 = 0)。
我的问题是,我在“train_one_step()”TensorFlow 装饰函数中使用“grad_mask_mul”列表变量。这会导致任何问题,例如:
ValueError: tf.function-decorated 函数试图创建变量 非第一次通话。
或者你们看到在 tensorflow 修饰函数中使用列表变量有什么问题吗?
谢谢!
【问题讨论】:
标签: python python-3.x tensorflow tensorflow2.0