Kaggle competition: Digit Recognizer
submission_data.loc[:, 'price'] = np.expm1(ytest_linear )<save_to_csv>
transform = transforms.Normalize(0, 255, inplace=False)
 train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 submit_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 features_train = transform(torch.Tensor(np.reshape(train_df.iloc[:,1:].to_numpy() ,(-1,1,28,28))))
labels_train = torch.LongTensor(train_df['label'].to_numpy())
 features_submit = transform(torch.Tensor(np.reshape(submit_df.to_numpy() ,(-1,28,28))))
train_len = int(features_train.shape[0]*0.6)
 valid_len = int(features_train.shape[0]*0.2)
 train_set = torch.utils.data.TensorDataset(features_train[:train_len],labels_train[:train_len])
 valid_set = torch.utils.data.TensorDataset(features_train[train_len:(train_len+valid_len)],labels_train[train_len:(train_len+valid_len)])
 test_set = torch.utils.data.TensorDataset(features_train[(train_len+valid_len):],labels_train[(train_len+valid_len):])
 train_loader = DataLoader(train_set, batch_size = 50)
 valid_loader = DataLoader(valid_set, batch_size = 50)
 test_loader = DataLoader(test_set, batch_size = 50 )
submission_data.to_csv('submission.csv', index=False )<load_pretrained>
class CNN_classifier(nn.Module):
def __init__(self):
super(CNN_classifier, self ).__init__()
self.conv1 = nn.Conv2d(1, 32, 5, padding=2)
 self.conv2 = nn.Conv2d(32, 32, 5, padding=2)
 self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
 self.pool = nn.MaxPool2d(2, 2)
 self.fc1 = nn.Linear(64*3*3, 256)
 self.fc2 = nn.Linear(256, 10)
 self.dropout = nn.Dropout(0.25)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.dropout(x)
 x = self.pool(F.relu(self.conv2(x)))
 x = self.dropout(x)
 x = self.pool(F.relu(self.conv3(x)))
 x = self.dropout(x)
 x = x.view(-1, 64*3*3)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = self.fc2(x)
 return x
model = CNN_classifier()
print(model )
!apt-get install p7zip
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/train.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/test.tsv.7z
!p7zip -d -f -k /kaggle/input/mercari-price-suggestion-challenge/sample_submission.csv.7z<load_from_csv>
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters() , lr=0.01)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()
!unzip /kaggle/input/mercari-price-suggestion-challenge/sample_submission_stg2.csv.zip
!unzip /kaggle/input/mercari-price-suggestion-challenge/test_stg2.tsv.zip<import_modules>
n_epochs = 10
valid_plot = []
train_plot = []
valid_loss_min = np.Inf
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 model.eval()
for data, target in valid_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.sampler)
 valid_loss = valid_loss/len(valid_loader.sampler)
 train_plot.append(train_loss)
 valid_plot.append(valid_loss)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
 epoch, train_loss, 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_mnist_cnn.pt')
 valid_loss_min = valid_loss
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge, LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.preprocessing import LabelBinarizer<load_from_csv>
test_loss = 0.0
correct_pred = 0
false_images = torch.empty(( 1, 28, 28))
false_preds = []
false_targets = []
model.eval()
for data, target in test_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 test_loss += loss.item() *data.size(0)
 _, pred = torch.max(output, 1)
 correct_pred += torch.sum(pred == target ).item()
true_pred = pred==target
for i, value in enumerate(true_pred):
if value.item() == False:
false_images = torch.cat(( false_images, data[i]), dim=0)
 false_preds.append(pred[i])
 false_targets.append(target[i])
 false_images = false_images[1:]
test_loss = test_loss/len(test_loader.dataset)
 accuracy = correct_pred /len(test_loader.dataset)
 print('Test Loss: {:.6f}
'.format(test_loss))
print('Accuracy: {:.2f}%'.format(accuracy*100))
train = pd.read_table('train.tsv')
 test = pd.read_table('test_stg2.tsv' )<count_missing_values>
data = features_submit.view(-1,1,28,28)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 score, predictions = torch.topk(output, 1 )
<set_options><EOS> 
ImageId = np.arange(1,predictions.size() [0]+1)
 Label = predictions.view(predictions.size() [0] ).numpy()
new_submission = pd.DataFrame({'ImageId': ImageId, 'Label': Label})
 new_submission.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )