Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
%matplotlib inline
from sklearn.model_selection import train_test_split<import_modules>
class TrainDataset(Dataset):
def __init__(self, file_path, transform=None):
self.data = pd.read_csv(file_path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
images = self.data.iloc[index, 1:].values.astype(np.uint8 ).reshape(( 28, 28, 1))
labels = self.data.iloc[index, 0]
if self.transform is not None:
images = self.transform(images)
 return images, labels
transform = transforms.ToTensor()
train_data = TrainDataset('.. /input/digit-recognizer/train.csv', transform=transform)
 valid_size = 0.15
num_train = len(train_data)
 indices = list(range(num_train))
torch.manual_seed(0)
 np.random.shuffle(indices)
 split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 batch_size = 20
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler)
 class TestDataset(Dataset):
def __init__(self, file_path, transform=None):
self.data = pd.read_csv(file_path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
images = self.data.iloc[index, :].values.astype(np.uint8 ).reshape(( 28, 28, 1))
if self.transform is not None:
images = self.transform(images)
 return images
test_data = TestDataset('.. /input/digit-recognizer/test.csv', transform=transform)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False )
from sklearn.model_selection import train_test_split<prepare_x_and_y>
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1)
 self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
 hidden_1 = 1024
hidden_2 = 512
self.fc1 = nn.Linear(128*7*7, hidden_1)
 self.fc2 = nn.Linear(hidden_1, hidden_2)
 self.fc3 = nn.Linear(hidden_2, 10)
 self.dropout = nn.Dropout(p=0.5)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = x.view(-1, 128*7*7)
 x = self.dropout(x)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = F.relu(self.fc2(x))
x = self.dropout(x)
 x = self.fc3(x)
 return x
X=train_data.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_data['Cover_Type']<split>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model = Net()
criterion = nn.CrossEntropyLoss()
lr = 0.001
optimizer = optim.Adam(model.parameters() , lr = lr)
 model.to(device )
X_train,X_val,y_train,y_val=train_test_split(X,y,random_state=12 )<import_modules>
t0 = time.time()
n_epochs = 50
valid_loss_min = np.Inf
for epoch in range(n_epochs):
train_loss = 0.0
model.train()
for data, target in train_loader:
data, target = data.to(device),target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 valid_loss = 0.0
model.eval()
with torch.no_grad() :
for data, target in valid_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.dataset)
 valid_loss = valid_loss/len(valid_loader.dataset)
 print("Epoch: {}/{}.. ".format(epoch+1, n_epochs),
"Training Loss: {:.6f}.. ".format(train_loss),
"Validation Loss: {:.6f}.. ".format(valid_loss))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'model.pt')
 valid_loss_min = valid_loss
print('Training and validation executed in {:.1f} minutes.'.format(( time.time() - t0)/60))
from sklearn.ensemble import RandomForestClassifier<train_model>
model.load_state_dict(torch.load('model.pt'))
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(X_train,y_train )<compute_test_metric>
class_correct = list(0.for i in range(10))
class_total = list(0.for i in range(10))
model.eval()
with torch.no_grad() :
for data, target in valid_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 _, pred = torch.max(output, dim=1)
 correct = pred == target.view_as(pred)
 for i in range(len(target)) :
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
for i in range(10):
if class_total[i] > 0:
print('Validation Accuracy of %5s: %.1f%%(%2d/%2d)' %(
 str(i), 100 * class_correct[i] / class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
 else:
print('Validation Accuracy of %5s: N/A(no training examples)' %(classes[i]))
print('
Validation Accuracy(Overall): %.1f%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)) )
rfc.score(X_val,y_val )<predict_on_test>
test_preds = torch.LongTensor()
model.eval()
with torch.no_grad() :
for data in test_loader:
data, target = data.to(device),target.to(device)
 output = model(data)
 _, pred = torch.max(output, dim=1)
 test_preds = torch.cat(( test_preds.cpu() , pred.cpu()), dim=0)
 submission = pd.DataFrame({"ImageId":list(range(1, len(test_preds)+1)) ,
"Label":test_preds.numpy() })
 submission
predict=rfc.predict(test_data.drop(labels=['Id'],axis=1))<prepare_output>
submission.to_csv("my_submission.csv", index=False, header=True )
Submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 Submission.head()<prepare_output>
submission = pd.read_csv("my_submission.csv")
 submission