import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torch.nn.functional as F
from sklearn import preprocessing
class OttoDataset(Dataset):
def __init__(self,file_path):
data = pd.read_csv(file_path,delimiter=',')
le = preprocessing.LabelEncoder()
data['target'] = le.fit_transform(data['target'])
self.x_data = torch.tensor(data.iloc[:,1:-1].values,dtype=torch.float32)
self.y_data = torch.tensor(data['target'].values,dtype=torch.long)
self.len = data.shape[0]
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
train_dataset = OttoDataset('otto_train.csv')
#print(train_dataset.y_data)
test_dataset = pd.read_csv('otto_test.csv')
#print(test_dataset)
test_x_data = torch.tensor(test_dataset.iloc[:,1:].values,dtype=torch.float32)
#print(test_x_data.shape)
train_data_loader = DataLoader(dataset=train_dataset,shuffle=True,batch_size=32,num_workers=2)
#print(train_dataset.y_data)
class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.l1 = torch.nn.Linear(93,128)
self.l2 = torch.nn.Linear(128,64)
self.l3 = torch.nn.Linear(64,32)
self.l4 = torch.nn.Linear(32,9)
self.activate = torch.nn.ReLU()
def forward(self,x):
x = self.activate(self.l1(x))
x = self.activate(self.l2(x))
x = self.activate(self.l3(x))
return self.l4(x)
model = Model()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
def train(epoch):
running_loss = 0
for batch_idx, data in enumerate(train_data_loader,0):
inputs, labels = data
y_pred = model(inputs)
loss = criterion(y_pred,labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('epoch= ',epoch,'bacth_idx= ',batch_idx,'loss= ',running_loss / 300)
running_loss = 0
def test():
with torch.no_grad():
outputs = model(test_x_data)
print(outputs)
outputs_propability = F.softmax(outputs,dim=1).numpy()
predictions_df = pd.DataFrame(outputs_propability, columns=['Class_1', 'Class_2', 'Class_3', 'Class_4', 'Class_5', 'Class_6', 'Class_7', 'Class_8', 'Class_9'])
predictions_df.insert(0, 'id', test_dataset['id'])
predictions_df.to_csv('predictions.csv', index=False)
if __name__ == '__main__':
for epoch in range(100):
train(epoch)
test()
多分类任务中,使用Crossentropyloss的时候,训练数据中的标签需要为long整型数据,以支持类别索引。
最终分数: