Exemplo n.º 1
0
    def build_model(self):
        with tf.variable_scope("knowledge-embedding-layer"):
            knowledge_embedding = tf.Variable(tf.random_normal([2, self.config.knowledge_dimension],
                                                               stddev=0, seed=1), trainable=True,
                                                               name='knowledge-embedding')
            self.x2_label_embedding = tf.nn.embedding_lookup(knowledge_embedding, self.x2_label)

        with tf.variable_scope("first-CNN-layer"):
            self.output_x1_1 = tf.layers.conv1d(self.input_X1,filters=self.config.filters_num,kernel_size=self.config.first_kernel_size,padding='same',name='first-cnn1')
            # self.output_x1_1 = tf.nn.dropout(self.output_x1_1, self.dropout_rate)
            self.output_x2_1 = tf.layers.conv1d(self.input_X2,filters=self.config.filters_num,kernel_size=self.config.first_kernel_size,padding='same',name='first-cnn2')
            # self.output_x2_1 = tf.nn.dropout(self.output_x2_1, self.dropout_rate)

        with tf.variable_scope("first-interaction"):
            interaction = modules.Interaction(10, self.output_x1_1,self.output_x2_1,self.x2_label_embedding)
            self.inter1_output_x2 = interaction.exeInteraction()

        with tf.variable_scope("second-CNN-layer"):
            self.output_x1_2 = tf.layers.conv1d(self.output_x1_1,filters=self.config.filters_num,kernel_size=self.config.second_kernel_size,padding='same',name='second-cnn1')
            self.output_x2_2 = tf.layers.conv1d(self.output_x2_1,filters=self.config.filters_num,kernel_size=self.config.second_kernel_size,padding='same',name='second-cnn2')
            # self.output_x1_2 = tf.nn.dropout(self.output_x1_2, self.dropout_rate)
            # self.output_x2_2 = tf.nn.dropout(self.output_x2_2, self.dropout_rate)

        with tf.variable_scope("second-interaction"):
            interaction = modules.Interaction(10, self.output_x1_2, self.output_x2_2,self.x2_label_embedding)
            self.inter2_output_x2 = interaction.exeInteraction()

        with tf.variable_scope("third-CNN-layer"):
            self.output_x1_3 = tf.layers.conv1d(self.output_x1_2,filters=self.config.filters_num,kernel_size=self.config.third_kernel_size,padding='same',name='third-cnn1')
            self.output_x2_3 = tf.layers.conv1d(self.output_x2_2,filters=self.config.filters_num,kernel_size=self.config.third_kernel_size,padding='same',name='third-cnn2')
            # self.output_x1_3 = tf.nn.dropout(self.output_x1_3, self.dropout_rate)
            # self.output_x2_3 = tf.nn.dropout(self.output_x2_3, self.dropout_rate)

        with tf.variable_scope("third-interaction"):
            interaction = modules.Interaction(10, self.output_x1_3,self.output_x2_3,self.x2_label_embedding)
            self.inter3_output_x2 = interaction.exeInteraction()

        with tf.variable_scope("fusion-layer"):
            self.fusion_output = tf.concat([self.inter1_output_x2, self.inter2_output_x2,self.inter3_output_x2], axis=-1)  # [Batch, 3 * len]

        with tf.variable_scope("predict-layer"):
            self.output_ = tf.nn.relu(tf.layers.dense(inputs=self.fusion_output,units=self.config.mlp_output,name='fnn1'))
            self.output_ = tf.layers.dropout(self.output_,rate=self.dropout_rate)
            self.logit = tf.layers.dense(inputs=self.output_,units=2,name='fnn2')

        with tf.variable_scope("optimize-layer"):
            self.pred_y = tf.argmax(tf.nn.softmax(self.logit), 1)
            # 损失函数,交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=self.logit,
                                                                    labels=self.y)  # 对logits进行softmax操作后,做交叉墒,输出的是一个向量
            self.loss = tf.reduce_mean(cross_entropy)  # 将交叉熵向量求和,即可得到交叉熵
            # 优化器
            self.optim = tf.train.AdamOptimizer(learning_rate=param.BaseConfig.lr).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(tf.argmax(self.y, 1),
                                    self.pred_y)  # 由于input_y也是onehot编码,因此,调用tf.argmax(self.input_y)得到的是1所在的下表
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    def build_model(self):

        #WIL-1  word-Interaction-layer
        # with tf.variable_scope("word-Interaction-layer"):
        #     interaction = modules.Interaction(4, self.input_X1, self.input_X2)
        #     self.inter0_output_x2 = interaction.exeInteraction()

        #WIL-2  word-Interaction-layer
        with tf.variable_scope("word-Interaction-layer"):
            self.beta = tf.Variable(tf.random_normal(shape=[3,1], stddev=0, seed=1, dtype=tf.float32), trainable=True, name='beta')
            interaction = modules.Interaction(6, self.input_X1, self.input_X2, self.x2_label, self.beta)
            self.inter0_output_x2 = interaction.exeInteraction()
            #添加一个mean pooling
            # self.inter0_output_x2 = tf.expand_dims(tf.expand_dims(self.inter0_output_x2,axis=2),axis=3)
            # self.inter0_output_x2 = tf.nn.avg_pool(self.inter0_output_x2,ksize=[1,3,1,1],strides=[1,1,1,1],padding='VALID',name='mean-pooling')

        with tf.variable_scope("first-CNN-layer"):
            self.output_x1_1 = tf.layers.conv1d(self.input_X1,filters=self.config.filters_num,kernel_size=self.config.first_kernel_size,padding='same',name='first-cnn1')
            self.output_x2_1 = tf.layers.conv1d(self.input_X2,filters=self.config.filters_num,kernel_size=self.config.first_kernel_size,padding='same',name='first-cnn2')

        with tf.variable_scope("first-interaction"):
            self.beta1 = tf.Variable(tf.random_normal(shape=[3,1], stddev=0, seed=1, dtype=tf.float32), trainable=True,
                                    name='beta1')
            interaction = modules.Interaction(6, self.output_x1_1,self.output_x2_1,self.x2_label,self.beta1)
            self.inter1_output_x2 = interaction.exeInteraction()
            # self.inter1_output_x2 = self.interaction(self.output_x1_1, self.output_x2_1)

            #mean pooling
            # self.inter1_output_x2 = tf.expand_dims(tf.expand_dims(self.inter1_output_x2,axis=2),axis=3)
            # self.inter1_output_x2 = tf.nn.avg_pool(self.inter1_output_x2,ksize=[1,3,1,1],strides=[1,1,1,1],padding='VALID', name='mean-pooling')
            # self.inter1_output_x2 = tf.reshape(self.inter1_output_x2, shape=[-1, 28])

        with tf.variable_scope("second-CNN-layer"):
            self.output_x1_2 = tf.layers.conv1d(self.output_x1_1,filters=self.config.filters_num,kernel_size=self.config.second_kernel_size,padding='same',name='second-cnn1')
            self.output_x2_2 = tf.layers.conv1d(self.output_x2_1,filters=self.config.filters_num,kernel_size=self.config.second_kernel_size,padding='same',name='second-cnn2')

        with tf.variable_scope("second-interaction"):
            self.beta2 = tf.Variable(tf.random_normal(shape=[3,1], stddev=0, seed=1, dtype=tf.float32), trainable=True,
                                    name='beta2')
            interaction = modules.Interaction(6, self.output_x1_2, self.output_x2_2,self.x2_label,self.beta2)
            self.inter2_output_x2 = interaction.exeInteraction()
            # self.inter2_output_x2 = self.interaction(self.output_x1_2, self.output_x2_2)

            #mean pooling
            # self.inter2_output_x2 = tf.expand_dims(tf.expand_dims(self.inter2_output_x2,axis=2),axis=3)
            # self.inter2_output_x2 = tf.nn.avg_pool(self.inter2_output_x2,ksize=[1,3,1,1],strides=[1,1,1,1],padding='VALID', name='mean-pooling')
            # self.inter2_output_x2 = tf.reshape(self.inter2_output_x2,shape=[-1,28])

        with tf.variable_scope("third-CNN-layer"):
            self.output_x1_3 = tf.layers.conv1d(self.output_x1_2,filters=self.config.filters_num,kernel_size=self.config.third_kernel_size,padding='same',name='third-cnn1')
            self.output_x2_3 = tf.layers.conv1d(self.output_x2_2,filters=self.config.filters_num,kernel_size=self.config.third_kernel_size,padding='same',name='third-cnn2')

        with tf.variable_scope("third-interaction"):
            self.beta3 = tf.Variable(tf.random_normal(shape=[3,1], stddev=0, seed=1, dtype=tf.float32), trainable=True,
                                    name='beta3')
            interaction = modules.Interaction(6, self.output_x1_3,self.output_x2_3,self.x2_label,self.beta3)
            self.inter3_output_x2 = interaction.exeInteraction()
            # self.inter3_output_x2 = self.interaction(self.output_x1_3,self.output_x2_3)

            #mean pooling
            # self.inter3_output_x2 = tf.expand_dims(tf.expand_dims(self.inter3_output_x2,axis=2),axis=3)
            # self.inter3_output_x2 = tf.nn.avg_pool(self.inter3_output_x2,ksize=[1,3,1,1],strides=[1,1,1,1],padding='VALID', name='mean-pooling')
            # self.inter3_output_x2 = tf.reshape(self.inter3_output_x2, shape=[-1, 28])


        with tf.variable_scope("fusion-layer"):
            #v2
            # self.fusion_output = tf.stack(
            #     [self.inter1_output_x2, self.inter2_output_x2, self.inter3_output_x2],
            #     axis=-1)  # [Batch,len,3]
            # self.fusion_output = tf.layers.conv1d(self.output_x1_1, filters=64,
            #                                       kernel_size=self.config.second_kernel_size, padding='same',
            #                                       name='fifth-cnn1')
            # self.fusion_output = tf.reduce_max(self.fusion_output, axis=-1)

            # #v3
            # self.fusion_output = tf.stack([self.inter1_output_x2, self.inter2_output_x2,self.inter3_output_x2,self.x2_label], axis=-1)  # [Batch,len,4]
            # self.fusion_output = tf.layers.conv1d(self.output_x1_1,filters=64,kernel_size=self.config.second_kernel_size,padding='same',name='fifth-cnn1')
            # self.fusion_output = tf.reduce_max(self.fusion_output,axis=-1)

            #v4
            # self.fusion_output = tf.concat([self.inter1_output_x2, self.inter2_output_x2,self.inter3_output_x2],axis=-1)

            #v5
            self.fusion_output = tf.concat([self.inter0_output_x2, self.inter1_output_x2, self.inter2_output_x2, self.inter3_output_x2],
                                           axis=-1)

        with tf.variable_scope("predict-layer"):
            self.output_ = tf.nn.relu(tf.layers.dense(inputs=self.fusion_output,units=self.config.mlp_output,name='fnn1'))
            self.output_ = tf.layers.dropout(self.output_,rate=self.config.dropout_rate)
            self.logit = tf.layers.dense(inputs=self.output_,units=2,name='fnn2')

        with tf.variable_scope("optimize-layer"):
            self.pred_y = tf.argmax(tf.nn.softmax(self.logit), 1)
            # 损失函数,交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=self.logit,
                                                                    labels=self.y)  # 对logits进行softmax操作后,做交叉墒,输出的是一个向量
            self.loss = tf.reduce_mean(cross_entropy)  # 将交叉熵向量求和,即可得到交叉熵
            # 优化器
            self.optim = tf.train.AdamOptimizer(learning_rate=param.BaseConfig.lr).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(tf.argmax(self.y, 1),
                                    self.pred_y)  # 由于input_y也是onehot编码,因此,调用tf.argmax(self.input_y)得到的是1所在的下表
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
Exemplo n.º 3
0
    def build_model(self):
        with tf.variable_scope("first-CNN-layer"):
            self.output_x1_1 = tf.layers.conv1d(
                self.input_X1,
                filters=self.config.filters_num,
                kernel_size=self.config.first_kernel_size,
                padding='same',
                name='first-cnn1')
            self.output_x2_1 = tf.layers.conv1d(
                self.input_X2,
                filters=self.config.filters_num,
                kernel_size=self.config.first_kernel_size,
                padding='same',
                name='first-cnn2')
            self.pooling_x2_1 = tf.reduce_max(self.output_x2_1,
                                              axis=-1)  #[B,l]

        with tf.variable_scope("second-CNN-layer"):
            self.output_x1_2 = tf.layers.conv1d(
                self.output_x1_1,
                filters=self.config.filters_num,
                kernel_size=self.config.second_kernel_size,
                padding='same',
                name='second-cnn1')
            self.output_x2_2 = tf.layers.conv1d(
                self.output_x2_1,
                filters=self.config.filters_num,
                kernel_size=self.config.second_kernel_size,
                padding='same',
                name='second-cnn2')

        with tf.variable_scope("third-CNN-layer"):
            self.output_x1_3 = tf.layers.conv1d(
                self.output_x1_2,
                filters=self.config.filters_num,
                kernel_size=self.config.third_kernel_size,
                padding='same',
                name='third-cnn1')
            self.output_x2_3 = tf.layers.conv1d(
                self.output_x2_2,
                filters=self.config.filters_num,
                kernel_size=self.config.third_kernel_size,
                padding='same',
                name='third-cnn2')
            self.pooling_x2_3 = tf.reduce_max(self.output_x2_3, axis=-1)

        with tf.variable_scope("interaction"):
            interaction11 = modules.Interaction(4, self.output_x1_1,
                                                self.output_x2_1)
            self.inter11 = interaction11.exeInteraction()

            # interaction21 = modules.Interaction(4, self.output_x1_2, self.output_x2_1)
            # self.inter21 = interaction21.exeInteraction()

            # interaction31 = modules.Interaction(4, self.output_x1_3, self.output_x2_1)
            # self.inter31 = interaction31.exeInteraction()

            # interaction12 = modules.Interaction(4, self.output_x1_1,self.output_x2_2)
            # self.inter12 = interaction12.exeInteraction()

            interaction22 = modules.Interaction(4, self.output_x1_2,
                                                self.output_x2_2)
            self.inter22 = interaction22.exeInteraction()

            # interaction32 = modules.Interaction(4, self.output_x1_3, self.output_x2_2)
            # self.inter32 = interaction32.exeInteraction()
            #
            # interaction13 = modules.Interaction(4, self.output_x1_1,self.output_x2_3)
            # self.inter13 = interaction13.exeInteraction()
            #
            # interaction23 = modules.Interaction(4, self.output_x1_2, self.output_x2_3)
            # self.inter23 = interaction23.exeInteraction()

            interaction33 = modules.Interaction(4, self.output_x1_3,
                                                self.output_x2_3)
            self.inter33 = interaction33.exeInteraction()  #[B,l]

            # self.inter = tf.reshape(tf.stack([self.inter11,self.inter12,self.inter13,self.inter12,self.inter22,self.inter32,self.inter31,self.inter32,self.inter33],axis=-1),
            #            shape=[-1,self.config.Y_maxlen,9]) #[B,9,l]
            self.inter = tf.reshape(tf.stack(
                [self.inter11, self.inter22, self.inter33], axis=-1),
                                    shape=[-1, self.config.Y_maxlen, 3])

        with tf.variable_scope("fusion-layer"):
            self.fusion_output = tf.reduce_max(self.inter, axis=1)

        with tf.variable_scope("predict-layer"):
            self.output_ = tf.nn.relu(
                tf.layers.dense(inputs=self.fusion_output,
                                units=self.config.mlp_output,
                                name='fnn1'))
            self.output_ = tf.layers.dropout(self.output_,
                                             rate=self.config.dropout_rate)
            self.logit = tf.layers.dense(inputs=self.output_,
                                         units=2,
                                         name='fnn2')

        with tf.variable_scope("optimize-layer"):
            self.pred_y = tf.argmax(tf.nn.softmax(self.logit), 1)
            # 损失函数,交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                logits=self.logit,
                labels=self.y)  # 对logits进行softmax操作后,做交叉墒,输出的是一个向量
            self.loss = tf.reduce_mean(cross_entropy)  # 将交叉熵向量求和,即可得到交叉熵
            # 优化器
            self.optim = tf.train.AdamOptimizer(
                learning_rate=param.BaseConfig.lr).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(
                tf.argmax(self.y, 1), self.pred_y
            )  # 由于input_y也是onehot编码,因此,调用tf.argmax(self.input_y)得到的是1所在的下表
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
Exemplo n.º 4
0
    def build_model(self):
        with tf.variable_scope("first-CNN-layer"):
            self.output_x1_1 = tf.layers.conv1d(
                self.input_X1,
                filters=self.config.filters_num,
                kernel_size=self.config.first_kernel_size,
                padding='same',
                name='first-cnn1')
            self.output_x2_1 = tf.layers.conv1d(
                self.input_X2,
                filters=self.config.filters_num,
                kernel_size=self.config.first_kernel_size,
                padding='same',
                name='first-cnn2')

        with tf.variable_scope("first-interaction"):
            interactionModel = mudules.Interaction(3, self.output_x1_1,
                                                   self.output_x2_1)
            self.inter_x1_1, self.inter_x2_1, self.inter_x2_weight_1 = interactionModel.exeInteraction(
            )

        with tf.variable_scope("second-CNN-layer"):
            self.output_x1_2 = tf.layers.conv1d(
                self.inter_x1_1,
                filters=self.config.filters_num,
                kernel_size=self.config.second_kernel_size,
                padding='same',
                name='second-cnn1')
            self.output_x2_2 = tf.layers.conv1d(
                self.inter_x2_1,
                filters=self.config.filters_num,
                kernel_size=self.config.second_kernel_size,
                padding='same',
                name='second-cnn2')

        with tf.variable_scope("second-interaction"):
            interactionModel = mudules.Interaction(3, self.output_x1_2,
                                                   self.output_x2_2)
            self.inter_x1_2, self.inter_x2_2, self.inter_x2_weight_2 = interactionModel.exeInteraction(
            )

        with tf.variable_scope("fusion-layer"):
            fusionModel = mudules.Fusion(1, self.inter_x2_weight_1,
                                         self.inter_x2_weight_2)
            self.fusion_output = fusionModel.exeFusion()

        with tf.variable_scope("predict-layer"):
            self.output_ = tf.nn.relu(
                tf.layers.dense(inputs=self.fusion_output,
                                units=self.config.mlp_output,
                                name='fnn1'))
            self.output_ = tf.layers.dropout(self.output_,
                                             rate=self.config.dropout_rate)
            self.logit = tf.layers.dense(inputs=self.output_,
                                         units=2,
                                         name='fnn2')

        with tf.variable_scope("optimize-layer"):
            self.pred_y = tf.argmax(tf.nn.softmax(self.logit), 1)
            # 损失函数,交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                logits=self.logit,
                labels=self.y)  # 对logits进行softmax操作后,做交叉墒,输出的是一个向量
            self.loss = tf.reduce_mean(cross_entropy)  # 将交叉熵向量求和,即可得到交叉熵
            # 优化器
            self.optim = tf.train.AdamOptimizer(
                learning_rate=param.BaseConfig.lr).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(
                tf.argmax(self.y, 1), self.pred_y
            )  # 由于input_y也是onehot编码,因此,调用tf.argmax(self.input_y)得到的是1所在的下表
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))