(self, inputs)
return [token_out, softmax_out, adjvec_out, sigmoid_out]
build rnn net
build rnn net
build rnn net
def build_rnn(self, inputs):
        """
            build rnn net
        """
        batch_size = self.parser_args.batch_size
        input_size = self.parser_args.input_size
        hidden_size = self.parser_args.hidden_size
        num_layers = self.parser_args.num_layers
        num_nodes = self.parser_args.num_nodes
        num_tokens = self.parser_args.num_tokens

        depth = max(num_nodes - 1, num_tokens)
        lstm_unit = LstmUnit(input_size, hidden_size, num_layers)

        def encode_token(inp):
            """
                encode token
            """
            token = fluid.layers.assign(inp)
            token.stop_gradient = True
            token = fluid.layers.one_hot(token, depth)
            return token

        def encode_adj(adj, step):
            """
                encode adj
            """
            adj = fluid.layers.cast(adj, dtype='float32')
            adj_pad = fluid.layers.pad(x=adj, paddings=[0, 0, 0, depth - step],
                                       pad_value=0.0)
            return adj_pad

        def decode_token(hidden):
            """
                decode token
            """
            initiallizer = fluid.initializer.TruncatedNormalInitializer(
                    scale=np.sqrt(2.0 / self.parser_args.hidden_size))
            param_attr = fluid.ParamAttr(initializer=initiallizer)
            logits = fluid.layers.fc(hidden, num_tokens, param_attr=param_attr)
            temper = 5.0
            tanh_c = 2.5
            logits = fluid.layers.tanh(logits / temper) * tanh_c
            token = distribute_generator.multinomial(logits,
                                                     [batch_size, 1], 1)
            return token, fluid.layers.unsqueeze(logits, axes=[1])

        def decode_adj(hidden, step):
            """
                decode adj
            """
            initiallizer = fluid.initializer.TruncatedNormalInitializer(
                    scale=np.sqrt(2.0 / self.parser_args.hidden_size))
            param_attr = fluid.ParamAttr(initializer=initiallizer)
            logits = fluid.layers.fc(hidden, step, param_attr=param_attr)
            temper = 5.0
            tanh_c = 2.5
            logits = fluid.layers.tanh(logits / temper) * tanh_c
            adj = distribute_generator.bernoulli(logits,
                                                 output_shape=logits.shape)
            return adj, logits

        tokens = []
        softmax = []
        adjvec = []
        sigmoid = []

        def rnn_block(hidden, last_hidden, last_cell):
            """
                rnn block
            """
            last_output, last_hidden, last_cell = lstm_unit(
                    hidden, last_hidden, last_cell)
            token, logits = decode_token(last_output)
            tokens.append(token)
            softmax.append(logits)

            for step in range(1, num_nodes):
                token_vec = encode_token(token)
                last_output, last_hidden, last_cell = lstm_unit(
                        token_vec, last_hidden, last_cell)
                adj, logits = decode_adj(last_output, step)
                adjvec.append(adj)
                sigmoid.append(logits)
                adj_vec = encode_adj(adj, step)
                last_output, last_hidden, last_cell = lstm_unit(
                        adj_vec, last_hidden, last_cell)
                token, logits = decode_token(last_output)
                tokens.append(token)
                softmax.append(logits)
            return token, last_hidden, last_cell
        init_hidden = fluid.layers.fill_constant(
                shape=[num_layers, batch_size, hidden_size],
                value=0.0, dtype='float32')
        init_cell = fluid.layers.fill_constant(
                shape=[num_layers, batch_size, hidden_size],
                value=0.0, dtype='float32')

        hidden = encode_adj(inputs, 1)
        token, last_hidden, last_cell = rnn_block(hidden, init_hidden,
                                                  init_cell)
        hidden = encode_token(token)
        token, last_hidden, last_cell = rnn_block(hidden, last_hidden,
                                                  last_cell)
        token_out = fluid.layers.concat(tokens, axis=1)
        softmax_out = fluid.layers.concat(softmax, axis=1)
        adjvec_out = fluid.layers.concat(adjvec, axis=1)
        sigmoid_out = fluid.layers.concat(sigmoid, axis=1)
        return [token_out, softmax_out, adjvec_out, sigmoid_out]
