(self, inputs, state, scope=None)
return m, new_state
Run one step of LSTM.
    
        Args:
          inputs: input Tensor, 2D, batch x num_units.
          state: if `state_is_tuple` is False, this must be a state Tensor,
            `2-D, batch x state_size`.  If `state_is_tuple` is True, this must be a
            tuple of state Tensors, both `2-D`, with column sizes `c_state` and
            `m_state`.
          scope: VariableScope for the created subgraph; defaults to "lstm_cell".
    
        Returns:
          A tuple containing:
    
          - A `2-D, [batch x output_dim]`, Tensor representing the output of the
            LSTM after reading `inputs` when previous state was `state`.
            Here output_dim is:
               num_proj if num_proj was set,
               num_units otherwise.
          - Tensor(s) representing the new state of LSTM after reading `inputs` when
            the previous state was `state`.  Same type and shape(s) as `state`.
    
        Raises:
          ValueError: If input size cannot be inferred from inputs via
            static shape inference.
Run one step of LSTM.
    
        Args:
          inputs: input Tensor, 2D, batch x num_units.
          state: if `state_is_tuple` is False, this must be a state Tensor,
            `2-D, batch x state_size`.  If `state_is_tuple` is True, this must be a
            tuple of state Tensors, both `2-D`, with column sizes `c_state` and
            `m_state`.
          scope: VariableScope for the created subgraph; defaults to "lstm_cell".
    
        Returns:
          A tuple containing:
    
          - A `2-D, [batch x output_dim]`, Tensor representing the output of the
            LSTM after reading `inputs` when previous state was `state`.
            Here output_dim is:
               num_proj if num_proj was set,
               num_units otherwise.
          - Tensor(s) representing the new state of LSTM after reading `inputs` when
            the previous state was `state`.  Same type and shape(s) as `state`.
    
        Raises:
          ValueError: If input size cannot be inferred from inputs via
            static shape inference.
