(self, inputs)
return logits
Implements core of model that transforms input_data into predictions.

        The core transformation for this model which transforms a batch of input
        data into a batch of predictions.

        Models can be added by defining another function like that have defined below
        (take `inputs` and generate `logtis`),
        and add two lines of control statement:
            if self.config.neural_model == 'yourModel':
                logits = basic_lstm_model(inputs)
        then you can specify which model to use in your config file.

        Args:
          inputs: a tensor have the shape of shape(b_sz, tstp, emb_sz)
        Returns:
          logits: A tensor take the shape of shape(batch_size, n_classes), score tensor
Implements core of model that transforms input_data into predictions.
Implements core of model that transforms input_data into predictions .
def add_model(self, inputs):
        """Implements core of model that transforms input_data into predictions.

        The core transformation for this model which transforms a batch of input
        data into a batch of predictions.

        Models can be added by defining another function like that have defined below
        (take `inputs` and generate `logtis`),
        and add two lines of control statement:
            if self.config.neural_model == 'yourModel':
                logits = basic_lstm_model(inputs)
        then you can specify which model to use in your config file.

        Args:
          inputs: a tensor have the shape of shape(b_sz, tstp, emb_sz)
        Returns:
          logits: A tensor take the shape of shape(batch_size, n_classes), score tensor
        """
        input_shape = tf.shape(inputs)
        b_sz = input_shape[0]
        tstp = input_shape[1]
        emb_sz = self.config.embed_size

        def basic_lstm_model(inputs):
            print "Loading basic lstm model.."
            for i in range(self.config.rnn_numLayers):
                with tf.variable_scope('rnnLayer'+str(i)):
                    lstm_cell = rnn_cell.BasicLSTMCell(self.config.hidden_size)
                    outputs, _ = tf.nn.dynamic_rnn(lstm_cell, inputs, self.ph_seqLen,  #(b_sz, tstp, h_sz)
                                                   dtype=tf.float32 ,swap_memory=True,
                                                   scope = 'basic_lstm_model_layer-'+str(i))
                    inputs = outputs #b_sz, tstp, h_sz
            mask = TfUtils.mkMask(self.ph_seqLen, tstp) # b_sz, tstp
            mask = tf.expand_dims(mask, axis=2) #b_sz, tstp, 1

            aggregate_state = TfUtils.reduce_avg(outputs, self.ph_seqLen, dim=1) #b_sz, h_sz
            inputs = aggregate_state
            inputs = tf.reshape(inputs, [-1, self.config.hidden_size])

            for i in range(self.config.fnn_numLayers):
                inputs = TfUtils.linear(inputs, self.config.hidden_size, bias=True, scope='fnn_layer-'+str(i))
                inputs = tf.nn.tanh(inputs)
            aggregate_state = inputs
            logits = TfUtils.linear(aggregate_state, self.config.class_num, bias=True, scope='fnn_softmax')
            return logits

        def basic_cbow_model(inputs):
            mask = TfUtils.mkMask(self.ph_seqLen, tstp) # b_sz, tstp
            mask = tf.expand_dims(mask, axis=2) #b_sz, tstp, 1

            aggregate_state = TfUtils.reduce_avg(inputs, self.ph_seqLen, dim=1) #b_sz, emb_sz
            inputs = aggregate_state
            inputs = tf.reshape(inputs, [-1, self.config.embed_size])

            for i in range(self.config.fnn_numLayers):
                inputs = TfUtils.linear(inputs, self.config.embed_size, bias=True, scope='fnn_layer-'+str(i))
                inputs = tf.nn.tanh(inputs)
            aggregate_state = inputs
            logits = TfUtils.linear(aggregate_state, self.config.class_num, bias=True, scope='fnn_softmax')
            return logits

        def basic_cnn_model(inputs):
            in_channel = self.config.embed_size
            filter_sizes = self.config.filter_sizes
            out_channel = self.config.num_filters
            input = inputs
            for layer in range(self.config.cnn_numLayers):
                with tf.name_scope("conv-layer-"+ str(layer)):
                    conv_outputs = []
                    for i, filter_size in enumerate(filter_sizes):
                        with tf.variable_scope("conv-maxpool-%d" % filter_size):
                            # Convolution Layer
                            filter_shape = [filter_size, in_channel, out_channel]
                            W = tf.get_variable(name='W', shape=filter_shape)
                            b = tf.get_variable(name='b', shape=[out_channel])
                            conv = tf.nn.conv1d(                # size (b_sz, tstp, out_channel)
                              input,
                              W,
                              stride=1,
                              padding="SAME",
                              name="conv")
                            # Apply nonlinearity
                            h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
                            conv_outputs.append(h)
                    input = tf.concat(axis=2, values=conv_outputs) #b_sz, tstp, out_channel*len(filter_sizes)
                    in_channel = out_channel * len(filter_sizes)
            # Maxpooling
#             mask = tf.sequence_mask(self.ph_seqLen, tstp, dtype=tf.float32) #(b_sz, tstp)
            mask = TfUtils.mkMask(self.ph_seqLen, tstp) # b_sz, tstp
            pooled = tf.reduce_max(input*tf.expand_dims(tf.cast(mask, dtype=tf.float32), 2), [1]) #(b_sz, out_channel*len(filter_sizes))
            #size (b_sz, out_channel*len(filter_sizes))
            inputs = tf.reshape(pooled, shape=[b_sz, out_channel*len(filter_sizes)])

            for i in range(self.config.fnn_numLayers):
                inputs = TfUtils.linear(inputs, self.config.embed_size, bias=True, scope='fnn_layer-'+str(i))
                inputs = tf.nn.tanh(inputs)
            aggregate_state = inputs
            logits = TfUtils.linear(aggregate_state, self.config.class_num, bias=True, scope='fnn_softmax')
            return logits

        if self.config.neural_model == 'lstm_basic':
            logits = basic_lstm_model(inputs)
        elif self.config.neural_model == 'cbow_basic':
            logits = basic_cbow_model(inputs)
        elif self.config.neural_model == 'cnn_basic':
            logits = basic_cnn_model(inputs)
        else:
            raise ValueError('No such model:'+ self.config.neural_model)

        return logits
