Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<choose_model_class>
nltk.download('stopwords')
 stop_words = stopwords.words('english')
 nlp = spacy.load('en' )
PROJECT_ID = 'kaggle-bqml-course'
bucket_name = 'leosuky_kaggle_competitions'
region = 'us-central1'
storage_client = storage.Client(project=PROJECT_ID)
 automl_client = automl.AutoMlClient() 
<load_pretrained>
!pip install transformers
def upload_to_gcs(bucket_name, source_file_name, destination_blob_name):
"Uploads a file to the bucket.https://cloud.google.com/storage/docs/"
bucket = storage_client.get_bucket(bucket_name)
 blob = bucket.blob(destination_blob_name)
 blob.upload_from_filename(source_file_name)
<define_variables>
from transformers import RobertaModel, RobertaTokenizer
from transformers import RobertaForSequenceClassification, RobertaConfig, AdamW, get_linear_schedule_with_warmup
destination_path = 'uploads/kaggle_auto_ml/train.csv' 
<define_variables>
print(torch.cuda.is_available() )
dataset_name = 'disaster_tweets_nlp'
model_name = 'disaster_tweets_nlp'
client = automl_client
region = region
project_id = PROJECT_ID
bucket_name = bucket_name<drop_column>
data = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")[["text", "target"]]
test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 data.head()
amw = AutoMLWrapper(client=client,
project_id=PROJECT_ID,
bucket_name=bucket_name,
region='us-central1',
dataset_display_name=dataset_name,
model_display_name=model_name )<create_dataframe>
def clean_text(text):
text = re.sub(r"\x89Û_", "", text)
 text = re.sub(r"\x89ÛÒ", "", text)
 text = re.sub(r"\x89ÛÓ", "", text)
 text = re.sub(r"\x89ÛÏWhen", "When", text)
 text = re.sub(r"\x89ÛÏ", "", text)
 text = re.sub(r"China\x89Ûªs", "China's", text)
 text = re.sub(r"let\x89Ûªs", "let's", text)
 text = re.sub(r"\x89Û÷", "", text)
 text = re.sub(r"\x89Ûª", "", text)
 text = re.sub(r"\x89Û\x9d", "", text)
 text = re.sub(r"å_", "", text)
 text = re.sub(r"\x89Û¢", "", text)
 text = re.sub(r"\x89Û¢åÊ", "", text)
 text = re.sub(r"fromåÊwounds", "from wounds", text)
 text = re.sub(r"åÊ", "", text)
 text = re.sub(r"åÈ", "", text)
 text = re.sub(r"JapÌ_n", "Japan", text)
 text = re.sub(r"Ì©", "e", text)
 text = re.sub(r"å¨", "", text)
 text = re.sub(r"SuruÌ¤", "Suruc", text)
 text = re.sub(r"åÇ", "", text)
 text = re.sub(r"å£3million", "3 million", text)
 text = re.sub(r"åÀ", "", text)
 text = re.sub(r'https?://\S+|www\.\S+', '', text)
 text = re.sub(r'<.*?>', '', text)
 text = re.sub(r'[\d]+', ' ', text)
 return text
def create_dataset(project_id, dataset_name):
client = automl.AutoMlClient()
project_location = client.location_path(project_id, region)
 metadata = automl.types.TextSentimentDatasetMetadata(sentiment_max=1)
 dataset = automl.types.Dataset(display_name=dataset_name,
text_sentiment_dataset_metadata=metadata)
 result = client.create_dataset(project_location, dataset)
 print(result )<feature_engineering>
unique_targets = data.groupby('text' ).agg(unique_target=('target', pd.Series.nunique))
controversial_tweets = unique_targets[unique_targets['unique_target'] > 1].index
data = data[~data['text'].isin(controversial_tweets)]
data = data.drop_duplicates(subset='text', keep='first')
 data['text'] = data['text'].apply(clean_text)
 test['text'] = test['text'].apply(clean_text )
def import_dataset(project_id, dataset_id, path):
client = automl.AutoMlClient()
dataset_full_id = client.dataset_path(project_id, 'us-central1', dataset_id)
 input_uris = path.split(",")
 gcs_source = automl.types.GcsSource(input_uris=input_uris)
 input_config = automl.types.InputConfig(gcs_source=gcs_source)
 result = client.import_data(dataset_full_id, input_config)
 print("Processing import...")
 print("Data imported.{}".format(result))<define_variables>
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
 model = RobertaForSequenceClassification.from_pretrained('roberta-base' )
