Kaggle competition: Time Series Classification
<SOS> metric: auc Kaggle data source: time-series-classification<save_to_csv>
%matplotlib inline
RANDOM_SEED = 42
r_np = np.random.seed(RANDOM_SEED)
 r_tf = tf.set_random_seed(RANDOM_SEED)
 np.random.seed(RANDOM_SEED)
 tf.set_random_seed(RANDOM_SEED )
nlp_train_df[['text_tokens','target']].to_csv('train.csv', index=False, header=False )<define_variables>
training_gcs_path = 'uploads/kaggle_getstarted/full_train.csv' 
<choose_model_class>
')
 Time taken: %i hours %i minutes and %s seconds.' %
amw = AutoMLWrapper(client=client,
project_id=PROJECT_ID,
bucket_name=bucket_name,
region='us-central1',
dataset_display_name=dataset_display_name,
model_display_name=model_display_name )<train_model>
batch_size = 16
num_classes = 10
epochs = 100
patience = 15
data_augmentation = False
num_predictions = 20
save_dir = os.path.join(os.getcwd() , 'saved_models')
 model_name = 'keras_cifar10_trained_model.h5' 
(x_train, y_train),(x_test, y_test)= cifar10.load_data()
print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples')
 y_train = keras.utils.to_categorical(y_train, num_classes)
 y_test = keras.utils.to_categorical(y_test, num_classes)
 model = Sequential()
model.add(Conv2D(32,(2, 2), padding = 'valid', input_shape = x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32,(2, 2)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Dropout(0.15))
model.add(Conv2D(64,(2, 2), padding = 'same'))
model.add(Activation('relu'))
model.add(Conv2D(64,(2, 2)))
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255
x_test /= 255
callbacks = [ReduceLROnPlateau(monitor = 'val_acc', factor = 0.15, patience = 7, verbose = 2, mode = 'auto'), ReduceLROnPlateau(monitor = 'val_acc', patience = 3, verbose = 2, factor = 0.5, min_lr = 0.00001), EarlyStopping(monitor = 'val_acc', patience = patience, mode = 'max', verbose = 0), ModelCheckpoint('best_model.h5', monitor = 'val_acc', mode = 'max', save_best_only = True, verbose = 0)]
if not data_augmentation:
print('Not using data augmentation.')
 model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, validation_data =(x_test, y_test), shuffle = True, callbacks = callbacks, verbose = 2)
 else:
print('Using real-time data augmentation.')
 datagen = ImageDataGenerator(
 featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = True,
samplewise_std_normalization = False,
zca_whitening = False,
zca_epsilon = 1e-06,
rotation_range = 0,
width_shift_range = 0.1,
height_shift_range = 0.1,
shear_range = 0.15,
zoom_range = 0.2,
channel_shift_range = 0.,
fill_mode = 'nearest',
cval = 0.,
horizontal_flip = True,
vertical_flip = True,
rescale = 0.2,
preprocessing_function = None,
data_format = None,
validation_split = 0.0)
 datagen.fit(x_train)
 model = model.fit_generator(datagen.flow(x_train, y_train, batch_size = batch_size),
steps_per_epoch = x_train.shape[0] * 2, epochs = epochs, validation_data =(x_test, y_test), workers = -1, verbose = 1, callbacks = callbacks)
 del model
model = load_model('best_model.h5')
 if not os.path.isdir(save_dir):
os.makedirs(save_dir)
 model_path = os.path.join(save_dir, model_name)
 model.save(model_path)
 print('Saved trained model at %s ' % model_path)
 scores = model.evaluate(x_test, y_test, verbose = 0)
 print('Test loss:', scores[0])
 print('Test accuracy:', scores[1] )
if not amw.get_dataset_by_display_name(dataset_display_name):
print('dataset not found')
 amw.create_dataset()
amw.import_gcs_data(training_gcs_path)
 amw.dataset<train_model>
res = pd.DataFrame({'loss': scores[0], 'score': scores[1]}, index = list('0'))
res.to_csv('result.csv')
 res.head()
if not amw.get_model_by_display_name() :
amw.train_model()
amw.model<find_best_params>
Fold %d - Run %d
' %(( i + 1),(run + 1)))
amw.model_full_path<choose_model_class>
Fold %d Run %d Log-loss: %.5f' %(( i + 1),(run + 1), LL_run))
Fold %d Log-loss: %.5f' %(( i + 1), LL))
<concatenate><EOS> 
Average Log-loss: %.5f' %(cv_LL/folds))
Average AUC: %.5f' %(cv_AUC/folds))
Average normalized gini: %.5f' %(cv_gini/folds))