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

        pooled_outputs = []
        for i, filter_size in enumerate(self.filter_sizes):
            with tf.name_scope("conv-%s" % i):
                # conv layer
                conv = tf.layers.conv1d(embedding_inputs, self.num_filters,filter_size,
                                        padding='valid', activation=tf.nn.relu,
                                        kernel_regularizer=self.regularizer)
                # global max pooling
                pooled = tf.layers.max_pooling1d(conv, self.seq_length - filter_size + 1, 1)
                pooled_outputs.append(pooled)

        num_filters_total = self.num_filters * len(self.filter_sizes)
        h_pool = tf.concat(pooled_outputs, 2)
        h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total])

        # # Add dropout
        # with tf.name_scope("dropout"):
        #     h_drop = tf.layers.dropout(h_pool_flat, self.drop_prob)

        with tf.name_scope("score"):
            fc = tf.layers.dense(h_pool_flat, self.hidden_dim, activation=tf.nn.relu, name='fc1')
            fc = tf.layers.dropout(fc, self.drop_prob)
            # classify
            self.logits = tf.layers.dense(fc, 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 ) pooled_outputs = [ ] for i , filter_size in enumerate ( self . filter_sizes ) : with tf . name_scope ( "conv-%s" % i ) : # conv layer conv = tf . layers . conv1d ( embedding_inputs , self . num_filters , filter_size , padding = 'valid' , activation = tf . nn . relu , kernel_regularizer = self . regularizer ) # global max pooling pooled = tf . layers . max_pooling1d ( conv , self . seq_length - filter_size + 1 , 1 ) pooled_outputs . append ( pooled ) num_filters_total = self . num_filters * len ( self . filter_sizes ) h_pool = tf . concat ( pooled_outputs , 2 ) h_pool_flat = tf . reshape ( h_pool , [ - 1 , num_filters_total ] ) # # Add dropout # with tf.name_scope("dropout"): #     h_drop = tf.layers.dropout(h_pool_flat, self.drop_prob) with tf . name_scope ( "score" ) : fc = tf . layers . dense ( h_pool_flat , self . hidden_dim , activation = tf . nn . relu , name = 'fc1' ) fc = tf . layers . dropout ( fc , self . drop_prob ) # classify self . logits = tf . layers . dense ( fc , 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" )