Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
bert_config = transformers.RobertaConfig.from_json_file(BERT_CONFIG_PATH)
 bert_config.output_hidden_states = True
tokenizer = transformers.RobertaTokenizer(BERT_VOCAB_PATH,BERT_MERGES_PATH )<string_transform>
zf1 = zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/train.csv.zip')
 print(zf1.namelist())
 zf2 = zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/test.csv.zip')
 print(zf2.namelist())
 zf3 = zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/sample_submission.csv.zip')
 print(zf3.namelist() )
def _get_masks(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 return [1]*len(tokens)+ [0] *(max_seq_length - len(tokens))
def _get_segments(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 segments = []
current_segment_id = 0
for token in tokens:
segments.append(current_segment_id)
 if token == "[SEP]":
current_segment_id = 1
return segments + [0] *(max_seq_length - len(tokens))
def _get_segments2(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 segments = []
first_sep = True
current_segment_id = 0
for token in tokens:
segments.append(current_segment_id)
 if token == "[SEP]":
if first_sep:
first_sep = False
else:
current_segment_id = 1
return segments + [0] *(max_seq_length - len(tokens))
def _trim_input(title, question, answer, max_sequence_length,
t_max_len=30, q_max_len=238, a_max_len=238):
t = tokenizer.tokenize(title)
 q = tokenizer.tokenize(question)
 a = tokenizer.tokenize(answer)
 t_len = len(t)
 q_len = len(q)
 a_len = len(a)
 if(t_len+q_len+a_len+6)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
a_max_len = a_max_len + floor(( t_max_len - t_len)/2)
 q_max_len = q_max_len + ceil(( t_max_len - t_len)/2)
 else:
t_new_len = t_max_len
if a_max_len > a_len:
a_new_len = a_len
q_new_len = q_max_len +(a_max_len - a_len)
 elif q_max_len > q_len:
a_new_len = a_max_len +(q_max_len - q_len)
 q_new_len = q_len
else:
a_new_len = a_max_len
q_new_len = q_max_len
if t_new_len+a_new_len+q_new_len+6 != max_sequence_length:
raise ValueError("New sequence length should be %d, but is %d"
%(max_sequence_length,(t_new_len+a_new_len+q_new_len+6)))
 t = t[:t_new_len]
q_len_head = round(q_new_len/3)
 q_len_tail = -1*(q_new_len -q_len_head)
 a_len_head = round(a_new_len/3)
 a_len_tail = -1*(a_new_len -a_len_head)
 q = q[:q_len_head]+q[q_len_tail:]
a = a[:a_len_head]+a[a_len_tail:]
return t, q, a
def convert_lines(question, answer, max_sequence_length, tokenizer):
all_tokens = []
all_masks = []
all_segments = []
longer = 0
for q, a in tqdm(zip(question, answer)) :
tokens_q = tokenizer.tokenize(q)
 tokens_a = tokenizer.tokenize(a)
 q_len = len(tokens_q)
 a_len = len(tokens_a)
 if(q_len+a_len+3)> max_sequence_length:
longer += 1
new_q_len = q_len/(a_len+q_len)*(max_sequence_length-3)
 new_a_len = a_len/(q_len+a_len)*(max_sequence_length-3)
 new_q_len, new_a_len = int(np.ceil(new_q_len)) , int(np.floor(new_a_len))
if new_a_len+new_q_len+3 != max_sequence_length:
raise ValueError("too small %s" % str(new_a_len+new_q_len+3))
tokens_q = tokens_q[:new_q_len]
tokens_a = tokens_a[:new_a_len]
stoken = ["[CLS]"] + tokens_q + ["[SEP]"] + tokens_a + ["[SEP]"]
token_ids = tokenizer.encode(stoken, add_special_tokens=Ture)
 input_ids = token_ids + [0] *(max_sequence_length-len(token_ids))
attention_masks = _get_masks(stoken, max_sequence_length)
 input_segments = _get_segments2(stoken, max_sequence_length)
 all_tokens.append(input_ids)
 all_masks.append(attention_masks)
 all_segments.append(input_segments)
 print(longer)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<categorify>
zf1.extractall()
zf2.extractall()
zf3.extractall()
def convert_lines2(title, question, answer, max_sequence_length, tokenizer, t_max_len_seq=30, q_max_len_seq=238, a_max_len_seq=238):
all_tokens = []
all_masks = []
all_segments = []
longer = 0
for t, q, a in tqdm(zip(title, question, answer)) :
tokens_t, tokens_q, tokens_a = _trim_input(t, q, a, max_sequence_length=max_sequence_length)
 stoken = ["
"] + tokens_t + ["
"] + ["
"] + tokens_q + ["
"] + ["
"] + tokens_a + ["
"]
token_ids = tokenizer.convert_tokens_to_ids(stoken)
 input_ids = token_ids + [0] *(max_sequence_length-len(token_ids))
attention_masks = _get_masks(stoken, max_sequence_length)
 input_segments = _get_segments2(stoken, max_sequence_length)
 all_tokens.append(input_ids)
 all_masks.append(attention_masks)
 all_segments.append(input_segments)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<categorify>
train = pd.read_csv('train.csv')
 test = pd.read_csv('test.csv' )
test_tokens, test_masks, test_segments = convert_lines2(test_df["question_title"],
test_df["question_body"],
test_df["answer"],
max_sequence_length=512,
tokenizer=tokenizer )<categorify>
csv_file = open("train.csv", "r", encoding="ms932", errors="", newline="")
 f = csv.reader(csv_file, delimiter=",", doublequote=True, lineterminator="\r
", quotechar='"', skipinitialspace=True)
 header = next(f)
 print(header )
class QuestDataset_test(Dataset):
def __init__(self, token_ids, masks, segments, hosts, categories, handmade_features):
self.token_ids = token_ids
self.masks = masks
self.segments = segments
self.hosts = hosts
self.categories = categories
self.handmades = handmade_features
def __len__(self):
return self.token_ids.shape[0]
def __getitem__(self, idx):
token_id = self.token_ids[idx]
mask = self.masks[idx]
segment = self.segments[idx]
host = self.hosts[idx]
category = self.categories[idx]
handmade = self.handmades[idx]
return [token_id, mask, segment, host, category, handmade]<categorify>
pd.get_dummies(train['color'])
 pd.get_dummies(test['color'])
 training = pd.get_dummies(train, columns=['color'])
 training
testing = pd.get_dummies(test, columns=['color'])
 testing
def children(m):
return m if isinstance(m,(list, tuple)) else list(m.children())
 def set_trainable_attr(m, b):
m.trainable = b
for p in m.parameters() :
p.requires_grad = b
def apply_leaf(m, f):
c = children(m)
 if isinstance(m, torch.nn.Module):
f(m)
 if len(c)> 0:
for l in c:
apply_leaf(l, f)
 def set_trainable(l, b):
apply_leaf(l, lambda m: set_trainable_attr(m, b))<normalization>
from sklearn.neural_network import MLPClassifier
from sklearn import metrics
import joblib
class SpatialDropout(nn.Dropout2d):
def forward(self, x):
x = x.unsqueeze(2)
 x = x.permute(0, 3, 2, 1)
 x = super(SpatialDropout, self ).forward(x)
 x = x.permute(0, 3, 2, 1)
 x = x.squeeze(2)
 return x
class Attention(nn.Module):
def __init__(self, feature_dim, step_dim, bias=True, **kwargs):
super(Attention, self ).__init__(**kwargs)
 self.supports_masking = True
self.bias = bias
self.feature_dim = feature_dim
self.step_dim = step_dim
self.features_dim = 0
weight = torch.zeros(feature_dim, 1)
 nn.init.xavier_uniform_(weight)
 self.weight = nn.Parameter(weight)
 if bias:
self.b = nn.Parameter(torch.zeros(step_dim))
def forward(self, x, mask=None):
feature_dim = self.feature_dim
step_dim = self.step_dim
eij = torch.mm(
 x.contiguous().view(-1, feature_dim),
self.weight
).view(-1, step_dim)
 if self.bias:
eij = eij + self.b
eij = torch.tanh(eij)
 a = torch.exp(eij)
 if mask is not None:
a = a * mask
a = a / torch.sum(a, 1, keepdim=True)+ 1e-10
weighted_input = x * torch.unsqueeze(a, -1)
 return torch.sum(weighted_input, 1 )<choose_model_class>
hidden_layer_sizes=(100,)
 activation = 'relu'
solver = 'adam'
batch_size = 'auto'
alpha = 0.0001
random_state = 0
max_iter = 10000
x = ['bone_length', 'rotting_flesh', 'hair_length', 'has_soul']
train_X = training[x]
y1 = ['type']
train_y1 = training[y1]
clf = MLPClassifier(
 hidden_layer_sizes=hidden_layer_sizes,
activation=activation,
solver=solver,
batch_size=batch_size,
alpha=alpha,
random_state=random_state,
max_iter=max_iter, 
)
 clf.fit(train_X, train_y1)
 SAVE_TRAINED_DATA_PATH = 'train1.learn'
joblib.dump(clf, SAVE_TRAINED_DATA_PATH)
 clf1 = joblib.load(SAVE_TRAINED_DATA_PATH)
 test_X1 = testing[x]
predict_y1 = clf1.predict_proba(test_X1)
 predict_Y1 = clf1.predict(test_X1)
 print(predict_Y1)
 print("学習データの精度: {:.3f}".format(clf.score(train_X, train_y1)))
 print("テスト結果の精度: {:.3f}".format(clf.score(test_X1, predict_Y1)) )
<predict_on_test><EOS> 
sub = pd.read_csv('sample_submission.csv')
 sub['type'] = list(predict_Y1)
 sub.to_csv('sample_submission.csv', index=False )