def build_rnn ( self , inputs ) : batch_size = self . parser_args . batch_size input_size = self . parser_args . input_size hidden_size = self . parser_args . hidden_size num_layers = self . parser_args . num_layers num_nodes = self . parser_args . num_nodes num_tokens = self . parser_args . num_tokens depth = max ( num_nodes - 1 , num_tokens ) lstm_unit = LstmUnit ( input_size , hidden_size , num_layers ) def encode_token ( inp ) : """
                encode token
            """ token = fluid . layers . assign ( inp ) token . stop_gradient = True token = fluid . layers . one_hot ( token , depth ) return token def encode_adj ( adj , step ) : """
                encode adj
            """ adj = fluid . layers . cast ( adj , dtype = 'float32' ) adj_pad = fluid . layers . pad ( x = adj , paddings = [ 0 , 0 , 0 , depth - step ] , pad_value = 0.0 ) return adj_pad def decode_token ( hidden ) : """
                decode token
            """ initiallizer = fluid . initializer . TruncatedNormalInitializer ( scale = np . sqrt ( 2.0 / self . parser_args . hidden_size ) ) param_attr = fluid . ParamAttr ( initializer = initiallizer ) logits = fluid . layers . fc ( hidden , num_tokens , param_attr = param_attr ) temper = 5.0 tanh_c = 2.5 logits = fluid . layers . tanh ( logits / temper ) * tanh_c token = distribute_generator . multinomial ( logits , [ batch_size , 1 ] , 1 ) return token , fluid . layers . unsqueeze ( logits , axes = [ 1 ] ) def decode_adj ( hidden , step ) : """
                decode adj
            """ initiallizer = fluid . initializer . TruncatedNormalInitializer ( scale = np . sqrt ( 2.0 / self . parser_args . hidden_size ) ) param_attr = fluid . ParamAttr ( initializer = initiallizer ) logits = fluid . layers . fc ( hidden , step , param_attr = param_attr ) temper = 5.0 tanh_c = 2.5 logits = fluid . layers . tanh ( logits / temper ) * tanh_c adj = distribute_generator . bernoulli ( logits , output_shape = logits . shape ) return adj , logits tokens = [ ] softmax = [ ] adjvec = [ ] sigmoid = [ ] def rnn_block ( hidden , last_hidden , last_cell ) : """
                rnn block
            """ last_output , last_hidden , last_cell = lstm_unit ( hidden , last_hidden , last_cell ) token , logits = decode_token ( last_output ) tokens . append ( token ) softmax . append ( logits ) for step in range ( 1 , num_nodes ) : token_vec = encode_token ( token ) last_output , last_hidden , last_cell = lstm_unit ( token_vec , last_hidden , last_cell ) adj , logits = decode_adj ( last_output , step ) adjvec . append ( adj ) sigmoid . append ( logits ) adj_vec = encode_adj ( adj , step ) last_output , last_hidden , last_cell = lstm_unit ( adj_vec , last_hidden , last_cell ) token , logits = decode_token ( last_output ) tokens . append ( token ) softmax . append ( logits ) return token , last_hidden , last_cell init_hidden = fluid . layers . fill_constant ( shape = [ num_layers , batch_size , hidden_size ] , value = 0.0 , dtype = 'float32' ) init_cell = fluid . layers . fill_constant ( shape = [ num_layers , batch_size , hidden_size ] , value = 0.0 , dtype = 'float32' ) hidden = encode_adj ( inputs , 1 ) token , last_hidden , last_cell = rnn_block ( hidden , init_hidden , init_cell ) hidden = encode_token ( token ) token , last_hidden , last_cell = rnn_block ( hidden , last_hidden , last_cell ) token_out = fluid . layers . concat ( tokens , axis = 1 ) softmax_out = fluid . layers . concat ( softmax , axis = 1 ) adjvec_out = fluid . layers . concat ( adjvec , axis = 1 ) sigmoid_out = fluid . layers . concat ( sigmoid , axis = 1 ) return [ token_out , softmax_out , adjvec_out , sigmoid_out ]