I recently coded up my own implementation of a cross validation script for pytorch and my models keep overfitting. Can someone help me out here. I am desperate. Here’s the code.
def predict(val_x,model=mymod,loss=loss,thresh=0.5):
valxtorch=torch.tensor(val_x,dtype=torch.long).cuda()
#valytorch=torch.tensor(val_y,dtype=torch.float32).cuda()
vald=torch.utils.data.TensorDataset(valxtorch)
testit=torch.utils.data.DataLoader(vald,batch_size=1024)
sig=nn.Sigmoid()
a=[]
tes=[]
losse=[]
for i,(trainx) in enumerate(testit):
xl=trainx[0]
pred=model(xl)
#losses=loss(pred,trainy)
pred=pred.cpu().detach()
#losse.append(losses.item())
pred=sig(pred).numpy()
a.append(list(pred))
#tes.append(trainy.cpu().detach())
b=[]
for i in a:
for j in i:
b.append(j)
testq=[]
for i in tes:
for j in i:
testq.append(int(j))
a=[]
for i in b:
if i>=thresh:
a.append(1)
else:
a.append(0)
#valy=list(np.asarray(val_y,dtype=int))
#valloss=sum(losse)/len(losse)
return a
def Train(model,train_x,train_y,n_epochs=4,splits=splits,scheduler=None):
it=0
best=0.0
for k in range(len(splits)):
print(“Fold {}”.format(k+1))
v=k
train,val=splits[v]
train12=torch.tensor(train_x[train],dtype=torch.long)
trainy12=torch.tensor(train_y[train],dtype=torch.float32)
traintorch=train12.cuda()
trainytorch=trainy12.cuda()
valxt=torch.tensor(train_x[val],dtype=torch.long)
valyt=torch.tensor(train_y[val],dtype=torch.float32)
valxtorch=valxt.cuda()
valytorch=valyt.cuda()
reqd=torch.utils.data.TensorDataset(traintorch,trainytorch)
reqd1=torch.utils.data.TensorDataset(valxtorch,valytorch)
load=torch.utils.data.DataLoader(reqd,batch_size=512,shuffle=True)
valload=torch.utils.data.DataLoader(reqd1,batch_size=1024,shuffle=True)
opti=torch.optim.Adam(model.parameters())
mymod.train()
for j in range(n_epochs):
avg=0
for i,(x,y) in enumerate(load):
xl=x
opti.zero_grad()
pred=mymod(xl)
if scheduler!=None:
scheduler.batch_step()
losses=loss(pred,y)
avg+=losses.item()
losses.backward()
opti.step()
it+=1
avg=avg/len(load)
“”“qwe,valloss=predict(val_x,val_y,mymod)
if qwe>best:
best=qwe
torch.save(mymod,’/content/gdrive/My Drive/My files/Jigsaw/quoramodbest.pt’)”""
  print("Epoch {}/{}: Loss:{}".format(j+1,n_epochs,avg))
mymod.eval()
sig=nn.Sigmoid()
avgs=[]
for i,(x,y) in enumerate(valload):
  a=[]
  xl=x
  pred=mymod(xl)
  pred=sig(pred)
  pred=pred.detach().cpu().numpy()
  a.append(list(pred))
  b=[]
  for i in a:
    for j in i:
      b.append(j)
  c=[]
  for i in b:
    if i>=0.5:
      c.append(1)
    else:
      c.append(0)
  valyq=list(np.asarray(y.detach().cpu().numpy(),dtype=int))
  avgs.append(f1_score(c,valyq))
print(sum(avgs)/len(avgs))

return model