Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
%matplotlib inline
np.random.seed(2)
 dense_regularizer = L1L2(l2=0.0001)
 sns.set(style='white', context='notebook', palette='deep' )
classifier_model.compile(optimizer=optimizer,
loss=loss,
metrics=metrics )<train_model>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
print(f'Training model with {tfhub_handle_encoder}')
 history = classifier_model.fit(x=train_ds, epochs=epochs,validation_data=valid_ds)
<save_model>
X_train = X_train / 255.0
test = test / 255.0
classifier_model.save("./model.h5" )<predict_on_test>
Y_train = to_categorical(Y_train, num_classes = 10 )
probs = classifier_model.predict(test_df["text"])
 threshold = 0.40
preds = np.where(probs[:,] > threshold, 1, 0 )<load_from_csv>
random_seed = 2
submission=pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv' )<prepare_output>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
submission["target"]=preds<save_to_csv>
def Model_1(x=None):
model = Sequential()
model.add(Conv2D(64,(5, 5), input_shape=(28,28,1), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(64,(5, 5), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(64,(5, 5), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(512,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(512,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(3, 3)))
 model.add(Flatten())
 model.add(Dense(10, activation='softmax', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
return model
model = Model_1()
model.summary()
submission.to_csv('submission.csv', index=False, header=True )<load_from_csv>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
df = pd.read_csv('.. /input/nlp-getting-started/train.csv',index_col=0)
 df_test = pd.read_csv('.. /input/nlp-getting-started/test.csv',index_col=0)
 temp = [(x,y)for x,y in zip(list(df['text']),list(df['target'])) ]
random.shuffle(temp)
 tweets = [t[0] for t in temp]
y = [t[1] for t in temp]
y = np.array(y ).astype('float32' )<count_values>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
print('Observations in training set')
 print(df['target'].count())
 print()
print('Label proportion in training set')
 print(df['target'].value_counts() /(sum(df['target'].value_counts())))
 print()
print('Observations in test set')
 print(df_test['text'].count() )<import_modules>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
import tensorflow as tf
from transformers import RobertaTokenizerFast, TFRobertaForSequenceClassification<load_pretrained>
epochs = 100
batch_size = 86
model_name = 'roberta-large'
roberta_tokenizer = RobertaTokenizerFast.from_pretrained(model_name)
 roberta_seq = TFRobertaForSequenceClassification.from_pretrained(model_name )<define_variables>
for t in tweets:
if '&' in re.sub(r'(&amp|&gt|&lt)','',t):
print(t )<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(X_train )
for t in tweets:
if any([x in t for x in [' btw ',' omg ',' lol ',' thx ']]):
print(t )<categorify>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
def process_tweets(tweets):
r = tweets
r = [re.sub(r'https?://t.co/\w+','',t)for t in r]
r = [re.sub('&amp;','&',t)for t in r]
r = [re.sub('&gt;','gt',t)for t in r]
r = [re.sub('&lt;','lt',t)for t in r]
return r
tweets = process_tweets(tweets )<string_transform>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
temp = roberta_tokenizer(tweets[:5],padding='max_length',max_length=50)
 temp.keys()<categorify>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen_MMA.csv",index=False )