Kaggle competition: Digit Recognizer
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<categorify>
%matplotlib inline
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values<train_model>
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 train_data.head()
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)<predict_on_test>
test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 test_data.head()
model.load_weights('model.h5')
 test_pred = model.predict(test_input )<save_to_csv>
from sklearn.model_selection import train_test_split
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
from sklearn.model_selection import train_test_split
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 print(train.shape, test.shape)
 train.sample(10, random_state=26 )<feature_engineering>
y = train_data['label']
def preprocess(df):
df_new = df.copy(deep=True)
 df_new['text'] = df.apply(lambda row: re.sub('@[A-z0-9]', '', row['text'] ).lower() , axis=1)
 df_new['text_w_kword'] = df_new.apply(lambda row: 'keyword: ' + str(row['keyword'])+ '.'+ str(row['text']), axis=1)
 return df_new
train_prep = preprocess(train)
 test_prep = preprocess(test)
 train_prep[40:60]<split>
df_train = train_data.drop(['label'], axis=1 )
X_train, X_valid, y_train, y_valid = train_test_split(train_prep['text_w_kword'],
train_prep['target'],
test_size=0.1,
random_state=1 )<load_pretrained>
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
tokenizer = DistilBertTokenizerFast.from_pretrained('/kaggle/input/huggingface-bert-variants/distilbert-base-uncased/distilbert-base-uncased/')
 train_encodings = tokenizer(list(X_train), truncation=True, padding='max_length', max_length=100)
 valid_encodings = tokenizer(list(X_valid), truncation=True, padding='max_length', max_length=100 )<data_type_conversions>
df_train = df_train/255
df_train[0:5]
train_dataset = tf.data.Dataset.from_tensor_slices((
dict(train_encodings),
y_train.values.astype('float32' ).reshape(( -1,1)) 
))
valid_dataset = tf.data.Dataset.from_tensor_slices((
dict(valid_encodings),
y_valid.values.astype('float32' ).reshape(( -1,1)) 
))
train_dataset<choose_model_class>
y = to_categorical(y, num_classes = 10)
 y.shape
es = EarlyStopping(monitor='val_loss',
verbose=1,
patience=4,
restore_best_weights=True )<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(df_train, y, test_size = 0.1, random_state=42, stratify=y )
batch_size = 64
num_epochs = 15
num_train_steps =(X_train.shape[0] // batch_size)* num_epochs
lr_scheduler = PolynomialDecay(
 initial_learning_rate=5e-5,
end_learning_rate=1e-5,
decay_steps=num_train_steps 
)
 new_opt = Adam(learning_rate=lr_scheduler )<compute_test_metric>
input_shape =(28,28,1)
 X_input = Input(input_shape)
 x = Conv2D(64,(3,3),strides=(1,1),name='layer_conv1',padding='same' )(X_input)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool1' )(x)
 x = Conv2D(32,(3,3),strides=(1,1),name='layer_conv2',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool2' )(x)
 x = Conv2D(32,(3,3),strides=(1,1),name='conv3',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2), name='maxPool3' )(x)
 x = Flatten()(x)
 x = Dense(64,activation ='relu',name='fc0' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(32,activation ='relu',name='fc1' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(10,activation ='softmax',name='fc2' )(x)
 conv_model = Model(inputs=X_input, outputs=x, name='Predict')
 conv_model.summary()
def f1_score(true, pred):
ground_positives = K.sum(true, axis=0)+ K.epsilon()
pred_positives = K.sum(pred, axis=0)+ K.epsilon()
true_positives = K.sum(true * pred, axis=0)+ K.epsilon()
precision = true_positives / pred_positives
recall = true_positives / ground_positives
f1 = 2 *(precision * recall)/(precision + recall + K.epsilon())
 return f1<train_model>
conv_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
 conv_model.fit(X_train, y_train, epochs=10, batch_size=100, validation_data=(X_test,y_test))
model = TFDistilBertForSequenceClassification.from_pretrained('/kaggle/input/huggingface-bert-variants/distilbert-base-uncased/distilbert-base-uncased/',
num_labels=2)
 model.compile(
 optimizer=new_opt,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
)
 history = model.fit(train_dataset.batch(batch_size),
validation_data=valid_dataset.batch(batch_size),
epochs=num_epochs,
callbacks=[es] )<categorify>
sgd = SGD(lr=0.0005, momentum=0.5, decay=0.0, nesterov=False)
 conv_model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy'])
 conv_model.fit(X_train, y_train, epochs=30, validation_data=(X_test, y_test))
test_encodings = tokenizer(list(test_prep['text_w_kword']), truncation=True, padding='max_length', max_length=100)
 test_dataset = tf.data.Dataset.from_tensor_slices((
dict(test_encodings)
))<predict_on_test>
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 )
test_preds = model.predict(test_dataset.batch(1))<prepare_output>
hist = conv_model.fit_generator(datagen.flow(X_train,y_train),
epochs = 1, validation_data =(X_test,y_test),
verbose = 2)
class_preds = np.argmax(test_preds.logits, axis=1)
 class_preds<predict_on_test>
data_generator = ImageDataGenerator(rescale=1./255,
rotation_range=1,
zoom_range=0.1,
width_shift_range=0.05,
height_shift_range=0.05)
 data_generator.fit(X_train)
valid_preds = model.predict(valid_dataset.batch(batch_size))<prepare_output>
df_test = test_data/255
df_test[0:5]
valid_class_preds = np.argmax(valid_preds.logits, axis=1 )<compute_test_metric>
results = conv_model.predict(df_test)
 results = np.argmax(results,axis=1)
 my_submission = pd.DataFrame({'ImageId': list(range(1, len(results)+1)) , 'Label': results})
 my_submission.to_csv('submission.csv', index=False )
<create_dataframe><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False)
 print("Your submission was successfully saved!")