Pytorch的nn.embedding的一些理解

检验

        • 动机
        • 同class内的embedding
        • 不同class的embedding

动机

在做Bundle Recommendation的时候涉及到Bundle和Item的embedding,于是想到因为他们的索引都是从1开始编的,经过embedding之后的特征是否会有重叠,因此分几种情况做了如下实验。

同class内的embedding

不同class的embedding

import torch
import torch.nn as nn

loss_func = nn.CrossEntropyLoss()

inputs = [0]
targets = [0]


class Stu(nn.Module):
    def __init__(self):
        super(Stu, self).__init__()
        self.embedding = nn.Embedding(5, 3)
        self.layer1 = nn.Linear(3, 2)

    def forward(self, inputs):
        print('打印五种类型的emb')
        for i in range(5):
            print(stu.embedding(torch.tensor(i)))
        inputs = torch.tensor(inputs)
        inputs = self.embedding(inputs)
        inputs = self.layer1(inputs)
        return inputs


stu = Stu()
optimizer = torch.optim.Adam(stu.parameters(), lr=1)

pred = stu(0)

loss = loss_func(pred.unsqueeze(0), torch.tensor([0]))
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()


pred = stu(2)


print('打印完毕,计算损失')
loss = loss_func(pred.unsqueeze(0), torch.tensor([0]))
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()

pred = stu(3)

print('打印完毕,计算损失')
loss = loss_func(pred.unsqueeze(0), torch.tensor([0]))
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()

pred = stu(4)

print('打印完毕,计算损失')
loss = loss_func(pred.unsqueeze(0), torch.tensor([0]))
print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()


# print('打印五种类型的emb')
# for i in range(5):
#     print(stu.embedding(torch.tensor(i)))



# for name in stu.state_dict():
#      print(name)
#      print(stu.state_dict()[name])

ddd = stu.state_dict()['embedding.weight']
print(ddd.requires_grad_(True))
# params = list(stu.named_parameters())#get the index by debuging
# print(params[0][0])#name
# print(params[0][1].data.requires_grad_(True))#data

# print(dddddd.state_dict()['embedding'])
# print(dddddd)
# for name, param in dddddd:
#     print(name, param)

你可能感兴趣的:(python,数据挖掘,人工智能)