Пример #1
0
def attention_3d_block(input_shapeR, input_shapeQ):
    relation_maxlen = gl.get_relation_maxlen()
    LSTM_DIM=gl.get_LSTM_DIM()
    # inputs.shape = (batch_size, time_steps, seq_len)
    print("input_shapeR: ")
    print(K.int_shape(input_shapeR))
    print("input_shapeQ: ")
    print(K.int_shape(input_shapeQ))


    mid = Dense(2*LSTM_DIM,name="att_dense",kernel_regularizer=regularizers.l2(0.01))(input_shapeR)
    print("mid: ")
    print(K.int_shape(mid))
    print("rq: ")
    rq = Permute((2, 1))(input_shapeQ)
    print(K.int_shape(rq))
    #a = K.batch_dot(mid,rq,axes=[2,1])
    a = Dot(axes=[2,2])([mid,input_shapeQ])
   # a = K.batch_dot(a,mid,axes=2)
    a = Activation('softmax')(a)

    ##rtt =Permute((2, 1))(input_shapeR)
    # x.shape = (batch_size, seq_len, time_steps)
    print("a: ")
    print(K.int_shape(a))


    outputs = Dot(axes=[1, 1])([input_shapeR, a])
    outputs = Permute((2, 1))(outputs)
    print("outputs: ")
    print(K.int_shape(outputs))
    return outputs
Пример #2
0
gl.set_NUM_FILTERS(150)
gl.set_filter_sizes([1, 3, 5])

NUM_FILTERS = gl.get_NUM_FILTERS()
filter_sizes = gl.get_filter_sizes()

#预处理

ques_train, rela_train, label_train, ques_test, rela_test, label_test, wd_idx = preprocess(
    train_rela_files, train_ques_file, train_label_file, test_rela_files,
    test_ques_file, test_label_file)
embedding_index = loadEmbeddingsIndex(preprocessWordVector_path,
                                      preprocessWordVector_files)
embedding_matrix = generateWord2VectorMatrix(embedding_index, wd_idx)
#model
relation_maxlen = gl.get_relation_maxlen()
ques_maxlen = gl.get_ques_maxlen()
tweet_relation = Input(shape=(relation_maxlen, ))
tweet_ques = Input(shape=(ques_maxlen, ))

relation_embedding_layer = Embedding(len(wd_idx) + 1,
                                     EMBEDDING_DIM,
                                     input_length=relation_maxlen,
                                     weights=[embedding_matrix],
                                     trainable=True)(tweet_relation)

relation_conv1 = Conv1D(128, 3, activation='tanh')(relation_embedding_layer)
relation_drop_1 = Dropout(0.2)(relation_conv1)
relation_max_1 = MaxPooling1D(relation_maxlen - 3 + 1)(relation_drop_1)
relation_conv2 = Conv1D(128, 1, activation='tanh')(relation_max_1)
relation_drop_2 = Dropout(0.2)(relation_conv2)