file_path = 'gs://leosuky_kaggle_competitions/uploads/kaggle_auto_ml/train.csv'
dataset_id = 'TST2133943162303938560' 
<choose_model_class>
def prepare_features(data_set, labels=None, max_seq_length = 100,
zero_pad = True, include_special_tokens = True):
input_ids = []
attention_masks = []
for sent in data_set:
encoded_dict = tokenizer.encode_plus(
 sent,
add_special_tokens = include_special_tokens,
max_length = max_seq_length,
pad_to_max_length = zero_pad,
return_attention_mask = True,
return_tensors = 'pt', 
)
 input_ids.append(encoded_dict['input_ids'])
 attention_masks.append(encoded_dict['attention_mask'])
 input_ids = torch.cat(input_ids, dim=0)
 attention_masks = torch.cat(attention_masks, dim=0)
 if labels is not None:
labels = torch.tensor(labels)
 return input_ids, attention_masks, labels
else:
return input_ids, attention_masks
def create_model(project_id, dataset_id, dataset_name):
client = automl.AutoMlClient()
project_location = client.location_path(project_id, 'us-central1')
 metadata = automl.types.TextSentimentModelMetadata()
model = automl.types.Model(display_name=dataset_name,
dataset_id=dataset_id,
text_sentiment_model_metadata=metadata)
 result = client.create_model(project_location, model)
 print(u"Training operation name: {}".format(result.operation.name))
print("Training started..." )<train_model>
train_input_ids, train_attention_masks, train_labels = prepare_features(
 train['text'], train['target'])
 val_input_ids, val_attention_masks, val_labels = prepare_features(
 val['text'], val['target'])
 test_input_ids, test_attention_masks = prepare_features(
 test['text'] )
print("model has already been created and trained!" )<train_model>
training_set = TensorDataset(train_input_ids, train_attention_masks, train_labels)
 validation_set = TensorDataset(val_input_ids, val_attention_masks, val_labels)
 test_set = TensorDataset(test_input_ids, test_attention_masks )
def model_evaluations(project_id, model_id):
client = automl.AutoMlClient()
full_model_id = client.model_path(project_id, 'us-central1', model_id)
 print('Model Evaluations:')
 for evaluation in client.list_model_evaluations(full_model_id, ""):
print('Model Evaluation Name: {}'.format(evaluation.name))
print('Model Annotation Spec Id: {}'.format(evaluation.annotation_spec_id))
print("Create Time:")
 print("\tseconds: {}".format(evaluation.create_time.seconds))
print("\tnanos: {}".format(evaluation.create_time.nanos / 1e9))
print('Evaluation Example Count: {}'.format(evaluation.evaluated_example_count))
print('Sentiment Analysis Evaluation Metrics: {}'.format(evaluation.text_sentiment_evaluation_metrics))
print('Translation Model Evaluation Metrics: {}'.format(evaluation.translation_evaluation_metrics)) 
<find_best_params>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model = model.to(device )
model_id = "TST4544356324388896768"
model_evaluations(project_id=project_id, model_id=model_id )<prepare_x_and_y>
torch.cuda.is_available()
if not amw.get_dataset_by_display_name(dataset_display_name=dataset_name):
print('dataset not found')
 amw.create_dataset()
amw.import_gcs_data(training_gcs_path)
 amw.dataset<train_model>
