import torch
import pandas as pd
import numpy as np
df = pd.DataFrame({
'id':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
'applist':[
[1,4,3,7,8],
[5,7,32,4,68,23,4,78,23,1],
[21,23,45],
[3,2,8,6,4,7],
[6,7,8,23,56,76],
[2,3],
[1,6,3,5,2],
[9,4,12,66,90],
[34,36,37],
[4,5,67,8,2,1],
[5,2,3,6,8,1,8],
[1,2,3],
[89],
[4,3,44],
[33,45,12,34],
[55,8],
[77,54,20],
[21,45,89],
[17,13,14,10],
[2,6]
],
'label':[0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,1,0,0],
})
var_list = 'applist'
Y = df['label'].values
X = df['applist'].values
def cust2mpad(x,maxlen=10):
pad = (np.zeros(maxlen)).tolist()
TT = []
for i,j in enumerate(x):
TT.append(j+pad[0:maxlen-len(j)])
return TT
X = cust2mpad(X,maxlen=10)
class model(torch.nn.Module):
def __init__(self):
super(model,self).__init__()
self.emb = torch.nn.Embedding(100,5)
self.line1 = torch.nn.Linear(5,2)
self.relu = torch.nn.ReLU()
self.line2 = torch.nn.Linear(2,1)
def forward(self,input):
x1 = self.emb(input)
x2 = x1.mean(dim=1)
x3 = self.line1(x2)
x4 = self.relu(x3)
x5 = self.line2(x4)
return x5
mymodel = model()
optimizer = torch.optim.Adam(mymodel.parameters(),lr=0.001)
lossf = torch.nn.BCEWithLogitsLoss()
train_da = torch.tensor(X,dtype=torch.float32)
train_lab = torch.tensor(Y,dtype=torch.float32)
train_dataset = torch.utils.data.TensorDataset(train_da,train_lab)
traind = torch.utils.data.DataLoader(train_dataset,batch_size=5,shuffle=False)
for jj in range(5):
i = 0
for x,y0 in traind:
i = i+1
y = mymodel(x.int())
loss = lossf(y,y0.float().unsqueeze(1))
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(jj,i,loss.item())
if torch.isnan(loss).item():
break
yy = mymodel(train_da.int())
emb = mymodel.emb.weight.data.numpy()