RUCAIBox / NCL

[WWW'22] Official PyTorch implementation for "Improving Graph Collaborative Filtering with Neighborhood-enriched Contrastive Learning".
119 stars 20 forks source link

绘图问题 #24

Closed Doctor-JZL closed 10 months ago

Doctor-JZL commented 2 years ago
    Hi,

We firstly train models with --embedding_size=2, then

import torch.nn.functional as F
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from recbole.quick_start import load_data_and_model

filepath = 'path/to/your/model'  # replace this to your path

config, model, dataset, train_data, valid_data, test_data = load_data_and_model(
    model_file=filepath,
)

item_emb = model.item_embedding.weight.cpu().detach()
item_emb = F.normalize(item_emb, dim=1).numpy()
print(item_emb.shape)

plt.figure(figsize=(3, 3))

df = pd.DataFrame({
    'x': item_emb.T[0],
    'y': item_emb.T[1]
})

ax = sns.kdeplot(
    data=df, x='x', y='y',
    thresh=0, levels=300, cmap=sns.color_palette('light:b', as_cmap=True)
)

plt.xlabel('')
plt.ylabel('')

plt.tight_layout()
plt.savefig('your pdf file name', format='pdf', dpi=300)  # replace this to your path
plt.show()

Originally posted by @hyp1231 in https://github.com/RUCAIBox/NCL/issues/8#issuecomment-1102792819

Doctor-JZL commented 2 years ago

你好,embedding_size设置为2,不是会影响指标吗?

hyp1231 commented 2 years ago

你好,embedding_size设置为2,不是会影响指标吗?

是的,这里设置为2只是为了可视化方便。

Doctor-JZL commented 2 years ago

你好,按我的理解,应该是要训练完之后再进行绘图吧?但是这上面的代码好像没有体现出训练过程?

linzihan-backforward commented 2 years ago

你好,按我的理解,应该是要训练完之后再进行绘图吧?但是这上面的代码好像没有体现出训练过程?

是的,需要在上面代码中将filepath指定为训练好的模型文件。

loredunk commented 2 years ago

你好这个代码具体怎么使用呀 我复制到ncl文件夹下面 跑完出现model_name [NCL] is not the name of an existing model.

linzihan-backforward commented 2 years ago

你好这个代码具体怎么使用呀 我复制到ncl文件夹下面 跑完出现model_name [NCL] is not the name of an existing model.

您好,首先您需要将NCL模型成功训练出来,然后将得到的模型文件地址修改到上面的代码中,在任何目录下运行上面代码即可。