BATCH_SIZE = 32
LEARNING_RATE = 1e-05
EPSILON = 1e-8
MAX_EPOCHS = 10
if not amw.get_model_by_display_name() :
amw.train_model()
amw.deploy_model()
amw.model<find_best_params>
loading_params = {'batch_size': BATCH_SIZE,
'shuffle': True,
'drop_last': False,
'num_workers': 1}
training_loader = DataLoader(training_set, **loading_params)
 validation_loader = DataLoader(validation_set, **loading_params)
 test_loading_params = {'batch_size': BATCH_SIZE,
'shuffle': False,
'drop_last': False,
'num_workers': 1}
testing_loader = DataLoader(test_set, **test_loading_params )
amw.model_full_path<predict_on_test>
loss_function = nn.CrossEntropyLoss()
optimizer = AdamW(model.parameters() ,
lr = LEARNING_RATE,
eps = EPSILON 
)
 total_steps = len(training_loader)* MAX_EPOCHS
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = total_steps )
def make_predictions(project_id, model_id, content):
prediction_client = automl.PredictionServiceClient()
model_full_id = prediction_client.model_path(project_id, "us-central1", model_id)
 text_snippet = automl.types.TextSnippet(content=content, mime_type="text/plain")
 payload = automl.types.ExamplePayload(text_snippet=text_snippet)
 response = prediction_client.predict(model_full_id, payload)
 for annotation_payload in response.payload:
return annotation_payload.text_sentiment.sentiment 
<predict_on_test>
def format_time(elapsed):

 elapsed_rounded = int(round(( elapsed)))
 return str(datetime.timedelta(seconds=elapsed_rounded))
make_predictions(project_id=PROJECT_ID, model_id=model_id, content=corpus[1] )<predict_on_test>
def flat_accuracy(preds, labels):

 pred_flat = np.argmax(preds, axis=1)
 labels_flat = labels
return accuracy_score(labels_flat, pred_flat )
predictions = []
print('Starting predictions...')
 print('Predicting 1st batch.....')
 for document in corpus[:600]:
predictions.append(
 make_predictions(project_id=PROJECT_ID, model_id=model_id, content=document)
)
 print('Predicting 2nd batch.....')
 for document in corpus[600:1200]:
predictions.append(
 make_predictions(project_id=PROJECT_ID, model_id=model_id, content=document)
)
 print('Predicting 3rd batch.....')
 for document in corpus[1200:1800]:
predictions.append(
 make_predictions(project_id=PROJECT_ID, model_id=model_id, content=document)
)
 print('Predicting 4th batch.....')
 for document in corpus[1800:2400]:
predictions.append(
 make_predictions(project_id=PROJECT_ID, model_id=model_id, content=document)
)
 print('Predicting last batch.....')
 for document in corpus[2400:]:
predictions.append(
 make_predictions(project_id=PROJECT_ID, model_id=model_id, content=document)
)
 print('Done.' )<prepare_output>
for epoch in tqdm_notebook(range(MAX_EPOCHS)) :
t0 = time.time()
total_train_loss = 0
model.train()
print("EPOCH -- {} / {}".format(epoch, MAX_EPOCHS))
for step, batch in enumerate(training_loader):
if step % 30 == 0 and not step == 0:
elapsed = format_time(time.time() - t0)
 print(' Batch {} of {}.Elapsed: {:}'.format(step, len(training_loader), elapsed))
input_ids = batch[0].to(device ).to(torch.int64)
 input_masks = batch[1].to(device ).to(torch.int64)
 labels = batch[2].to(device ).to(torch.int64)
 model.zero_grad()
loss, logits = model(input_ids,
token_type_ids=None,
attention_mask=input_masks,
labels=labels)
 logits = logits.detach().cpu().numpy()
label_ids = labels.to('cpu' ).numpy()
total_train_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters() , 1.0)
 optimizer.step()
scheduler.step()
avg_train_loss = total_train_loss / len(training_loader)
 training_time = format_time(time.time() - t0)
 print('')
 print(' Average training loss: {0:.4f}'.format(avg_train_loss))
print(' Training epoch took: {:}'.format(training_time))
print('Running Validation')
 model.eval()
val_predictions = []
val_labels = []
for batch in validation_loader:
b_input_ids = batch[0].to(device)
 b_input_mask = batch[1].to(device)
 b_labels = batch[2].to(device)
 with torch.no_grad() :
loss, logits = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask,
labels=b_labels)
 val_predictions.append(logits.detach().cpu().numpy())
 val_labels.append(b_labels.to('cpu' ).numpy())
 val_predictions = [item for sublist in val_predictions for item in sublist]
val_labels = [item for sublist in val_labels for item in sublist]
val_accuracy = flat_accuracy(val_predictions, val_labels)
 print(' Accuracy: {0:.4f}'.format(val_accuracy))
