Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_x_and_y>
submit = True
x,y = next(train_generator )<create_dataframe>
( x_train, y_train),(x_test, y_test)= tf.keras.datasets.mnist.load_data()
x_train_norm = x_train/255.
 x_test_norm = x_test/255 .
valid_generator=train_datagen.flow_from_dataframe(
 dataframe=traindf,
directory=train_dir,
x_col="id",
y_col="breed",
subset="validation",
batch_size=BATCH_SIZE,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=image_size,
color_mode="rgb")
<prepare_x_and_y>
kaggle = pd.read_csv('.. /input/digit-recognizer/test.csv')
 kaggle_norm = np.asarray(kaggle/255.)
k_train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 k_labels = np.asarray(k_train['label'])
 k_train = k_train.drop(columns=['label'])
 k_train_norm = np.asarray(k_train/255.)
test_datagen=ImageDataGenerator(rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(
 dataframe=testdf,
directory=test_dir,
x_col="id",
y_col=None,
batch_size=BATCH_SIZE,
seed=42,
shuffle=False,
class_mode=None,
target_size=image_size,
color_mode="rgb" )<choose_model_class>
k_train_norm = k_train_norm.reshape(42000, 28, 28, 1)
 kaggle_norm = kaggle_norm.reshape(28000, 28, 28, 1)
 x_train_norm = x_train_norm.reshape(60000, 28, 28, 1)
 x_test_norm = x_test_norm.reshape(10000, 28, 28, 1 )
pretrained_model = tf.keras.applications.InceptionV3(
 weights='imagenet',
include_top=False ,
input_shape=shape 
)
 pretrained_model.trainable = False
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(120, activation='softmax')
 ] )<choose_model_class>
def train_model(model, train_features, train_label, epochs,
batch_size=None, validation_split=None):
history = model.fit(x=train_features, y=train_label,
batch_size=batch_size,
epochs=epochs, shuffle=True,
validation_split=validation_split,
verbose = 1)
 epochs = history.epoch
hist = pd.DataFrame(history.history)
 return epochs, hist
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 opt=tf.keras.optimizers.SGD(lr=1e-3, momentum=0.9)
 model.compile(optimizer = opt ,
loss="categorical_crossentropy",
metrics=["accuracy"])
 model.summary()<choose_model_class>
def create_X(learning_rate):

 model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, 4, activation='relu', input_shape=(28, 28, 1)))
 model.add(tf.keras.layers.MaxPooling2D(( 2,2)))
 model.add(tf.keras.layers.Conv2D(64, 2, activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(( 2,2)))
 model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(units=300, activation='relu'))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Dense(units=100, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(lr=learning_rate),
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
 return model
reduce = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',factor=0.2,patience=5, min_lr=0.001)
 early = tf.keras.callbacks.EarlyStopping(patience=2,
min_delta=0.001,
restore_best_weights=True )<train_model>
def getKaggles() :
kaggles = pd.DataFrame(columns=['ImageId','Label'])
 predicts = convoluterX.predict(kaggle_norm)
 for j in range(len(kaggle_norm)) :
probs = predicts[j]
max_id = np.argmax(probs)
 kaggles.at[j,'ImageId'] = j+1
kaggles.at[j,'Label'] = max_id
return kaggles
kaggles = getKaggles()
kaggles.to_csv('submission.csv', columns=["ImageId","Label"], index=False )