Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
mnist = input_data.read_data_sets("/data/mnist", one_hot = True)
batch_size = 84
epochs = 100
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
 history = model.fit(
 train_datagen.flow(X, y, subset='training'),
validation_data=train_datagen.flow(X, y, batch_size=batch_size, subset='validation'),
epochs=epochs,
steps_per_epoch = X.shape[0] // batch_size,
batch_size=batch_size,
callbacks=callbacks,
use_multiprocessing=True )<load_from_csv>
for i in [random_crop,random_rot]:
a=random.sample(range(0,55000),22500*2)
 for j in a:
train_.append(i(mnist.train.images[j]))
labels_.append(mnist.train.labels[j] )
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<predict_on_test>
def model(x):
x = layers.conv2d(x,32*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,64*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.conv2d(x,64*2,(3,3),padding='SAME',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,128*2,(3,3),padding='SAME',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.conv2d(x,128*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.conv2d(x,256*2,(3,3),padding='VALID',activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.max_pool2d(x,2)
 x = layers.flatten(x)
 x = layers.fully_connected(x,64,activation_fn=None)
 x = tf.nn.relu(layers.batch_norm(x))
x = layers.fully_connected(x,10,activation_fn=None)
 x = tf.nn.softmax(layers.batch_norm(x))
return x
batch=200
x = tf.placeholder(tf.float32, shape=(None, 28, 28,1))
y = tf.placeholder(tf.int32,shape=(None,10))
models=[model(x)for _ in range(9)]
y_pred=tf.reduce_mean(models,0)
 loss=tf.reduce_mean(tf.losses.softmax_cross_entropy(y,logits=y_pred))
opt=tf.train.AdamOptimizer().minimize(loss)
 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_pred, 1))
X_test = test_df.values.reshape(( -1, 28, 28, 1)) / 255
y_hat = model.predict(X_test)
 y_class = y_hat.argmax(axis=-1)
 submission['Label'] = y_class<save_to_csv>
sess=tf.Session()
sess.run(tf.global_variables_initializer() )
submission.to_csv('output.csv', index=False)
 display(submission.tail() )<import_modules>
epoch=0
k=0
batch=200
for i in range(int(len(train_)/batch*15)) :
if k>=len(train_):
epoch+=1
k=0
x_=np.array(train_[k:k+batch])
 y_=np.array(labels_[k:k+batch])
 sess.run(opt,{x:x_.reshape(-1,28,28,1),y:y_})
 k+=batch
if i%72.5==0:
print("EPOCH: ",i/725 )
import torch
import torchvision
from torch import nn
from torchvision.transforms import transforms
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
from torch import nn
from torch import optim
import torch.nn.functional as F
import numpy as np
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets, transforms, models
from torchvision.datasets import ImageFolder
from torch.autograd import Variable<set_options>
csv=pd.read_csv(".. /input/test.csv")
 test=[list(csv.iloc[i])for i in range(len(csv)) ]
label=[]
for i in range(140):
label.extend(np.argmax(sess.run(y_pred,{x:np.reshape(test[i*200:(i+1)*200],(-1,28,28,1)) /255}),1))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 print(device )<load_from_csv>
csv=pd.DataFrame({'ImageId':range(1,28001),'Label':label})
 csv.to_csv("submission.csv",index=False )