sentiment_predictions = pd.DataFrame(predictions, columns=['target'])
 sentiment_predictions.head(4 )<concatenate>
model.eval()
predictions = []
for batch in testing_loader:
batch = tuple(t.to(device)for t in batch)
 input_ids, input_masks = batch
with torch.no_grad() :
logits = model(input_ids,
token_type_ids=None,
attention_mask=input_masks)[0]
logits = logits.detach().cpu().numpy()
predictions.append(logits )
submission_df = pd.concat([nlp_test['id'], sentiment_predictions['target']], axis=1)
 print(submission_df.shape)
 submission_df.head()<save_to_csv>
flat_predictions = [item for sublist in predictions for item in sublist]
targets = np.argmax(flat_predictions, axis=1 ).flatten()
submission_df.to_csv("submission.csv", index=False, header=True )<import_modules>
test['target'] = targets
import numpy as np
import pandas as pd
import spacy
from spacy.matcher import Matcher
from spacy.tokens import Span
from spacy import displacy<load_pretrained>
tokenizer.convert_tokens_to_ids(tokenizer.tokenize('dick'))
nlp=spacy.load("en_core_web_sm" )<load_from_csv>
submission = test[['id', 'target']].to_csv("submission_roberta.csv", index=False )
train=pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test=pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )<string_transform>
def preprocess(texts, allowed_postags=['NOUN', "ADJ", "VERB", "ADV", "DET"]):
texts_out = []
for text in texts:
lowered_text = text.lower()
doc = nlp(lowered_text)
 tokens = [token for token in doc if not(token.is_punct |
token.is_space |
token.is_digit)]
tokens = [token for token in tokens if token.is_alpha]
lemmas = [token.lemma_ for token in tokens if token.pos_ in allowed_postags]
words = [lemma for lemma in lemmas if lemma not in stop_words]
texts_out.append(words)
 return texts_out
stopwords = list(STOP_WORDS)
 punct=string.punctuation
def text_data_cleaning(sentence):
doc = nlp(sentence)
 tokens = []
for token in doc:
if token.lemma_ != "-PRON-":
temp = token.lemma_.lower().strip()
else:
temp = token.lower_
tokens.append(temp)
 cleaned_tokens = []
for token in tokens:
if token not in stopwords and token not in punct:
cleaned_tokens.append(token)
 return cleaned_tokens<import_modules>
data_pruned = data.copy(deep=True)
 data_pruned['text'] = data_pruned['text'].progress_apply(clean_text)
 data_pruned['text'] = data_pruned['text'].progress_apply(preprocess )
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix<choose_model_class>
test_pruned = test.copy(deep=True)
 test_pruned['text'] = test_pruned['text'].apply(clean_text)
 test_pruned['text'] = test_pruned['text'].apply(preprocess )
tfidf = TfidfVectorizer(tokenizer = text_data_cleaning)
 classifier = LinearSVC()<prepare_x_and_y>
all_text = pd.concat([data_pruned[['text']], test_pruned[['text']]], ignore_index=True )
x = train['text']
y = train['target']<split>
cv = CountVectorizer(ngram_range=(1,2))
tfidf_transformer = TfidfTransformer()
x = cv.fit_transform(all_text['text'])
 x_all_tfidf = tfidf_transformer.fit_transform(x )
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42 )<choose_model_class>
training_samples = data_pruned.shape[0]
X_train = x_all_tfidf[:training_samples,:]
X_test = x_all_tfidf[training_samples:,:]
clf = Pipeline([('tfidf', tfidf),('clf', classifier)] )<train_model>
y_train = data_pruned['target']
clf.fit(X_train,y_train )<predict_on_test>
mb_classifier = MB().fit(X_train, y_train )
y_pred = clf.predict(X_test )<compute_test_metric>
pred = mb_classifier.predict(X_train)
 c = classification_report(y_train,pred )
print(classification_report(y_test, y_pred))<predict_on_test>
skf = StratifiedKFold(n_splits=5, random_state=RANDOM_STATE)
 total_accuracy = []
