(self)
:return:
def inference(self):
        """

        :return:
        """
        # 词向量映射
        with tf.name_scope("embedding"):
            embedding = tf.get_variable("embedding", [self.vocab_size, self.embedding_dim])
            embedding_inputs = tf.nn.embedding_lookup(embedding, self.input_x)
            embedding_inputs = tf.expand_dims(embedding_inputs, axis=-1)  # [None,seq,embedding,1]
            # region_embedding  # [batch,seq-3+1,1,250]
            region_embedding = tf.layers.conv2d(embedding_inputs, self.num_filters,
                                                [self.kernel_size, self.embedding_dim])

            pre_activation = tf.nn.relu(region_embedding, name='preactivation')

        with tf.name_scope("conv3_0"):
            conv3 = tf.layers.conv2d(pre_activation, self.num_filters, self.kernel_size,
                                     padding="same", activation=tf.nn.relu)
            conv3 = tf.layers.batch_normalization(conv3)

        with tf.name_scope("conv3_1"):
            conv3 = tf.layers.conv2d(conv3, self.num_filters, self.kernel_size,
                                     padding="same", activation=tf.nn.relu)
            conv3 = tf.layers.batch_normalization(conv3)

        # resdul
        conv3 = conv3 + region_embedding
        with tf.name_scope("pool_1"):
            pool = tf.pad(conv3, paddings=[[0, 0], [0, 1], [0, 0], [0, 0]])
            pool = tf.nn.max_pool(pool, [1, 3, 1, 1], strides=[1, 2, 1, 1], padding='VALID')

        with tf.name_scope("conv3_2"):
            conv3 = tf.layers.conv2d(pool, self.num_filters, self.kernel_size,
                                     padding="same", activation=tf.nn.relu)
            conv3 = tf.layers.batch_normalization(conv3)

        with tf.name_scope("conv3_3"):
            conv3 = tf.layers.conv2d(conv3, self.num_filters, self.kernel_size,
                                     padding="same", activation=tf.nn.relu)
            conv3 = tf.layers.batch_normalization(conv3)

        # resdul
        conv3 = conv3 + pool
        pool_size = int((self.seq_length - 3 + 1)/2)
        conv3 = tf.layers.max_pooling1d(tf.squeeze(conv3, [2]), pool_size, 1)
        conv3 = tf.squeeze(conv3, [1]) # [batch,250]
        conv3 = tf.nn.dropout(conv3, self.keep_prob)

        with tf.name_scope("score"):
            # classify
            self.logits = tf.layers.dense(conv3, self.num_classes, name='fc2')
            self.y_pred_cls = tf.argmax(tf.nn.softmax(self.logits), 1, name="pred")

        with tf.name_scope("loss"):
            # 损失函数，交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                logits=self.logits, labels=self.input_y)

            # l2_loss = tf.losses.get_regularization_loss()
            self.loss = tf.reduce_mean(cross_entropy, name="loss")
            # self.loss += l2_loss

            # optim
            self.optim = tf.train.AdamOptimizer(
                learning_rate=self.learning_rate).minimize(self.loss)
        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(tf.argmax(self.input_y, 1), self.y_pred_cls)
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name="acc")
def inference ( self ) : # 词向量映射 with tf . name_scope ( "embedding" ) : embedding = tf . get_variable ( "embedding" , [ self . vocab_size , self . embedding_dim ] ) embedding_inputs = tf . nn . embedding_lookup ( embedding , self . input_x ) embedding_inputs = tf . expand_dims ( embedding_inputs , axis = - 1 ) # [None,seq,embedding,1] # region_embedding  # [batch,seq-3+1,1,250] region_embedding = tf . layers . conv2d ( embedding_inputs , self . num_filters , [ self . kernel_size , self . embedding_dim ] ) pre_activation = tf . nn . relu ( region_embedding , name = 'preactivation' ) with tf . name_scope ( "conv3_0" ) : conv3 = tf . layers . conv2d ( pre_activation , self . num_filters , self . kernel_size , padding = "same" , activation = tf . nn . relu ) conv3 = tf . layers . batch_normalization ( conv3 ) with tf . name_scope ( "conv3_1" ) : conv3 = tf . layers . conv2d ( conv3 , self . num_filters , self . kernel_size , padding = "same" , activation = tf . nn . relu ) conv3 = tf . layers . batch_normalization ( conv3 ) # resdul conv3 = conv3 + region_embedding with tf . name_scope ( "pool_1" ) : pool = tf . pad ( conv3 , paddings = [ [ 0 , 0 ] , [ 0 , 1 ] , [ 0 , 0 ] , [ 0 , 0 ] ] ) pool = tf . nn . max_pool ( pool , [ 1 , 3 , 1 , 1 ] , strides = [ 1 , 2 , 1 , 1 ] , padding = 'VALID' ) with tf . name_scope ( "conv3_2" ) : conv3 = tf . layers . conv2d ( pool , self . num_filters , self . kernel_size , padding = "same" , activation = tf . nn . relu ) conv3 = tf . layers . batch_normalization ( conv3 ) with tf . name_scope ( "conv3_3" ) : conv3 = tf . layers . conv2d ( conv3 , self . num_filters , self . kernel_size , padding = "same" , activation = tf . nn . relu ) conv3 = tf . layers . batch_normalization ( conv3 ) # resdul conv3 = conv3 + pool pool_size = int ( ( self . seq_length - 3 + 1 ) / 2 ) conv3 = tf . layers . max_pooling1d ( tf . squeeze ( conv3 , [ 2 ] ) , pool_size , 1 ) conv3 = tf . squeeze ( conv3 , [ 1 ] ) # [batch,250] conv3 = tf . nn . dropout ( conv3 , self . keep_prob ) with tf . name_scope ( "score" ) : # classify self . logits = tf . layers . dense ( conv3 , self . num_classes , name = 'fc2' ) self . y_pred_cls = tf . argmax ( tf . nn . softmax ( self . logits ) , 1 , name = "pred" ) with tf . name_scope ( "loss" ) : # 损失函数，交叉熵 cross_entropy = tf . nn . softmax_cross_entropy_with_logits ( logits = self . logits , labels = self . input_y ) # l2_loss = tf.losses.get_regularization_loss() self . loss = tf . reduce_mean ( cross_entropy , name = "loss" ) # self.loss += l2_loss # optim self . optim = tf . train . AdamOptimizer ( learning_rate = self . learning_rate ) . minimize ( self . loss ) with tf . name_scope ( "accuracy" ) : # 准确率 correct_pred = tf . equal ( tf . argmax ( self . input_y , 1 ) , self . y_pred_cls ) self . acc = tf . reduce_mean ( tf . cast ( correct_pred , tf . float32 ) , name = "acc" )