Kaggle competition: Digit Recognizer
train_data['no_words'] = train_data['text'].apply(lambda x:len(str(x ).split()))
lessThanThree = train_data[train_data['no_words']<=2]<filter>
np.random.seed(2)
lessThanThree[lessThanThree['sentiment']=='negative']<split>
train_orig=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_orig=pd.read_csv('.. /input/digit-recognizer/test.csv')
 sam_orig=pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
final_train=load_data_of_train()
final_test=load_data_of_test()
final_sample_submission=load_data_of_submission()
final_train<feature_engineering>
train_x , test_x = train_test_split(train_orig , test_size=0.1 , random_state=1)
 train_y , test_y = train_x.pop('label'), test_x.pop('label')
 train_x , test_x = train_x.values , test_x.values
test_orig=test_orig.values
train_x.shape
final_train['no_words'] = final_train['text'].apply(lambda x:len(str(x ).split()))
final_train = final_train[final_train['no_words']>=3]<define_variables>
train_x = train_x.reshape(( train_x.shape[0], 28, 28, 1))
test_x = test_x.reshape(( test_x.shape[0], 28, 28, 1))
test_orig = test_orig.reshape(( test_orig.shape[0], 28 , 28 , 1))
train_x, test_x, test_orig = train_x / 255.0, test_x / 255.0, test_orig/ 255.0
train_y = to_categorical(train_y)
 test_y = to_categorical(test_y)
 train_x.shape, test_x.shape, train_y.shape, test_y.shape
MAX_LEN = 96
PATH = ''
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file='.. /input/tf-roberta/vocab-roberta-base.json',
merges_file='.. /input/tf-roberta/merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}<data_type_conversions>
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32,(3, 3), activation="relu",
kernel_initializer="he_uniform", input_shape=(28, 28, 1)) ,
tf.keras.layers.Conv2D(32,(3, 3), activation="relu", kernel_initializer="he_uniform"),
tf.keras.layers.BatchNormalization(axis=-1,momentum=0.99),
tf.keras.layers.MaxPooling2D(( 2, 2)) ,
tf.keras.layers.Dropout (.25),
tf.keras.layers.Conv2D(64,(3, 3), activation="relu", kernel_initializer="he_uniform"),
tf.keras.layers.Conv2D(64,(3, 3), activation="relu", kernel_initializer="he_uniform"),
tf.keras.layers.BatchNormalization(axis=-1,momentum=0.99),
tf.keras.layers.MaxPooling2D(( 2, 2)) ,
tf.keras.layers.Dropout (.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.BatchNormalization(axis=-1,momentum=0.99),
tf.keras.layers.Dense(256, activation="relu",kernel_initializer="he_uniform"),
tf.keras.layers.Dense(128, activation="relu",kernel_initializer="he_uniform"),
tf.keras.layers.Dense(84, activation="relu",kernel_initializer="he_uniform"),
tf.keras.layers.Dense(10, activation="softmax")
 ])
 model.compile(optimizer=tf.keras.optimizers.Adam(
 learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False,
name='Adam' 
),
loss=tf.keras.losses.BinaryCrossentropy() ,
metrics=["accuracy"])
def definitions(Count,flag):
d = dict()
d['input_ids'] = np.ones(( Count,MAX_LEN),dtype='int32')
 d['attention_mask'] = np.zeros(( Count,MAX_LEN),dtype='int32')
 d['token_type_ids'] = np.zeros(( Count,MAX_LEN),dtype='int32')
 if(flag):
d['start_tokens'] = np.zeros(( Count,MAX_LEN),dtype='int32')
 d['end_tokens'] = np.zeros(( Count,MAX_LEN),dtype='int32')
 return d<define_variables>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(train_x )
count_row = final_train.shape[0]
data_definitions = definitions(count_row, True)
 input_ids = data_definitions['input_ids']
attention_mask = data_definitions['attention_mask']
token_type_ids = data_definitions['token_type_ids']
start_tokens = data_definitions['start_tokens']
end_tokens = data_definitions['end_tokens']<categorify>
reduce_lr = ReduceLROnPlateau(monitor='Test_loss', factor=0.2,
patience=3, min_lr=0.00001)
iterate=0
for k,col in final_train.iterrows() :
text1 = " "+" ".join(col['text'].split())
 text2 = " ".join(col['selected_text'].split())
 idx = text1.find(text2)
 chars = np.zeros(( len(text1)))
 chars[idx:idx+len(text2)]=1
if text1[idx-1]==' ': chars[idx-1] = 1
enc = tokenizer.encode(text1)
 offsets = []; idx=0
for t in enc.ids:
w = tokenizer.decode([t])
 offsets.append(( idx,idx+len(w)))
 idx += len(w)
 toks = []
for i,(a,b)in enumerate(offsets):
sm = np.sum(chars[a:b])
 if sm>0: toks.append(i)
 s_tok = sentiment_id[final_train.loc[k]['sentiment']]
input_ids[iterate][:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask[iterate][:len(enc.ids)+5] = 1
if len(toks)>0:
start_tokens[iterate][toks[0]+1] = 1
end_tokens[iterate][toks[-1]+1] = 1
iterate = iterate + 1<categorify>
history = model.fit(train_x, train_y,batch_size=256, epochs=50, validation_data=(test_x, test_y), verbose=0)
 score1 = model.evaluate(train_x, train_y, verbose=0)
 score = model.evaluate(test_x, test_y, verbose=0)
 print(f"Train_loss: {score1[0]} / Train accuracy: {score1[1]}")
 print(f"Test_loss: {score[0]} / Test accuracy: {score[1]}" )
count_row = final_test.shape[0]
data_definitions = definitions(count_row,False)
 input_ids_t = data_definitions['input_ids']
token_type_ids_t = data_definitions['token_type_ids']
attention_mask_t = data_definitions['attention_mask']
for k,col in final_test.iterrows() :
text1 = " "+" ".join(col['text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[col['sentiment']]
input_ids_t[k][:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k][:len(enc.ids)+5] = 1<load_pretrained>
results = model.predict(test_orig)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<load_pretrained><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )