获取子类模型的权重

发布于 2025-01-16 02:43:49 字数 2435 浏览 0 评论 0原文

我一直在研究电影镜头和协同过滤。我使用了一个子类模型,并希望获得经过训练的嵌入,以在二维等中可视化它们。不知何故,权重似乎是空的,但我不知道为什么。显然,模型在训练时变得更好(如果我绘制历史记录['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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2025-01-23 02:43:49

实际上从tensorflow 2.2开始你可以保存自定义模型。
您需要指定格式,

# Save the model
model.save('path_to_my_model',save_format='tf')

# Recreate the exact same model purely from the file
new_model = keras.models.load_model('path_to_my_model')

尝试查看无法保存自定义子类模型

Actually from tensorflow 2.2 you can save custom models.
You need to specify format,

# Save the model
model.save('path_to_my_model',save_format='tf')

# Recreate the exact same model purely from the file
new_model = keras.models.load_model('path_to_my_model')

Try to have a look at Can't save custom subclassed model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文