目录 1、导入相关库 2、准备数据集 3、对问题和答案进行分词 4、构建模型 5、编译模型 6、训练模型 7、评估模型 8、使用模型进行预测
1、导入相关库
import numpy as np
from keras. preprocessing. text import Tokenizer
from keras. preprocessing. sequence import pad_sequences
from keras. models import Model
from keras. layers import Input, Dense, LSTM, Dropout
from transformers import TFBertModel, BertTokenizer
2、准备数据集
questions = [ '你好吗?' , '你叫什么名字?' , '你喜欢什么运动?' ]
answers = [ '我很好!' , '我叫小明。' , '我喜欢打篮球。' ]
3、对问题和答案进行分词
tokenizer = BertTokenizer. from_pretrained( 'bert-base-chinese' )
question_input_ids = tokenizer( questions, return_tensors= 'tf' , padding= True , truncation= True ) [ 'input_ids' ]
answer_input_ids = tokenizer( answers, return_tensors= 'tf' , padding= True , truncation= True ) [ 'input_ids' ]
4、构建模型
bert_model = TFBertModel. from_pretrained( 'bert-base-chinese' )
input_layer = Input( shape= ( None , ) , dtype= 'int32' )
bert_output = bert_model( input_layer) [ 0 ]
lstm_layer = LSTM( 100 ) ( bert_output)
dropout_layer = Dropout( 0.5 ) ( lstm_layer)
output_layer = Dense( len ( set ( answer_input_ids) ) , activation= 'softmax' ) ( dropout_layer)
model = Model( inputs= input_layer, outputs= output_layer)
5、编译模型
model. compile ( loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics= [ 'accuracy' ] )
6、训练模型
model. fit( question_input_ids, answer_input_ids, batch_size= 32 , epochs= 10 )
7、评估模型
score = model. evaluate( question_input_ids, answer_input_ids)
print ( 'Test loss:' , score[ 0 ] )
print ( 'Test accuracy:' , score[ 1 ] )
8、使用模型进行预测
def predict ( question) : question_input_id = tokenizer( question, return_tensors= 'tf' , padding= True , truncation= True ) [ 'input_ids' ] prediction = model. predict( question_input_id) return np. argmax( prediction) question = '你喜欢吃什么?'
answer_index = predict( question)
predicted_answer = tokenizer. decode( [ answer_index] )
print ( 'Predicted answer:' , predicted_answer)