def add_model ( self , inputs ) : input_shape = tf . shape ( inputs ) b_sz = input_shape [ 0 ] tstp = input_shape [ 1 ] emb_sz = self . config . embed_size def basic_lstm_model ( inputs ) : print "Loading basic lstm model.." for i in range ( self . config . rnn_numLayers ) : with tf . variable_scope ( 'rnnLayer' + str ( i ) ) : lstm_cell = rnn_cell . BasicLSTMCell ( self . config . hidden_size ) outputs , _ = tf . nn . dynamic_rnn ( lstm_cell , inputs , self . ph_seqLen , #(b_sz, tstp, h_sz) dtype = tf . float32 , swap_memory = True , scope = 'basic_lstm_model_layer-' + str ( i ) ) inputs = outputs #b_sz, tstp, h_sz mask = TfUtils . mkMask ( self . ph_seqLen , tstp ) # b_sz, tstp mask = tf . expand_dims ( mask , axis = 2 ) #b_sz, tstp, 1 aggregate_state = TfUtils . reduce_avg ( outputs , self . ph_seqLen , dim = 1 ) #b_sz, h_sz inputs = aggregate_state inputs = tf . reshape ( inputs , [ - 1 , self . config . hidden_size ] ) for i in range ( self . config . fnn_numLayers ) : inputs = TfUtils . linear ( inputs , self . config . hidden_size , bias = True , scope = 'fnn_layer-' + str ( i ) ) inputs = tf . nn . tanh ( inputs ) aggregate_state = inputs logits = TfUtils . linear ( aggregate_state , self . config . class_num , bias = True , scope = 'fnn_softmax' ) return logits def basic_cbow_model ( inputs ) : mask = TfUtils . mkMask ( self . ph_seqLen , tstp ) # b_sz, tstp mask = tf . expand_dims ( mask , axis = 2 ) #b_sz, tstp, 1 aggregate_state = TfUtils . reduce_avg ( inputs , self . ph_seqLen , dim = 1 ) #b_sz, emb_sz inputs = aggregate_state inputs = tf . reshape ( inputs , [ - 1 , self . config . embed_size ] ) for i in range ( self . config . fnn_numLayers ) : inputs = TfUtils . linear ( inputs , self . config . embed_size , bias = True , scope = 'fnn_layer-' + str ( i ) ) inputs = tf . nn . tanh ( inputs ) aggregate_state = inputs logits = TfUtils . linear ( aggregate_state , self . config . class_num , bias = True , scope = 'fnn_softmax' ) return logits def basic_cnn_model ( inputs ) : in_channel = self . config . embed_size filter_sizes = self . config . filter_sizes out_channel = self . config . num_filters input = inputs for layer in range ( self . config . cnn_numLayers ) : with tf . name_scope ( "conv-layer-" + str ( layer ) ) : conv_outputs = [ ] for i , filter_size in enumerate ( filter_sizes ) : with tf . variable_scope ( "conv-maxpool-%d" % filter_size ) : # Convolution Layer filter_shape = [ filter_size , in_channel , out_channel ] W = tf . get_variable ( name = 'W' , shape = filter_shape ) b = tf . get_variable ( name = 'b' , shape = [ out_channel ] ) conv = tf . nn . conv1d ( # size (b_sz, tstp, out_channel) input , W , stride = 1 , padding = "SAME" , name = "conv" ) # Apply nonlinearity h = tf . nn . relu ( tf . nn . bias_add ( conv , b ) , name = "relu" ) conv_outputs . append ( h ) input = tf . concat ( axis = 2 , values = conv_outputs ) #b_sz, tstp, out_channel*len(filter_sizes) in_channel = out_channel * len ( filter_sizes ) # Maxpooling #             mask = tf.sequence_mask(self.ph_seqLen, tstp, dtype=tf.float32) #(b_sz, tstp) mask = TfUtils . mkMask ( self . ph_seqLen , tstp ) # b_sz, tstp pooled = tf . reduce_max ( input * tf . expand_dims ( tf . cast ( mask , dtype = tf . float32 ) , 2 ) , [ 1 ] ) #(b_sz, out_channel*len(filter_sizes)) #size (b_sz, out_channel*len(filter_sizes)) inputs = tf . reshape ( pooled , shape = [ b_sz , out_channel * len ( filter_sizes ) ] ) for i in range ( self . config . fnn_numLayers ) : inputs = TfUtils . linear ( inputs , self . config . embed_size , bias = True , scope = 'fnn_layer-' + str ( i ) ) inputs = tf . nn . tanh ( inputs ) aggregate_state = inputs logits = TfUtils . linear ( aggregate_state , self . config . class_num , bias = True , scope = 'fnn_softmax' ) return logits if self . config . neural_model == 'lstm_basic' : logits = basic_lstm_model ( inputs ) elif self . config . neural_model == 'cbow_basic' : logits = basic_cbow_model ( inputs ) elif self . config . neural_model == 'cnn_basic' : logits = basic_cnn_model ( inputs ) else : raise ValueError ( 'No such model:' + self . config . neural_model ) return logits