Kaggle competition: Quora Insincere Questions Classification
from petal_helper import *<find_best_params>
train = pd.read_csv(path + "/train.csv")
 test = pd.read_csv(path + "/test.csv")
 sample = pd.read_csv(path + "/sample_submission.csv")
 print("training data: " + str(train.shape))
print("test data: " + str(test.shape))
print("sample data: " + str(sample.shape))
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<train_model>
max_words = 60000
tokenizer_class = Tokenizer(num_words=max_words, lower=True)
 tokenizer_class.fit_on_texts(train['question_text'])
 train_data = tokenizer_class.texts_to_sequences(train['question_text'])
 output_test_data = tokenizer_class.texts_to_sequences(test['question_text'])
 train_targets = train['target']
word_dict = tokenizer_class.word_index
number_of_words = min(max_words, len(word_dict))
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
 print("Validation:", ds_valid)
 print("Test:", ds_test )<train_model>
maxlen = 70
train_data = pad_sequences(train_data, maxlen=maxlen)
 output_test_data = pad_sequences(output_test_data, maxlen=maxlen )
print("Number of classes: {}".format(len(CLASSES)))
 print("First five classes, sorted alphabetically:")
 for name in sorted(CLASSES)[:5]:
print(name)
 print("Number of training images: {}".format(NUM_TRAINING_IMAGES))<train_model>
X_train, X_test, y_train, y_test = train_test_split(train_data, train_targets,
test_size = 0.1,
shuffle=False,
random_state = 42 )
print("Training data shapes:")
 for image, label in ds_train.take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Training data label examples:", label.numpy() )<data_type_conversions>
f = open(os.path.join('.. /input/embeddings/glove.840B.300d', 'glove.840B.300d.txt'))
embeddings_index = {}
for line in tqdm(f):
values = line.split(' ')
 word = values[0]
embedding = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = embedding
f.close()
print("Test data shapes:")
 for image, idnum in ds_test.take(3):
print(image.numpy().shape, idnum.numpy().shape)
 print("Test data IDs:", idnum.numpy().astype('U'))<define_variables>
all_embs = np.stack(embeddings_index.values())
 emb_mean = all_embs.mean()
emb_std = all_embs.std()
print(all_embs.shape)
 embedding_dim = all_embs.shape[1]
one_batch = next(iter(ds_train.unbatch().batch(20)))
 display_batch_of_images(one_batch )<install_modules>
embedding_matrix = np.random.normal(emb_mean,
emb_std, 
(number_of_words, embedding_dim))
pip install -q efficientnet<import_modules>
for word, i in word_dict.items() :
if i >= max_words:
continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
import efficientnet.tfkeras as efficientnet<choose_model_class>
input_class = Input(shape=(maxlen,))
x = Embedding(max_words, embedding_dim, weights=[embedding_matrix], trainable=False )(input_class)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = Bidirectional(LSTM(32, return_sequences=True))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=input_class, outputs=x)
 print(model.summary() )
with strategy.scope() :
pretrained_model = efficientnet.EfficientNetB7(weights='noisy-student',include_top=False,input_shape=[*IMAGE_SIZE, 3])
 pretrained_model.trainable=True
model = Sequential()
model.add(pretrained_model)
 model.add(GlobalAveragePooling2D())
 model.add(Dense(104, activation='softmax'))
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["sparse_categorical_accuracy"] )<choose_model_class>
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
 history = model.fit(X_train, y_train,
epochs=3,
batch_size=256,
validation_data=(X_test, y_test))
es = EarlyStopping(monitor='val_loss', mode='min', patience=6,
restore_best_weights=True, verbose=1)
 lr = ReduceLROnPlateau(monitor='val_loss',factor=0.2,patience=4,verbose=1)
 callbacks = [es, lr]<train_model>
pretrained_model_pred = model.predict([X_test], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(y_test,(pretrained_model_pred>thresh ).astype(int))))
<predict_on_test><EOS> 
test_predictions = model.predict([output_test_data], batch_size=256, verbose=1)
 test_predictions =(test_predictions>0.44 ).astype(int)
 output = pd.DataFrame({"qid":test["qid"].values})
 output['prediction'] = test_predictions
output.to_csv("submission.csv", index=False )