Kaggle competition: Digit Recognizer
glove_path = './glove.840B.300d/glove.840B.300d.txt'
paragram_path = './paragram_300_sl999/paragram_300_sl999.txt'
wiki_path = './wiki-news-300d-1M/wiki-news-300d-1M.vec'<load_pretrained>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
%%time
glove_embed = load_embed(glove_path)
 print("\033[1mGlove Coverage: \033[0m]")
 oov_glove = check_voc(vocabulary2, glove_embed )<set_options>
train=pd.read_csv(".. /input/digit-recognizer/train.csv")
 test=pd.read_csv(".. /input/digit-recognizer/test.csv")
 label=train["label"]
train=train.drop(labels = ["label"],axis = 1)
 train=train/255
test=test/255
strategy = None
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
strategy = tf.distribute.TPUStrategy(tpu)
 print('Use TPU')
 except ValueError:
if len(tf.config.list_physical_devices('GPU')) > 0:
strategy = tf.distribute.MirroredStrategy()
print('Use GPU')
 else:
strategy = tf.distribute.get_strategy()
print('Use CPU' )<count_unique_values>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1
)
count=0
glove_embedding_mat=np.zeros(( vocab_size,300))
for word,i in tqdm(word_index.items()):
try:
vec=glove_embed[word]
glove_embedding_mat[i]=vec
except KeyError:
count+=1
continue
print("Number of Out of Vocabulary",count )<train_model>
train=torch.tensor(train, dtype=torch.float)
 label=torch.tensor(label.values, dtype=torch.float)
 batch_size = 32
train_data = torch.utils.data.TensorDataset(train,label)
 train_iter = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True )
with strategy.scope() :
glove_model = get_model(glove_embedding_mat)
 glove_model.compile(loss=bin_loss, optimizer=Adam(lr=0.001), metrics=['accuracy'])
 glove_history=glove_model.fit(train_padded, train.target, batch_size=BATCH_SIZE, epochs=30, validation_data=(val_padded, val.target),callbacks=my_callbacks )<find_best_params>
class Residual(nn.Module):
def __init__(self, in_channels, out_channels, use_1x1conv=False, stride=1):
super(Residual, self ).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride)
 self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
 if use_1x1conv:
self.conv3 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride)
 else:
self.conv3 = None
self.bn1 = nn.BatchNorm2d(out_channels)
 self.bn2 = nn.BatchNorm2d(out_channels)
 def forward(self, X):
Y = F.relu(self.bn1(self.conv1(X)))
 Y = self.bn2(self.conv2(Y))
if self.conv3:
X = self.conv3(X)
 return F.relu(Y + X )
glove_y_pre=glove_model.predict(val_padded, verbose=1)
 best_score = 0
best_thresh = 0
for thresh in np.arange(0.1,0.5,0.01):
if(best_score < metrics.f1_score(val.target,(glove_y_pre>thresh ).astype(int))):
best_score = metrics.f1_score(val.target,(glove_y_pre>thresh))
best_thresh = round(thresh, 2)
 print("threshold {0:2.2f} f1 score:{1:2.3f}".format(thresh,metrics.f1_score(val.target,(glove_y_pre>thresh ).astype(int))))
print("\033[1mBest result {0:2.3f} in thresh_hold {1:2.2f}\033[0m".format(best_score, best_thresh))<predict_on_test>
def resnet_block(in_channels, out_channels, num_residuals, first_block=False):
if first_block:
assert in_channels == out_channels
blk = []
for i in range(num_residuals):
if i == 0 and not first_block:
blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2))
else:
blk.append(Residual(out_channels, out_channels))
return nn.Sequential(*blk )
threshold=best_thresh
glove_y_test_pre=glove_model.predict(test_padded, batch_size=BATCH_SIZE, verbose=1)
 glove_y_test_pre=(glove_y_test_pre>thresh ).astype(int )<drop_column>
net = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=7, stride=2, padding=3),
nn.BatchNorm2d(32),
nn.ReLU() ,
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
resnet_block(32, 32, 2, first_block=True),
resnet_block(32, 64, 2),
resnet_block(64, 128, 2),
resnet_block(128, 256, 2),
nn.AdaptiveAvgPool2d(( 1,1)) ,
nn.Flatten() ,
nn.Linear(256,10),
)
del glove_embed, glove_embedding_mat, glove_history, best_score, best_thresh
gc.collect()<set_options>
lr=0.0015
decay=0
num_epochs=30
loss=nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters() , lr=lr )
strategy = None
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
strategy = tf.distribute.TPUStrategy(tpu)
 print('Use TPU')
 except ValueError:
if len(tf.config.list_physical_devices('GPU')) > 0:
strategy = tf.distribute.MirroredStrategy()
print('Use GPU')
 else:
strategy = tf.distribute.get_strategy()
print('Use CPU' )<load_pretrained>
def train_net(net, train_iter,loss, num_epochs, lr, optimizer):
for epochs in range(num_epochs):
for x,y in train_iter:
if(epochs % 2)==0:
b=np.array([datagen.flow(x[i:i+1] ).next() [0] for i in range(x.shape[0])])
 x=torch.tensor(b, dtype=torch.float)
 y_hat=net(x)
 l=loss(y_hat,y.long())
 optimizer.zero_grad()
l.backward()
optimizer.step()
print("epochs:"+str(epochs)+" loss:"+str(l))
train_net(net,train_iter,loss, num_epochs, lr, optimizer )
%%time
paragram_embed = load_embed(paragram_path)
 print("\033[1mParagram Coverage: \033[0m]")
 oov_paragram = check_voc(vocabulary2, paragram_embed )<find_best_params>
test=torch.tensor(test, dtype=torch.float)
 sub=net(test)
 sub=sub.argmax(dim=1)
 sub=sub.numpy()
sub.reshape(-1,1)
 submission=pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 submission["Label"]=sub
submission.to_csv('submission.csv', index=False )