Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<concatenate>
print(f'Using PyTorch v{torch.__version__}' )
calculated_probas = []
true_labels = []
for lang in val_predictions:
probabilities_, true_labels_ = val_predictions[lang]
calculated_probas.append(probabilities_)
 true_labels.append(true_labels_)
 calculated_probas = np.concatenate(calculated_probas)
 true_labels = np.concatenate(true_labels )<set_options>
train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
del val_predictions
gc.collect()<drop_column>
transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize(( 0.5,),(0.5,))
])
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 def get_digits(df):

 labels = []
start_inx = 0
if 'label' in df.columns:
labels = [v for v in df.label.values]
start_inx = 1
digits = []
for i in range(df.pixel0.size):
digit = df.iloc[i].astype(float ).values[start_inx:]
digit = np.reshape(digit,(28,28))
digit = transform(digit ).type('torch.FloatTensor')
 if len(labels)> 0:
digits.append([digit, labels[i]])
 else:
digits.append(digit)
 return digits
del true_labels, calculated_probas<drop_column>
train_X = get_digits(train)
 num_workers = 0
batch_size = 64
valid_size = 0.2
num_train = len(train_X)
 indices = list(range(num_train))
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)
 train_loader = torch.utils.data.DataLoader(train_X, batch_size=batch_size,
sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(train_X, batch_size=batch_size,
sampler=valid_sampler, num_workers=num_workers)
 dataiter = iter(train_loader)
 images, labels = dataiter.next()
print(type(images))
print(images.shape)
 print(labels.shape )
del dataset_for_training
del dataset_for_validation
gc.collect()<train_model>
def calc_out(in_layers, stride, padding, kernel_size, pool_stride):

 return int(( 1+(in_layers - kernel_size +(2*padding)) /stride)/pool_stride)
 class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
inputs = [1,32,64,64]
kernel_size = [5,5,3]
stride = [1,1,1]
pool_stride = [2,2,2]
layers = []
self.out = 28
self.depth = inputs[-1]
for i in range(len(kernel_size)) :
padding = int(kernel_size[i]/2)
 self.out = calc_out(self.out, stride[i], padding,
kernel_size[i], pool_stride[i])
 layers.append(nn.Conv2d(inputs[i], inputs[i+1], kernel_size[i],
stride=stride[i], padding=padding))
layers.append(nn.ReLU())
 layers.append(nn.Conv2d(inputs[i+1], inputs[i+1], kernel_size[i],
stride=stride[i], padding=padding))
layers.append(nn.ReLU())
 layers.append(nn.MaxPool2d(pool_stride[i],pool_stride[i]))
layers.append(nn.Dropout(p=0.2))
self.cnn_layers = nn.Sequential(*layers)
 print(self.depth*self.out*self.out)
 layers2 = []
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(self.depth*self.out*self.out, 512))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(512, 256))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(256, 256))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(256, 10))
self.fc_layers = nn.Sequential(*layers2)
 def forward(self, x):
x = self.cnn_layers(x)
 x = x.view(-1, self.depth*self.out*self.out)
 x = self.fc_layers(x)
 return x
model = Net()
model
experiment_duration = int(round(time.time() - experiment_start_time))
print('Duration of XLM-RoBERTa training is {0} seconds.'.format(
 experiment_duration))<predict_on_test>
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005 )
final_predictions = predict_with_classifier(
 texts=texts_for_submission,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len,
classifier=xlmr_based_classifier,
batch_size=batch_size_for_xlmr
)<save_to_csv>
n_epochs = 25
valid_loss_min = np.Inf
print(device)
 model.to(device)
 tLoss, vLoss = [], []
for epoch in range(n_epochs):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
data = data.to(device)
 target = target.to(device)
 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:
data = data.to(device)
 target = 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)
 tLoss.append(train_loss)
 vLoss.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_cifar.pt')
 valid_loss_min = valid_loss
with codecs.open('submission.csv', mode='w', encoding='utf-8', errors='ignore')as fp:
fp.write('id,toxic
')
 for lang in final_predictions:
probabilities, IDs = final_predictions[lang]
assert probabilities.shape == IDs.shape
assert len(probabilities.shape)== 1
for sample_idx in range(probabilities.shape[0]):
id_val = IDs[sample_idx]
proba_val = probabilities[sample_idx]
fp.write('{0},{1:.9f}
'.format(id_val, proba_val))<train_model>
model.load_state_dict(torch.load('model_cifar.pt')) ;
print('Experiment duration is {0:.3f}.'.format(time.time() - experiment_start_time))<set_options>
test_loss = 0.0
class_correct = [0]*10
class_total = [0]*10
model.eval()
conf_matrix = np.zeros(( 10,10))
for data, target in valid_loader:
data = data.to(device)
 target = target.to(device)
 output = model(data)
 loss = criterion(output, target)
 test_loss += loss.item() *data.size(0)
 _, pred = torch.max(output, 1)
 correct_tensor = pred.eq(target.data.view_as(pred))
correct = np.squeeze(correct_tensor.numpy())if device == "cpu" else np.squeeze(correct_tensor.cpu().numpy())
 for i in range(target.size(0)) :
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
conf_matrix[label][pred.data[i]] += 1
test_loss = test_loss/len(valid_loader.dataset)
 print('Test Loss: {:.6f}
'.format(test_loss))
for i in range(10):
if class_total[i] > 0:
print('Test Accuracy of %3s: %2d%%(%2d/%2d)' %(
 i, 100 * class_correct[i] / class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
 else:
print('Test Accuracy of %3s: N/A(no training examples)' %(classes[i]))
print('
Test Accuracy(Overall): %2d%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)) )
%matplotlib inline
tqdm.pandas() 
<load_from_csv>
test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 test_X = get_digits(test)
 test_loader = torch.utils.data.DataLoader(test_X, batch_size=batch_size,
num_workers=num_workers )
train = pd.read_csv('.. /input/quora-insincere-questions-classification/train.csv')
 test = pd.read_csv('.. /input/quora-insincere-questions-classification/test.csv' )<feature_engineering>
ImageId, Label = [],[]
for data in test_loader:
data = data.to(device)
 output = model(data)
 _, pred = torch.max(output, 1)
 for i in range(len(pred)) :
ImageId.append(len(ImageId)+1)
 Label.append(pred[i].cpu().numpy())
 sub = pd.DataFrame(data={'ImageId':ImageId, 'Label':Label})
 sub.describe
print("Tỉ lệ phần trăm số câu hỏi Insincere là:",(len(train.loc[train.target==1])) /(len(train.loc[train.target == 0])) * 100 )<string_transform>
sub.to_csv("submission.csv", index=False )