total_precision = []
total_recall = []
for train_index, val_index in skf.split(X_train, y_train):
current_X_train = X_train[train_index]
current_y_train = y_train.iloc[train_index]
current_X_val = X_train[val_index]
current_y_val = y_train.iloc[val_index]
clf = clf_svm
clf.fit(current_X_train, current_y_train)
 current_predictions = clf.predict(current_X_val)
 total_accuracy.append(accuracy_score(current_y_val, current_predictions))
total_precision.append(precision_score(current_y_val, current_predictions))
total_recall.append(recall_score(current_y_val, current_predictions))
ave_accuracy = mean(total_accuracy)
 ave_precision = mean(total_precision)
 ave_recall = mean(total_recall)
 print("Average Accuracy: {:.4f}".format(ave_accuracy))
print("Average Precision: {:.4f}".format(ave_precision))
print("Average Recall: {:.4f}".format(ave_recall))
y_pred=clf.predict(test['text'] )<create_dataframe>
test_pruned['target'] = mb_classifier.predict(x_test )
sub_file=pd.DataFrame({'id':test['id'],'target':y_pred.round().astype(int)} )<load_from_url>
test_pruned[['id', 'target']].to_csv("submission_2.csv", index=False )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
clf_svm = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto' )
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization<categorify>
clf_svm.fit(x_train, y_train)
 pred = clf_svm.predict(x_train)
 print(classification_report(y_train,pred))
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
test_pruned['target'] = clf_svm.predict(x_test)
 test_pruned[['id', 'target']].to_csv("submission_svm.csv", index=False )
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=2e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
TRAIN_VAL_SPLIT = 0.8
train = data.sample(frac=TRAIN_VAL_SPLIT, random_state=RANDOM_STATE)
 val = data[~data.index.isin(train.index)]
X_train = train['text'].values
y_train = train['target'].values
X_val = val['text'].values
y_val = val['target'].values
X_test = test['text'].values
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<feature_engineering>
def preprocess(texts, allowed_postags=['NOUN', "ADJ", "VERB", "ADV", "PROPN", "DET"]):
texts_out = []
for text in texts:
lowered_text = text.lower()
lowered_text = re.sub(r'https?://\S+|www\.\S+', '', lowered_text)
 lowered_text = re.sub(r'<.*?>', '', lowered_text)
 doc = nlp(lowered_text)
 tokens = [token for token in doc if not(token.is_punct |
token.is_space |
token.is_digit)]
lemmas = [token.lemma_ for token in tokens if token.pos_ in allowed_postags]
words = [lemma for lemma in lemmas if lemma not in stop_words]
texts_out.append(words)
 return texts_out
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>
train_corpus = preprocess(X_train)
 val_corpus = preprocess(X_val)
 test_corpus = preprocess(X_test )
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>
corpus = train_corpus + val_corpus + test_corpus
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
batch_size=16
)<predict_on_test>
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
 train_sequences = tokenizer.texts_to_sequences(train_corpus)
 val_sequences = tokenizer.texts_to_sequences(val_corpus)
 test_sequences = tokenizer.texts_to_sequences(test_corpus)
 word_index = tokenizer.word_index
train_max_length = max([len(x)for x in train_sequences])
 val_max_length = max([len(x)for x in val_sequences])
 test_max_length = max([len(x)for x in test_sequences])
 max_length = max(train_max_length, val_max_length, test_max_length)
 X_train_pad = pad_sequences(train_sequences, maxlen=max_length, padding="post")
 X_val_pad = pad_sequences(val_sequences, maxlen=max_length, padding="post")
 X_test_pad = pad_sequences(test_sequences, maxlen=max_length, padding="post")
 vocab = np.array(list(tokenizer.word_index.keys()))
vocab_size = len(tokenizer.word_index)+ 1
test_pred = model.predict(test_input )<save_to_csv>
EMBEDDING_DIM = 30
submission=pd.DataFrame()
submission['id']=test['id']
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
model_tuned = Sequential()
model_tuned.add(Embedding(vocab_size, EMBEDDING_DIM, input_length=max_length))
model_tuned.add(GRU(units=30, dropout=0.2, recurrent_dropout=0.2, return_sequences=True))
model_tuned.add(GRU(units=30, dropout=0.2, recurrent_dropout=0.2, return_sequences=True))
model_tuned.add(GRU(units=30, dropout=0.2, recurrent_dropout=0.2,
kernel_regularizer=l1_l2(0.01, 0.01), recurrent_regularizer=l1_l2(0.01, 0.01)))
 model_tuned.add(Dense(1, activation='sigmoid'))
