用Keras几行代码实现基于bert的文本分类
x1_in = Input(shape=(max_x_len,), dtype='int32')x2_in = Input(shape=(max_x_len,), dtype='int32')x = bert_model([x1_in, x2_in])x4cls = Lambda(lambda x: x[:, 0])(x) #取出每个样本的第一个向量。因为bert输入的第一个向量是<cls>,这个向量可以用来文本分类。out = Dense(num_class, activation='softmax')(x4cls)model = Model([x1_in, x2_in], out)model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])model.summary()
软件获取方法
附
件
下
载
件
下
载