Kaggle competition: What's Cooking?
<load_from_csv>
train = pd.read_json('.. /input/train.json', orient='columns')
 test = pd.read_json('.. /input/test.json', orient='columns')
 sample_submission = pd.read_csv(".. /input/sample_submission.csv" )
ROOT = '.. /input/google-quest-challenge/'
test_df = pd.read_csv(ROOT+'test.csv')
 train_df = pd.read_csv(ROOT+'train.csv' )<define_variables>
def sub_space(x):
temp_value = list()
for i in x:
temp_value.append(re.sub(r'[^0-9a-zA-Z]+','_',i.lower()))
return temp_value
train['ingredients_new'] = train['ingredients'].apply(sub_space)
 test['ingredients_new'] = test['ingredients'].apply(sub_space)
 def convert_list_to_sent(x):
return ' '.join(x)
 train['ingredient_sent'] = train['ingredients_new'].apply(convert_list_to_sent)
 test['ingredient_sent'] = test['ingredients_new'].apply(convert_list_to_sent)
 display(train.head())
 display(test.head())
target_cols = ['question_asker_intent_understanding', 'question_body_critical',
'question_conversational', 'question_expect_short_answer',
'question_fact_seeking', 'question_has_commonly_accepted_answer',
'question_interestingness_others', 'question_interestingness_self',
'question_multi_intent', 'question_not_really_a_question',
'question_opinion_seeking', 'question_type_choice',
'question_type_compare', 'question_type_consequence',
'question_type_definition', 'question_type_entity',
'question_type_instructions', 'question_type_procedure',
'question_type_reason_explanation', 'question_type_spelling',
'question_well_written', 'answer_helpful',
'answer_level_of_information', 'answer_plausible',
'answer_relevance', 'answer_satisfaction',
'answer_type_instructions', 'answer_type_procedure',
'answer_type_reason_explanation', 'answer_well_written']<import_modules>
X_train, X_val, y_train, y_val = train_test_split(train['ingredient_sent'], train['cuisine'], test_size=0.33, random_state=42 )
import torch
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
from torch.utils.data import RandomSampler, SequentialSampler
from pytorch_transformers import BertTokenizer
from sklearn.preprocessing import MinMaxScaler<load_pretrained>
tfidf_vect = TfidfVectorizer(lowercase=True,binary=True)
 X_train_tfidf = tfidf_vect.fit_transform(X_train)
 X_val_tfidf = tfidf_vect.transform(X_val)
 X_test_tfidf = tfidf_vect.transform(test['ingredient_sent'])
def read_data(raw_data_path):
test = pd.read_csv(raw_data_path, encoding='utf-8')
 targets = [-1] * len(test)
 sentence_a = test['question_title'] + test['question_body']
sentence_b = test['answer']
return targets, sentence_a, sentence_b
def save_pickle(data, file_path):

 if isinstance(file_path, Path):
file_path = str(file_path)
 with open(file_path, 'wb')as f:
pickle.dump(data, f)
 y, X_a, X_b = read_data(ROOT+'test.csv' )<concatenate>
lb = LabelEncoder()
y_train_encode = lb.fit_transform(y_train)
 y_val_encode = lb.transform(y_val)
 y_train_dummy = np_utils.to_categorical(y_train_encode)
 y_val_dummy = np_utils.to_categorical(y_val_encode )
data = []
for step,(data_x_a, data_x_b, data_y)in enumerate(zip(X_a, X_b, y)) :
data.append(( [data_x_a, data_x_b], data_y))<load_pretrained>
input_shape = X_train_tfidf.shape[1]
def model_structure1() :
mdl = Sequential()
mdl.add(Dense(512, init='glorot_uniform', activation='relu',input_shape=(input_shape,)))
 mdl.add(Dropout(0.5))
mdl.add(Dense(128, init='glorot_uniform', activation='relu'))
mdl.add(Dropout(0.5))
mdl.add(Dense(20, activation='softmax'))
mdl.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
 mdl.summary()
return mdl
tokenizer = BertTokenizer("/kaggle/input/bertpretrained/uncased_L-24_H-1024_A-16/uncased_L-24_H-1024_A-16/vocab.txt", True )<categorify>
print("Compile model...")
 estimator = KerasClassifier(build_fn=model_structure1, epochs=10, batch_size=128 )
class InputExample(object):
def __init__(self, guid, text_a, text_b=None, label=None):

 self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.label = label
