DeepMatch
DeepMatch copied to clipboard
How to load saved SDM weights properly to reproduce embeddings?
Describe the question(问题描述) After I saved SDM weights and loaded it in another process. It produced different user embeddings.
How to save SDM model properly and then load it properly to reproduce embeddings?
Operating environment(运行环境):
- python version [e.g. 3.7.3]
- tensorflow version [e.g. 2.2.0,]
- deepmatch version [GPU e.g. 0.1.3,]
refer to examples/run_sdm.py
@wangzhegeek Thanks for your quick reply. My case is I trained the model in one notebook to get the user embeddings and item embeddings. Then I loaded the model in another notebook and got the same item embeddings but completely different user embeddings. I assume the reason is that some hidden states of RNN layer is lost based on my reading from this thread https://github.com/keras-team/keras/issues/11335
我先train好了model,save下来: K.set_learning_phase(True)
import tensorflow as tf
if tf.version >= '2.0.0': tf.compat.v1.disable_eager_execution()
model = SDM(user_feature_columns, item_feature_columns, history_feature_list=['events', 'event_types', 'event_classes', 'event_categories'], units=embedding_dim, num_sampled=100, )
optimizer = optimizers.Adam(lr=0.001, clipnorm=5.0)
model.compile(optimizer=optimizer, loss=sampledsoftmaxloss) # "binary_crossentropy")
history = model.fit(train_model_input,
train_label,
batch_size=512,
epochs=1,
verbose=1,
validation_data=(test_model_input, test_label),
)
K.set_learning_phase(False)
model.save('/tmp/saved_model.h5')
然后再加载模型: from deepmatch.layers import custom_objects loaded_model = load_model('/tmp/saved_model.h5',custom_objects)
然后用加载的模型获取embedding: test_user_model_input = test_model_input all_item_model_input = {"event_idx": betting_event_mapping['betting_event_idx'].values, }
user_embedding_model = Model(inputs=loaded_model.user_input, outputs=loaded_model.user_embedding) item_embedding_model = Model(inputs=loaded_model.item_input, outputs=loaded_model.item_embedding)
user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12) item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12)
print(user_embs.shape) print(item_embs.shape)
出现Attribute Error:
AttributeError: 'Model' object has no attribute 'user_input'
AttributeError Traceback (most recent call last)
AttributeError: 'Model' object has no attribute 'user_input'
同样的问题 不知道有没有解决?
采用save_weights和load_weights方式也出现该问题
Describe the question(问题描述) After I saved SDM weights and loaded it in another process. It produced different user embeddings.
How to save SDM model properly and then load it properly to reproduce embeddings?
Operating environment(运行环境):
- python version [e.g. 3.7.3]
- tensorflow version [e.g. 2.2.0,]
- deepmatch version [GPU e.g. 0.1.3,]
请问这个问题你自己有什么解决方案吗?
这个问题现在都有,至今未解决