Tensorflow在值更改后找不到变量



我在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图执行,而不是急于执行,因为它大大提高了我的代码的速度。

你知道我做错了什么吗?

发生这种情况是因为您更改了OPTIMIZER本身,而不是它的学习率。这会导致@tf.function.中出现一些问题

因此,不要通过更改整个优化器来更改优化器的lr,可以通过使用以下代码来实现:

optimzer.lr = lr

希望这会有所帮助!

相关内容

  • 没有找到相关文章

最新更新