class InputFeature(object):

 def __init__(self, input_ids, input_mask, segment_ids, label_id, input_len):
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
self.label_id = label_id
self.input_len = input_len
def create_examples(lines, example_type):

 examples = []
for i, line in enumerate(lines):
guid = '%s-%d' %(example_type, i)
 text_a = line[0][0]
text_b = line[0][1]
label = line[1]
example = InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label)
 examples.append(example)
 return examples
def truncate_seq_pair(tokens_a, tokens_b, max_length=512):
while True:
total_length = len(tokens_a)+ len(tokens_b)
 if total_length <= max_length:
break
if len(tokens_a)> len(tokens_b):
tokens_a.pop()
else:
tokens_b.pop()
def create_features(examples, max_seq_len=512):

 features = []
for ex_id, example in enumerate(examples):
tokenizer = BertTokenizer("/kaggle/input/bertpretrained/uncased_L-24_H-1024_A-16/uncased_L-24_H-1024_A-16/vocab.txt", True)
 tokens_a = tokenizer.tokenize(example.text_a)
 tokens_b = None
label_id = example.label
if example.text_b:
tokens_b = tokenizer.tokenize(example.text_b)
 truncate_seq_pair(tokens_a, tokens_b, max_length=max_seq_len - 3)
 else:
if len(tokens_a)> max_seq_len - 2:
tokens_a = tokens_a[:max_seq_len - 2]
tokens = ['[CLS]'] + tokens_a + ['[SEP]']
segment_ids = [0] * len(tokens)
 if tokens_b:
tokens += tokens_b + ['[SEP]']
segment_ids += [1] *(len(tokens_b)+ 1)
 input_ids = tokenizer.convert_tokens_to_ids(tokens)
 input_mask = [1] * len(input_ids)
 padding = [0] *(max_seq_len - len(input_ids))
input_len = len(input_ids)
 input_ids += padding
input_mask += padding
segment_ids += padding
assert len(input_ids)== max_seq_len
assert len(input_mask)== max_seq_len
assert len(segment_ids)== max_seq_len
feature = InputFeature(input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
label_id=label_id,
input_len=input_len)
 features.append(feature)
 return features<feature_engineering>
history = estimator.fit(X_train_tfidf.toarray() , y_train_dummy,\
validation_data=(X_val_tfidf.toarray() ,y_val_dummy))
test_examples = create_examples(data, 'test')
 test_features = create_features(test_examples )<create_dataframe>
mnb_train_prediction = estimator.predict_proba(X_train_tfidf.toarray())
 mnb_val_prediction = estimator.predict_proba(X_val_tfidf.toarray())
 mnb_tr_pred_value = estimator.predict(X_train_tfidf.toarray())
 mnb_val_pred_value = estimator.predict(X_val_tfidf.toarray())
 mnb_test_pred_value = estimator.predict(X_test_tfidf.toarray())
 print("=====")
 print("Log loss for TfIDFVectorizer features in MLP for Training set {}".format(log_loss(y_train_encode,mnb_train_prediction)))
 print("Log loss for TfIDFVectorizer features in MLP for validation set {}".format(log_loss(y_val_encode,mnb_val_prediction)))
 print("Accuracy for TfIDFVectorizer features in MLP for Training set {}".format(accuracy_score(y_train_encode,mnb_tr_pred_value)))
 print("Accuracy for TfIDFVectorizer features in MLP for validation set {}".format(accuracy_score(y_val_encode,mnb_val_pred_value)) )
def create_dataset(features, is_sorted=False):
if is_sorted:
logger.info("sorted data by th length of input")
 features = sorted(features, key=lambda x: x.input_len, reverse=True)
 all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
 all_input_mask = torch.tensor([f.input_mask for f in features], dtype=torch.long)
 all_segment_ids = torch.tensor([f.segment_ids for f in features], dtype=torch.long)
 all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.float)
 dataset = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
 return dataset<load_pretrained>
test_pred = list(lb.inverse_transform(mnb_test_pred_value))
print(test_pred )
test_dataset = create_dataset(test_features)
 test_sampler = SequentialSampler(test_dataset)
 test_dataloader = DataLoader(test_dataset,sampler=test_sampler,batch_size=32 )<choose_model_class>
result = pd.DataFrame({'id':test['id'],'cuisine':test_pred})
 result.head()
<categorify><EOS> 
result.to_csv('submission.csv',index=False )