Run one step of LSTM . Args : inputs : input Tensor 2D batch x num_units . state : if state_is_tuple is False this must be a state Tensor 2 - D batch x state_size . If state_is_tuple is True this must be a tuple of state Tensors both 2 - D with column sizes c_state and m_state . scope : VariableScope for the created subgraph ; defaults to lstm_cell . Returns : A tuple containing : - A 2 - D [ batch x output_dim ] Tensor representing the output of the LSTM after reading inputs when previous state was state . Here output_dim is : num_proj if num_proj was set num_units otherwise . - Tensor ( s ) representing the new state of LSTM after reading inputs when the previous state was state . Same type and shape ( s ) as state . Raises : ValueError : If input size cannot be inferred from inputs via static shape inference .
def __call__(self, inputs, state, scope=None):
        """Run one step of LSTM.
    
        Args:
          inputs: input Tensor, 2D, batch x num_units.
          state: if `state_is_tuple` is False, this must be a state Tensor,
            `2-D, batch x state_size`.  If `state_is_tuple` is True, this must be a
            tuple of state Tensors, both `2-D`, with column sizes `c_state` and
            `m_state`.
          scope: VariableScope for the created subgraph; defaults to "lstm_cell".
    
        Returns:
          A tuple containing:
    
          - A `2-D, [batch x output_dim]`, Tensor representing the output of the
            LSTM after reading `inputs` when previous state was `state`.
            Here output_dim is:
               num_proj if num_proj was set,
               num_units otherwise.
          - Tensor(s) representing the new state of LSTM after reading `inputs` when
            the previous state was `state`.  Same type and shape(s) as `state`.
    
        Raises:
          ValueError: If input size cannot be inferred from inputs via
            static shape inference.
        """
        num_proj = self._num_units if self._num_proj is None else self._num_proj
        
        if self._state_is_tuple:
            (c_prev, m_prev) = state
        else:
            c_prev = array_ops.slice(state, [0, 0], [-1, self._num_units])
            m_prev = array_ops.slice(state, [0, self._num_units], [-1, num_proj])
        
        dtype = inputs.dtype
        input_size = inputs.get_shape().with_rank(2)[1]
        if input_size.value is None:
            raise ValueError("Could not infer input size from inputs.get_shape()[-1]")
        with vs.variable_scope(scope or "lstm_cell",
                               initializer=self._initializer) as unit_scope:
            if self._num_unit_shards is not None:
                unit_scope.set_partitioner(
                    partitioned_variables.fixed_size_partitioner(
                        self._num_unit_shards))
            
            i_size = input_size.value - 1  # -1 to extract time
            times = array_ops.slice(inputs, [0, i_size], [-1, 1])
            filtered_inputs = array_ops.slice(inputs, [0, 0], [-1, i_size])
            
            # --------------------------------------- #
            # ------------- PHASED LSTM ------------- #
            # ---------------- BEGIN ---------------- #
            # --------------------------------------- #
            
            tau = vs.get_variable(
                "T", shape=[self._num_units],
                initializer=random_exp_initializer(0,
                                                   self.tau_init) if not self.manual_set else init_ops.constant_initializer(
                    self.tau_init),
                trainable=self.trainable, dtype=dtype)
            
            r_on = vs.get_variable(
                "R", shape=[self._num_units],
                initializer=init_ops.constant_initializer(self.r_on_init),
                trainable=self.trainable, dtype=dtype)
            
            s = vs.get_variable(
                "S", shape=[self._num_units],
                initializer=init_ops.random_uniform_initializer(0.,
                                                                tau.initialized_value()) if not self.manual_set else init_ops.constant_initializer(
                    0.),
                trainable=self.trainable, dtype=dtype)
            # for backward compatibility (v < 0.12.0) use the following line instead of the above
            # initializer = init_ops.random_uniform_initializer(0., tau), dtype = dtype)
            
            tau_broadcast = tf.expand_dims(tau, axis=0)
            r_on_broadcast = tf.expand_dims(r_on, axis=0)
            s_broadcast = tf.expand_dims(s, axis=0)
            
            r_on_broadcast = tf.abs(r_on_broadcast)
            tau_broadcast = tf.abs(tau_broadcast)
            times = tf.tile(times, [1, self._num_units])
            
            # calculate kronos gate
            phi = tf.div(tf.mod(tf.mod(times - s_broadcast, tau_broadcast) + tau_broadcast, tau_broadcast),
                         tau_broadcast)
            is_up = tf.less(phi, (r_on_broadcast * 0.5))
            is_down = tf.logical_and(tf.less(phi, r_on_broadcast), tf.logical_not(is_up))
            
            k = tf.where(is_up, phi / (r_on_broadcast * 0.5),
                         tf.where(is_down, 2. - 2. * (phi / r_on_broadcast), self.alpha * phi))
            
            # --------------------------------------- #
            # ------------- PHASED LSTM ------------- #
            # ----------------- END ----------------- #
            # --------------------------------------- #
            
            # i = input_gate, j = new_input, f = forget_gate, o = output_gate
            lstm_matrix = _linear([filtered_inputs, m_prev], 4 * self._num_units, bias=True,
                                  scope=scope)
            i, j, f, o = array_ops.split(
                value=lstm_matrix, num_or_size_splits=4, axis=1)
            
            # Diagonal connections
            if self._use_peepholes:
                with vs.variable_scope(unit_scope) as projection_scope:
                    if self._num_unit_shards is not None:
                        projection_scope.set_partitioner(None)
                    w_f_diag = vs.get_variable(
                        "w_f_diag", shape=[self._num_units], dtype=dtype)
                    w_i_diag = vs.get_variable(
                        "w_i_diag", shape=[self._num_units], dtype=dtype)
                    w_o_diag = vs.get_variable(
                        "w_o_diag", shape=[self._num_units], dtype=dtype)
            
            if self._use_peepholes:
                c = (sigmoid(f + self._forget_bias + w_f_diag * c_prev) * c_prev +
                     sigmoid(i + w_i_diag * c_prev) * self._activation(j))
            else:
                c = (sigmoid(f + self._forget_bias) * c_prev + sigmoid(i) *
                     self._activation(j))
            
            if self._cell_clip is not None:
                # pylint: disable=invalid-unary-operand-type
                c = clip_ops.clip_by_value(c, -self._cell_clip, self._cell_clip)
                # pylint: enable=invalid-unary-operand-type
            
            if self._use_peepholes:
                m = sigmoid(o + w_o_diag * c) * self._activation(c)
            else:
                m = sigmoid(o) * self._activation(c)
            
            if self._num_proj is not None:
                with vs.variable_scope("projection") as proj_scope:
                    if self._num_proj_shards is not None:
                        proj_scope.set_partitioner(
                            partitioned_variables.fixed_size_partitioner(
                                self._num_proj_shards))
                    m = _linear(m, self._num_proj, bias=False, scope=scope)
                
                if self._proj_clip is not None:
                    # pylint: disable=invalid-unary-operand-type
                    m = clip_ops.clip_by_value(m, -self._proj_clip, self._proj_clip)
                    # pylint: enable=invalid-unary-operand-type
            
            # APPLY KRONOS GATE
            c = k * c + (1. - k) * c_prev
            m = k * m + (1. - k) * m_prev
            # END KRONOS GATE
        
        new_state = (LSTMStateTuple(c, m) if self._state_is_tuple else
                     array_ops.concat([c, m], 1))
        return m, new_state