model_tuned.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'] )
import numpy as np
import pandas as pd
import time
from datetime import datetime<load_from_csv>
history = model_tuned.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_val_pad, y_val), verbose=2 )
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )<import_modules>
y_pred_tuned = model_tuned.predict(X_test_pad )
from google.cloud import storage, automl_v1beta1 as automl
from google.api_core.gapic_v1.client_info import ClientInfo
from automlwrapper import AutoMLWrapper<choose_model_class>
y_pred_binary = list(map(lambda x: 1 if x >= 0.5 else 0, y_pred_tuned))
PROJECT_ID = 'kaggle-nlp-wdt'
BUCKET_NAME = 'kaggle-nlp-wdt-lcm'
region = 'us-central1'
storage_client = storage.Client(project=PROJECT_ID)
 client = automl.AutoMlClient(client_info=ClientInfo())
 print(f'Starting AutoML notebook at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}' )<define_variables>
test['target'] = y_pred_binary
VERSION = 'V19'
BUCKET_PATH = 'preprocessing/'+VERSION+'/'
FILE_NAME = 'train_cleaned'+'_'+VERSION
training_gcs_path = BUCKET_PATH+FILE_NAME+'.csv'
dataset_display_name = FILE_NAME
model_display_name = 'model_'+FILE_NAME<save_to_csv>
test[['id', 'target']].to_csv("submission_rnn.csv", index=False )
train.loc[:,['text','target']].drop_duplicates() \ 
.to_csv('train.csv', index=False, header=False )<prepare_output>
def ConvNet(max_sequence_length, num_words, embedding_dim, labels_index):
embedding_layer = Embedding(num_words,
embedding_dim,
input_length=max_sequence_length)
 sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
 embedded_sequences = embedding_layer(sequence_input)
 convs = []
filter_sizes = [3,4,5,6]
for filter_size in filter_sizes:
l_conv = Conv1D(filters=50,
kernel_size=filter_size,
activation='relu' )(embedded_sequences)
 l_conv = Dropout(0.2 )(l_conv)
 l_pool = GlobalMaxPooling1D()(l_conv)
 convs.append(l_pool)
 l_merge = concatenate(convs, axis=1)
 x = Dense(32, activation='relu' )(l_merge)
 x = Dropout(0.2 )(x)
 preds = Dense(labels_index, activation='sigmoid' )(x)
 model = Model(sequence_input, preds)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['acc'])
 model.summary()
return model
bucket = storage.Bucket(storage_client, name=BUCKET_NAME)
 if not bucket.exists() :
bucket.create(location=BUCKET_REGION )<load_pretrained>
model = ConvNet(max_length, vocab_size, EMBEDDING_DIM, 1 )
def upload_blob(bucket_name, source_file_name, destination_blob_name):

 bucket = storage_client.get_bucket(bucket_name)
 blob = bucket.blob(destination_blob_name)
 blob.upload_from_filename(source_file_name)
 print('File {} uploaded to {}'.format(
 source_file_name,
'gs://' + bucket_name + '/' + destination_blob_name))
def download_to_kaggle(bucket_name,destination_directory,file_name,prefix=None):

 os.makedirs(destination_directory, exist_ok = True)
 full_file_path = os.path.join(destination_directory, file_name)
 blobs = storage_client.list_blobs(bucket_name,prefix=prefix)
 for blob in blobs:
blob.download_to_filename(full_file_path )<load_from_csv>
hist = model.fit(X_train_pad,
y_train,
epochs=25,
batch_size=128,
validation_data=(X_val_pad, y_val),
verbose=2 )
upload_blob(BUCKET_NAME, 'train.csv', training_gcs_path )<choose_model_class>
y_pred = model.predict(X_test_pad)
 y_pred_binary = list(map(lambda x: 1 if x >= 0.5 else 0, y_pred))
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)
<prepare_x_and_y>
test['target'] = y_pred_binary
test[['id', 'target']].to_csv("submission_cnn.csv", index=False )
<train_model><EOS> 
data[data['target'] == 0].values