Ejemplo n.º 1
0
#opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
#opt = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

#为每组实验记录F值得迭代
fpath = u'../data/fscore/Fscore-bils%s-ls%s-%s.txt' % (str(bils), str(ls),
                                                       label_mode)
mpath = u'../data/model/bils%s-ls%s-%s.h5' % (str(bils), str(ls), label_mode)
fpout = cs.open(fpath, 'w', 'utf-8')
for i in range(100):
    model.fit(xtrain, ytrain, batch_size=128, epochs=1)  #epochs练过程中数据将被“轮”多少次
    #使用开发集的f来决定是否保存模型,实体F最大的模型和关系F最大的模型分别保存
    y_predict = model.predict(xvaild, batch_size=128)
    pre_label = ypre2label(y_predict)
    pre_e, e_token = label2answer(pre_label, vaildtokens)
    print(u'第%s次训练开发集的结果为' % (str(i)))
    pe, re, fe = computeFe(gold_e_vaild, pre_e)
    fpout.write(u'第%s次开发集F值分别为:%f\n' % (str(i + 1), fe))
    if fe > maxf_e:
        maxf_e = fe
        save_model(mpath, model)
    #每轮迭代都额外考察在测试集上的数据,但测试集不参与模型的选择
    y_predict = model.predict(xtest, batch_size=128)
    pre_label = ypre2label(y_predict)
    pre_e, e_token = label2answer(pre_label, testtokens)
    print(u'第%s次训练测试集的结果为' % (str(i)))
    pe, re, fe = computeFe(gold_e, pre_e)
    fpout.write(u'第%s次测试集F值分别为:%f\n\n' % (str(i + 1), fe))
fpout.close()
Ejemplo n.º 2
0
#    cnn = Convolution1D(nb_filter=100, filter_length=3, activation='tanh')(cnn)
#    flattened = Flatten()(cnn)
#    dense = Dense(100, activation='tanh')(flattened)
#dense = Dense(50, activation='tanh')(dense)
predict = Dense(len(reptest.l2i_dic), activation='softmax')(bilstm)
model = Model(input=[word, distance_e1, distance_e2], output=predict)
opt = RMSprop(lr=0.001, rho=0.9, epsilon=1e-06)
model.compile(loss='categorical_crossentropy', optimizer=opt)
model.summary()

best_f = 0
for epoch in range(epoch_size):
    print('running the epoch:', (epoch + 1))
    model.fit([xtrain, pos1train, pos2train], ytrain, batch_size=256, epochs=1)
    answer_array_d = model.predict([xtest, pos1test, pos2test], batch_size=256)
    pre_label = ypre2label(answer_array_d)
    pre_r = [[] for i in range(len(goldr))]
    print(len(pre_r))
    for i in range(len(pre_label)):
        if pre_label[i] != 0:
            label = reptest.i2l_dic[pre_label[i]]  #类别
            sen_index = indexmap_test[i]  #句子索引
            ans = epostest[i] + [label]
            pre_r[sen_index].append(ans)
    p, r, f, f_label = computeFr(goldr, pre_r)
    if f > best_f:
        print('New Best F-score')
        best_f = f
        save_model('../data/model/bils%s.h5' % (str(bils)), model)
#    else:
#        print ('Lower than before')