Kaggle competition: Digit Recognizer
count=0
para_embedding_mat=np.zeros(( vocab_size,300))
for word,i in tqdm(word_index.items()):
try:
vec=paragram_embed[word.lower() ]
para_embedding_mat[i]=vec
except KeyError:
count+=1
continue
print("Number of Out of Vocabulary",count )<train_model>
def get_available_gpus() :
local_device_protos = device_lib.list_local_devices()
print(local_device_protos)
 return [x.name for x in local_device_protos if x.device_type == 'GPU']
get_available_gpus()
with strategy.scope() :
para_model = get_model(para_embedding_mat)
 para_model.compile(loss=bin_loss, optimizer=Adam(lr=0.001), metrics=['accuracy'])
 para_history=para_model.fit(train_padded, train.target, batch_size=BATCH_SIZE, epochs=30, validation_data=(val_padded, val.target),callbacks=my_callbacks )<find_best_params>
print(os.listdir(".. /input"))
directory_train = ".. /input/train.csv"
directory_test = ".. /input/test.csv"
para_y_pre=para_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,(para_y_pre>thresh ).astype(int))):
best_score = metrics.f1_score(val.target,(para_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,(para_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>
test_data = pd.read_csv(directory_test)
 print(test_data.shape)
 test_data.head(5 )
threshold=best_thresh
para_y_test_pre=para_model.predict(test_padded, batch_size=BATCH_SIZE, verbose=1)
 para_y_test_pre=(para_y_test_pre>thresh ).astype(int )<drop_column>
train_data = pd.read_csv(directory_train)
 print(train_data.shape)
 train_data.head(5 )
del paragram_embed, para_embedding_mat, para_history, best_score, best_thresh
gc.collect()<set_options>
def format_label() :
return [train_data["label"],
train_data.drop(labels = ["label"],axis = 1)
 ]
y_train, x_train = format_label()
print(x_train.head(5), y_train.head(5))
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 normalization() :
return [
x_train / 255.0,
test_data / 255.0
]
def reshape() :
return [
x_train.values.reshape(-1,28, 28,1),
test_data.values.reshape(-1,28,28,1)
 ]
def tensorflow_encoding() :
return to_categorical(y_train, num_classes = 10)
 x_train, test_data = normalization()
print('x_train:',x_train.shape)
 print('test:',test_data.shape)
 print("------ Reshape ---------------")
 x_train, test_data = reshape()
print('x_train:',x_train.shape)
 print('test:',test_data.shape)
 y_train = tensorflow_encoding()
print(y_train )
%%time
wiki_embed = load_embed(wiki_path)
 print("\033[1mWiki Coverage: \033[0m]")
 oov_wiki = check_voc(vocabulary2, wiki_embed )<find_best_params>
def separating_data() :
return train_test_split(x_train,
y_train,
test_size = 0.1,
random_state = 42 
)
 x_train, x_validation, y_train, y_validation = separating_data()
count=0
wiki_embedding_mat=np.zeros(( vocab_size,300))
for word,i in tqdm(word_index.items()):
try:
vec=wiki_embed[word]
wiki_embedding_mat[i]=vec
except KeyError:
count+=1
continue
print("Number of Out of Vocabulary",count )<train_model>
activations = ['relu', 'softmax']
max_epochs = 2
model = Sequential()
model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0],
input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation = activations[0]))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = activations[0]))
model.add(Dropout(0.5))
model.add(Dense(10, activation = activations[1]))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=["accuracy"])
 generator = ImageDataGenerator()
batches = generator.flow(x_train,
y_train,
batch_size=64)
 batches_value = generator.flow(x_validation,
y_validation,
batch_size=64)
 history = model.fit_generator(generator=batches,
steps_per_epoch=batches.n,
epochs=max_epochs,
validation_data=batches_value,
validation_steps=batches_value.n )
<find_best_params><EOS> 
results = model.predict(test_data)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),
name = "ImageId"),
results],axis = 1)
 submission.to_csv("submission.csv",index=False )