def __call__ ( self , inputs , state , scope = None ) : num_proj = self . _num_units if self . _num_proj is None else self . _num_proj if self . _state_is_tuple : ( c_prev , m_prev ) = state else : c_prev = array_ops . slice ( state , [ 0 , 0 ] , [ - 1 , self . _num_units ] ) m_prev = array_ops . slice ( state , [ 0 , self . _num_units ] , [ - 1 , num_proj ] ) dtype = inputs . dtype input_size = inputs . get_shape ( ) . with_rank ( 2 ) [ 1 ] if input_size . value is None : raise ValueError ( "Could not infer input size from inputs.get_shape()[-1]" ) with vs . variable_scope ( scope or "lstm_cell" , initializer = self . _initializer ) as unit_scope : if self . _num_unit_shards is not None : unit_scope . set_partitioner ( partitioned_variables . fixed_size_partitioner ( self . _num_unit_shards ) ) i_size = input_size . value - 1 # -1 to extract time times = array_ops . slice ( inputs , [ 0 , i_size ] , [ - 1 , 1 ] ) filtered_inputs = array_ops . slice ( inputs , [ 0 , 0 ] , [ - 1 , i_size ] ) # --------------------------------------- # # ------------- PHASED LSTM ------------- # # ---------------- BEGIN ---------------- # # --------------------------------------- # tau = vs . get_variable ( "T" , shape = [ self . _num_units ] , initializer = random_exp_initializer ( 0 , self . tau_init ) if not self . manual_set else init_ops . constant_initializer ( self . tau_init ) , trainable = self . trainable , dtype = dtype ) r_on = vs . get_variable ( "R" , shape = [ self . _num_units ] , initializer = init_ops . constant_initializer ( self . r_on_init ) , trainable = self . trainable , dtype = dtype ) s = vs . get_variable ( "S" , shape = [ self . _num_units ] , initializer = init_ops . random_uniform_initializer ( 0. , tau . initialized_value ( ) ) if not self . manual_set else init_ops . constant_initializer ( 0. ) , trainable = self . trainable , dtype = dtype ) # for backward compatibility (v < 0.12.0) use the following line instead of the above # initializer = init_ops.random_uniform_initializer(0., tau), dtype = dtype) tau_broadcast = tf . expand_dims ( tau , axis = 0 ) r_on_broadcast = tf . expand_dims ( r_on , axis = 0 ) s_broadcast = tf . expand_dims ( s , axis = 0 ) r_on_broadcast = tf . abs ( r_on_broadcast ) tau_broadcast = tf . abs ( tau_broadcast ) times = tf . tile ( times , [ 1 , self . _num_units ] ) # calculate kronos gate phi = tf . div ( tf . mod ( tf . mod ( times - s_broadcast , tau_broadcast ) + tau_broadcast , tau_broadcast ) , tau_broadcast ) is_up = tf . less ( phi , ( r_on_broadcast * 0.5 ) ) is_down = tf . logical_and ( tf . less ( phi , r_on_broadcast ) , tf . logical_not ( is_up ) ) k = tf . where ( is_up , phi / ( r_on_broadcast * 0.5 ) , tf . where ( is_down , 2. - 2. * ( phi / r_on_broadcast ) , self . alpha * phi ) ) # --------------------------------------- # # ------------- PHASED LSTM ------------- # # ----------------- END ----------------- # # --------------------------------------- # # i = input_gate, j = new_input, f = forget_gate, o = output_gate lstm_matrix = _linear ( [ filtered_inputs , m_prev ] , 4 * self . _num_units , bias = True , scope = scope ) i , j , f , o = array_ops . split ( value = lstm_matrix , num_or_size_splits = 4 , axis = 1 ) # Diagonal connections if self . _use_peepholes : with vs . variable_scope ( unit_scope ) as projection_scope : if self . _num_unit_shards is not None : projection_scope . set_partitioner ( None ) w_f_diag = vs . get_variable ( "w_f_diag" , shape = [ self . _num_units ] , dtype = dtype ) w_i_diag = vs . get_variable ( "w_i_diag" , shape = [ self . _num_units ] , dtype = dtype ) w_o_diag = vs . get_variable ( "w_o_diag" , shape = [ self . _num_units ] , dtype = dtype ) if self . _use_peepholes : c = ( sigmoid ( f + self . _forget_bias + w_f_diag * c_prev ) * c_prev + sigmoid ( i + w_i_diag * c_prev ) * self . _activation ( j ) ) else : c = ( sigmoid ( f + self . _forget_bias ) * c_prev + sigmoid ( i ) * self . _activation ( j ) ) if self . _cell_clip is not None : # pylint: disable=invalid-unary-operand-type c = clip_ops . clip_by_value ( c , - self . _cell_clip , self . _cell_clip ) # pylint: enable=invalid-unary-operand-type if self . _use_peepholes : m = sigmoid ( o + w_o_diag * c ) * self . _activation ( c ) else : m = sigmoid ( o ) * self . _activation ( c ) if self . _num_proj is not None : with vs . variable_scope ( "projection" ) as proj_scope : if self . _num_proj_shards is not None : proj_scope . set_partitioner ( partitioned_variables . fixed_size_partitioner ( self . _num_proj_shards ) ) m = _linear ( m , self . _num_proj , bias = False , scope = scope ) if self . _proj_clip is not None : # pylint: disable=invalid-unary-operand-type m = clip_ops . clip_by_value ( m , - self . _proj_clip , self . _proj_clip ) # pylint: enable=invalid-unary-operand-type # APPLY KRONOS GATE c = k * c + ( 1. - k ) * c_prev m = k * m + ( 1. - k ) * m_prev # END KRONOS GATE new_state = ( LSTMStateTuple ( c , m ) if self . _state_is_tuple else array_ops . concat ( [ c , m ] , 1 ) ) return m , new_state