Python TensorFlow 2 tf.函数装饰器
我有TensorFlow 2.0和Python 3.7.5 我编写了以下代码来执行小批量梯度下降,即:Python TensorFlow 2 tf.函数装饰器,python,python-3.x,tensorflow,tensorflow2.0,Python,Python 3.x,Tensorflow,Tensorflow2.0,我有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: # M
@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的掩码。“蒙版_模型”的使用是为了控制训练哪些参数(因为,0*梯度下降=0)
我的问题是,我在“train\u one\u step()”TensorFlow函数中使用“grad\u mask\u mul”列表变量。这是否会导致任何问题,例如:
ValueError:tf.function-decorated函数试图创建变量
非首次通话
或者你们看到了在tensorflow修饰函数中使用列表变量的一些问题吗
谢谢 这是TensorFlow 2中的一个错误。您可以在此处阅读更多信息这是TensorFlow 2中的一个错误。你可以在这里了解更多