import numpy as np
from keras_bert import load_trained_model_from_checkpoint,Tokenizer
import codecs
import pandas as pd
from keras.layers import *
from keras.models import Model
import keras.backend as K
from keras.optimizers import Adam
import os
os.environ['CUDA_DEVICE_ORDER'] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
config_path = '/home/ycy/publish/bert_config.json'
checkpoint_path = '/home/ycy/publish/bert_model.ckpt'
dict_path = '/home/ycy/publish/vocab.txt'
token_dict = {}
maxlen = 100
with codecs.open(dict_path,'r','utf8') as reader:
for line in reader:
token = line.strip()
token_dict[token] = len(token_dict)
class OurTokenizer(Tokenizer): #重寫了類,因爲bert分詞的空格會有問題
def _tokenize(self, text):
R = []
for c in text:
if c in self._token_dict:
R.append(c)
elif self._is_space(c):
R.append('[inised1]')
else:
R.append('[UNK]')
return R
tokenizer = OurTokenizer(token_dict) #保持原本的書記長度
neg = pd.read_excel('neg.xls',header=None)
pos = pd.read_excel('pos.xls',header=None)
data = []
for d in neg[0]:
data.append((d,0))
for d in pos[0]:
data.append((d,1))
random_order = list(range(len(data)))
np.random.shuffle(random_order)
train_data = [data[j] for i,j in enumerate(random_order) if i %10 != 0]
valid_data = [data[j] for i,j in enumerate(random_order) if i %10 == 0]
def seq_padding(X,padding = 0):
L = [len(x) for x in X]
ML = max(L)
return np.array([np.concatenate([x,[padding] * (ML -len(x))]) if len(x) < ML else x for x in X]) #padding
class data_generator:
def __init__(self,data,batch_size = 32):
self.data = data
self.batch_size = batch_size
self.steps = len(self.data) // self.batch_size
if len(self.data) % self.batch_size != 0:
self.steps += 1
def __len__(self):
return self.steps
def __iter__(self):
while True:
print(len(self.data))
idx = list(range(len(self.data)))
np.random.shuffle(idx)
X1,X2,Y = [], [],[] #X2 QA任務
print(X2)
for i in idx:
d = self.data[i]
text = d[0][:maxlen]
x1,x2 = tokenizer.encode(first=text) #cixianliang
y = d[1]
X1.append(x1)
X2.append(x2)
Y.append([y])
if len(X1) == self.batch_size or i == idx[-1]:
X1 = seq_padding(X1)
X2 = seq_padding(X2)
Y = seq_padding(Y)
yield [X1,X2],Y
[X1,X2,Y] = [],[],[]
bert_model = load_trained_model_from_checkpoint(config_path,checkpoint_path) #加載bert向量
for l in bert_model.layers:
# print(l)
l.trainable = True
x1_in = Input(shape=(None,))
x2_in = Input(shape=(None,))
x = bert_model([x1_in,x2_in])
x = Lambda(lambda x : x[:,0])(x)
p = Dense(1,activation='sigmoid')(x) #分類層
model = Model([x1_in,x2_in],p)
model.compile(loss='binary_crossentropy',optimizer=Adam(1e-5),metrics=['acc'])
model.summary()
train_D = data_generator(train_data)
valid_data = data_generator(valid_data)
# model.fit_generator(train_D.__iter__(),
# steps_per_epoch=len(train_D),
# epochs=5,
# validation_data=valid_data.__iter__(),
# validation_steps=len(valid_data))
model.fit_generator(train_D.__iter__(),steps_per_epoch=len(train_D),epochs=5)
model.evaluate_generator(valid_data.__iter__())