σ
VΎχXc           @` s7  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d   Z d e d  Z d e d  Z d e e d  Z d d e d  Z d d d d  Z d S(   s   LSTM layers for sequences.i    (   t   absolute_import(   t   division(   t   print_function(   t   xrange(   t	   variables(   t   core_rnn_cell_impl(   t   constant_op(   t	   array_ops(   t   math_ops(   t   nn_ops(   t
   random_ops(   t   rnn(   t   variable_scopec         C` s   |  j    j   S(   N(   t	   get_shapet   as_list(   t   tensor(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   _shape!   s    c         C` s  t  j  | d |  g  t |   \ } } } t j | d t } t j | | j g  } g  }	 t j |   }
 | r t	 t
 |
   }
 n  xY t |  D]K } | d k r½ t  j   j   n  | |
 | |  \ } } |	 | g 7}	 q W| rt	 t
 |	   }	 n  t j |	  } | SWd QXd S(   sZ  Run an LSTM, either forward or backward.

  This is a 1D LSTM implementation using unrolling and the TensorFlow
  LSTM op.

  Args:
    inputs: input sequence (length, batch_size, ninput)
    noutput: depth of output
    scope: optional scope name
    reverse: run LSTM in reverse

  Returns:
    Output sequence (length, batch_size, noutput)

  t   SeqLstmUnrolledt   state_is_tuplei    N(   R   R   R   t   BasicLSTMCellt   FalseR   t   zerost
   state_sizet   unstackt   listt   reversedR   t   get_variable_scopet   reuse_variablest   stack(   t   inputst   noutputt   scopet   reverset   lengtht
   batch_sizet   _t	   lstm_cellt   statet   output_ut   inputs_ut   it   outputt   outputs(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   ndlstm_base_unrolled%   s"    c         C` sύ   t  j  | d |  g  ί t |   \ } } } t j | d t } t j | | j g  } t |  j	   d  } t
 j t j | g |   }	 | r­ t j |  d g  }  n  t j | |  |	 | d t \ }
 } | rο t j |
 d g  }
 n  |
 SWd QXd S(   s[  Run an LSTM, either forward or backward.

  This is a 1D LSTM implementation using dynamic_rnn and
  the TensorFlow LSTM op.

  Args:
    inputs: input sequence (length, batch_size, ninput)
    noutput: depth of output
    scope: optional scope name
    reverse: run LSTM in reverse

  Returns:
    Output sequence (length, batch_size, noutput)
  t   SeqLstmR   i    t
   time_majorN(   R   R   R   R   R   R   R   R   t   intR   R   t   to_int64t   fillt
   reverse_v2R   t   dynamic_rnnt   True(   R   R   R   R    R#   R"   R$   R%   t   sequence_lengtht   sequence_lengthsR*   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   ndlstm_base_dynamicH   s    c         C` s<   | r t  |  | d | d | St |  | d | d | Sd S(   s9  Implements a 1D LSTM, either forward or backward.

  This is a base case for multidimensional LSTM implementations, which
  tend to be used differently from sequence-to-sequence
  implementations.  For general 1D sequence to sequence
  transformations, you may want to consider another implementation
  from TF slim.

  Args:
    inputs: input sequence (length, batch_size, ninput)
    noutput: depth of output
    scope: optional scope name
    reverse: run LSTM in reverse
    dynamic: use dynamic_rnn

  Returns:
    Output sequence (length, batch_size, noutput)

  R   R    N(   R6   R+   (   R   R   R   R    t   dynamic(    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   ndlstm_basei   s    c      	   C` s   t  j  | d |  g  β t |   \ } } } t j | d t } t j | | j g  }	 t j |   }
 | r t	 t
 |
   }
 n  xL t |  D]> } | d k r· t  j   j   n  | |
 | |	  \ } }	 q Wt j | | | g d | } | SWd QXd S(   s6  Run an LSTM across all steps and returns only the final state.

  Args:
    inputs: (length, batch_size, depth) tensor
    noutput: size of output vector
    scope: optional scope name
    name: optional name for output tensor
    reverse: run in reverse

  Returns:
    Batch of size (batch_size, noutput).
  t   SequenceToFinalR   i    t   nameN(   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   reshape(   R   R   R   R:   R    R!   R"   R#   t   lstmR%   R'   R(   R)   R*   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   sequence_to_final   s    c         C` s8  t  |   \ } } } t j |   } g  }	 t j | d |  g  π t j d | | g d d }
 t j d d | g } t j	 d d |
 } t j	 d d | } xr t
 |  D]d } t j | d	 | | g  @ t j | | | | d
 | } t j |  } |	 | g 7}	 Wd QXq± Wt j |	 d
 | } Wd QX| S(   sV  Run a softmax layer over all the time steps of an input sequence.

  Args:
    inputs: (length, batch_size, depth) tensor
    noutput: output depth
    scope: optional scope name
    name: optional name for output tensor
    linear_name: name for linear (pre-softmax) output

  Returns:
    A tensor of size (length, batch_size, noutput).

  t   SequenceSoftmaxi    t   stddevgΉ?t   shapet   weightst   initializert   biasest   SequenceSoftmaxStepR:   N(   R   R   R   R   R
   t   truncated_normalR   t   constantR   t   model_variableR   R	   t	   xw_plus_bt   softmaxR   (   R   R   R   R:   t   linear_nameR!   R#   t   ninputsR'   R&   t	   initial_wt	   initial_bt   wt   bR(   t   linearR)   R*   (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   sequence_softmax‘   s     N(   t   __doc__t
   __future__R    R   R   t	   six.movesR   t'   tensorflow.contrib.framework.python.opsR   t!   tensorflow.contrib.rnn.python.opsR   t   tensorflow.python.frameworkR   t   tensorflow.python.opsR   R   R	   R
   R   R   R   t   NoneR   R+   R6   R3   R8   R=   RQ   (    (    (    si   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/ndlstm/python/lstm1d.pyt   <module>   s&   	#!