(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)

        # average vectors, to get representation of the sentence
        with tf.name_scope("average"):
            mean_sentence = tf.reduce_mean(embedding_inputs, axis=1)

        # linear classifier
        with tf.name_scope("score"):
            # 分类器
            self.logits = tf.layers.dense(mean_sentence, self.num_classes,
                                          kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001),
                                          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 ) # average vectors, to get representation of the sentence with tf . name_scope ( "average" ) : mean_sentence = tf . reduce_mean ( embedding_inputs , axis = 1 ) # linear classifier with tf . name_scope ( "score" ) : # 分类器 self . logits = tf . layers . dense ( mean_sentence , self . num_classes , kernel_regularizer = tf . contrib . layers . l2_regularizer ( 0.001 ) , 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" )