Kaggle competition: notMNIST Competition
random.seed(1234)
 batch_size = 512
train_iter = torchtext.data.BucketIterator(dataset=train,
batch_size=batch_size,
shuffle=True,
sort=False )<choose_model_class>
xtr = np.load('.. /input/xtr.npy')
 xte = np.load('.. /input/xte.npy')
 ytr = np.load('.. /input/ytr.npy' )
def init_network(model, method='xavier', exclude='embedding', seed=123):
torch.manual_seed(seed)
 if torch.cuda.is_available() :
torch.cuda.manual_seed_all(seed)
 for name, w in model.named_parameters() :
if not exclude in name:
if 'weight' in name:
if method is 'xavier':
nn.init.xavier_normal_(w)
 elif method is 'kaiming':
nn.init.kaiming_normal_(w)
 else:
nn.init.normal_(w)
 elif 'bias' in name:
nn.init.constant_(w, 0.0)
 else:
pass
def print_model(model, ignore='embedding'):
total = 0
for name, w in model.named_parameters() :
if not ignore or ignore not in name:
total += w.nelement()
print('{} : {} {} parameters'.format(name, w.shape, w.nelement()))
print('-------'*4)
 print('Total {} parameters'.format(total))<choose_model_class>
xtr = np.expand_dims(xtr, -1 ).astype('float32')/ 255
xte = np.expand_dims(xte, -1 ).astype('float32')/ 255
ytr = np.array([[1 if ytr[i] == j else 0 for j in range(10)] for i in range(len(ytr)) ] )
text.fix_length = 70
model = TextCNN(text.vocab.vectors, padding_idx=text.vocab.stoi[text.pad_token], kernel_size=[1, 2, 3, 5], kernel_num=128, static=False, fixed_length=text.fix_length, dropout=0.1 ).cuda()
init_network(model)
 optimizer = optim.Adam(params=model.parameters() , lr=1e-3)
 loss_function = nn.BCEWithLogitsLoss()
print_model(model, ignore=None )<train_model>
model = Sequential()
model.add(Conv2D(16, 5, input_shape=(28, 28, 1), padding='same', activation='relu'))
model.add(MaxPool2D())
 model.add(Conv2D(32, 5, padding='same', activation='relu'))
model.add(MaxPool2D())
 model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'] )
alpha = training(3, model, loss_function, optimizer, train_iter )<predict_on_test>
datagen = ImageDataGenerator(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2 )
def predict(model, test_list):
pred = []
with torch.no_grad() :
for test_batch in test_list:
model.eval()
x = test_batch.text.cuda()
pred += torch.sigmoid(model.forward(x ).view(-1)).cpu().data.numpy().tolist()
return pred<create_dataframe>
train_idx = np.random.choice(range(len(xtr)) , size=int(np.floor(0.8 * len(xtr))), replace=False)
 val_idx = np.array([i for i in range(len(xtr)) if i not in train_idx] )
test_list = list(torchtext.data.BucketIterator(dataset=test,
batch_size=batch_size,
sort=False,
train=False))<predict_on_test>
xtrain = xtr[train_idx, :, :]
ytrain = ytr[train_idx, :]
xval = xtr[val_idx, :, :]
yval = ytr[val_idx, :]
preds = predict(model, test_list)
 sub = pd.DataFrame()
sub['qid'] = [qid.vocab.itos[j] for i in test_list for j in i.qid.view(-1 ).numpy() ]
sub['prediction'] =(preds > alpha ).astype(int)
 sub.head()<save_to_csv>
early_stop = EarlyStopping(patience=3, monitor='val_loss')
 batch_size = 128
epochs=50
model.fit_generator(datagen.flow(xtrain, ytrain, batch_size=batch_size),
steps_per_epoch = len(xtr)// batch_size,
epochs=epochs,
validation_data=(xval, yval),
callbacks=[early_stop] )
<define_variables><EOS> 
predictions = model.predict(xte)
 preds = predictions.argmax(axis=1)
 submission = pd.DataFrame({'id': range(len(xte)) , 'label': preds})
 submission.to_csv('my_submission.csv', index=None )