【发布时间】:2021-09-23 17:08:22
【问题描述】:
我在 tensorflow 2.5.0 中构建了一个自定义模型,我正在尝试对超参数值进行网格搜索。模型训练正确,但是当我重新初始化网格搜索循环中的参数时会引发错误(它似乎与张量流图有关,但我不知道问题的确切根源是什么)。下面包含一个简单的线性回归模型的最小可重复示例。
import tensorflow as tf
import numpy as np
class Model:
def __init__(self, X, y):
self.X = X
self.y = y
# initialize model weights
def initialize_model(self, lr, optimizer):
self.lr = lr
initializer = tf.keras.initializers.glorot_normal()
self.weights = tf.Variable(initializer(shape = (X.shape[1],)),
name = 'weights')
self.optimizer = optimizer(lr = lr)
# loss function
def sq_error_loss(self):
preds = tf.linalg.matvec(self.X, self.weights)
sq_error = tf.math.reduce_sum((self.y - preds)**2)
return sq_error
# one epoch of training
@tf.function
def train_step(self):
with tf.GradientTape() as tape:
loss = self.sq_error_loss()
grads = tape.gradient(loss, [self.weights])
self.optimizer.apply_gradients(zip(grads, [self.weights]))
return loss
# train the model for some number of epochs
def train(self, num_epochs = 10):
for e in range(num_epochs):
loss = self.train_step()
print('Training finished.')
# grid search over different values of the learning rate hyperparameter
def grid_search(self, lrs):
for lr in lrs:
self.initialize_model(lr, tf.keras.optimizers.Adam)
self.train()
X = tf.Variable(np.random.normal(size = (1000, 10)).astype('float32'))
y = tf.Variable(np.random.normal(size = 1000).astype('float32'))
model = Model(X, y)
lrs = [i*0.01 for i in range(1, 11)]
model.grid_search(lrs)
这会引发以下错误:
FailedPreconditionError: Could not find variable _AnonymousVar143. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/_AnonymousVar143/class tensorflow::Var does not exist.
[[node Adam/Cast_2/ReadVariableOp (defined at <ipython-input-112-1e76ddb3815a>:34) ]] [Op:__inference_train_step_293348]
Function call stack:
train_step
当我从 train_step() 函数中删除 @tf.function 装饰器时,该错误已得到修复,但理想情况下,我希望使用 tensorflow 图形执行而不是急切执行,因为它可以显着提高我的代码速度。
任何想法我做错了什么?
【问题讨论】:
标签: python tensorflow tensorflow2.0