获取子类模型的权重
我一直在研究电影镜头和协同过滤。我使用了一个子类模型,并希望获得经过训练的嵌入,以在二维等中可视化它们。不知何故,权重似乎是空的,但我不知道为什么。显然,模型在训练时变得更好(如果我绘制历史记录['loss'])。我不知道为什么权重是空的或如何获取它们如果我使用模型而不是子类它工作正常并且我可以获得权重。下面是我的代码
class RegressionModel(Model):
def __init__(self, embedding_size, max_user_id, max_item_id):
super().__init__()
self.user_embedding = Embedding(output_dim=embedding_size,
input_dim=max_user_id + 1,
input_length=1,
name='user_embedding')
self.item_embedding = Embedding(output_dim=embedding_size,
input_dim=max_item_id + 1,
input_length=1,
name='item_embedding')
self.flatten = Flatten()
self.dot = Dot(axes=1)
def call(self, inputs, training=False):
user_inputs = inputs[0]
item_inputs = inputs[1]
user_vecs = self.flatten(self.user_embedding(user_inputs))
item_vecs = self.flatten(self.item_embedding(item_inputs))
y = self.dot([user_vecs, item_vecs])
return y
max_user_id = ratings['userId'].max()
max_item_id = ratings['movieId'].max()
thename = 'models/my_model'#cannot save as .h5 as it's a subclassed model
model = RegressionModel(64, max_user_id, max_item_id)
model.compile(optimizer="adam", loss='mae')
inputs_train = [np.array(ratings_train["userId"]), np.array(ratings_train["movieId"])]
inputs_test = [np.array(ratings_test["userId"]), np.array(ratings_test["movieId"])]
model.fit(inputs_train,
np.array(ratings_train["rating"]),
batch_size=64,
epochs=10,
validation_split=0.1,
shuffle=True)
#save model
#model.save doesn't work for a subclassed model but save_weights works
def get_model():
return RegressionModel(s, max_user_id, max_item_id)
model.save_weights(thename, save_format='tf')
#load the model
model_new = get_model()
model_new.compile(optimizer="adam", loss='mae')
model_new.load_weights(thename)
weights = model_new.get_weights()
#this is empty (even without saving and then loading the weights)
weights
I have been playing around with movielens and collaborative filtering. I used a subclassed model and would like to get the trained embeddings to visualise them in 2d etc. Somehow the weights seem to be empty but I don't know why. Clearly the model is getting better when being trained (if I plot history['loss']). I have no idea why the weights are empty or how to get them If I use a model not as subclass it works fine and I can get the weights. Below is my code
class RegressionModel(Model):
def __init__(self, embedding_size, max_user_id, max_item_id):
super().__init__()
self.user_embedding = Embedding(output_dim=embedding_size,
input_dim=max_user_id + 1,
input_length=1,
name='user_embedding')
self.item_embedding = Embedding(output_dim=embedding_size,
input_dim=max_item_id + 1,
input_length=1,
name='item_embedding')
self.flatten = Flatten()
self.dot = Dot(axes=1)
def call(self, inputs, training=False):
user_inputs = inputs[0]
item_inputs = inputs[1]
user_vecs = self.flatten(self.user_embedding(user_inputs))
item_vecs = self.flatten(self.item_embedding(item_inputs))
y = self.dot([user_vecs, item_vecs])
return y
max_user_id = ratings['userId'].max()
max_item_id = ratings['movieId'].max()
thename = 'models/my_model'#cannot save as .h5 as it's a subclassed model
model = RegressionModel(64, max_user_id, max_item_id)
model.compile(optimizer="adam", loss='mae')
inputs_train = [np.array(ratings_train["userId"]), np.array(ratings_train["movieId"])]
inputs_test = [np.array(ratings_test["userId"]), np.array(ratings_test["movieId"])]
model.fit(inputs_train,
np.array(ratings_train["rating"]),
batch_size=64,
epochs=10,
validation_split=0.1,
shuffle=True)
#save model
#model.save doesn't work for a subclassed model but save_weights works
def get_model():
return RegressionModel(s, max_user_id, max_item_id)
model.save_weights(thename, save_format='tf')
#load the model
model_new = get_model()
model_new.compile(optimizer="adam", loss='mae')
model_new.load_weights(thename)
weights = model_new.get_weights()
#this is empty (even without saving and then loading the weights)
weights
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上从tensorflow 2.2开始你可以保存自定义模型。
您需要指定格式,
尝试查看无法保存自定义子类模型
Actually from tensorflow 2.2 you can save custom models.
You need to specify format,
Try to have a look at Can't save custom subclassed model