0

In order to differentiate LSTMs, I wish to give a name to the BasicLSTMCell variable in my code. But it reported the following error:

    num_units=self.config.num_lstm_units, state_is_tuple=True, name="some_basic_lstm")
TypeError: __init__() got an unexpected keyword argument 'name'

And I found in the library of my tensorflow installation. Int the file rnn_cell_impl.py:

class BasicLSTMCell(RNNCell):
  """Basic LSTM recurrent network cell.

  The implementation is based on: http://arxiv.org/abs/1409.2329.

  We add forget_bias (default: 1) to the biases of the forget gate in order to
  reduce the scale of forgetting in the beginning of the training.

  It does not allow cell clipping, a projection layer, and does not
  use peep-hole connections: it is the basic baseline.

  For advanced models, please use the full @{tf.nn.rnn_cell.LSTMCell}
  that follows.
  """

  def __init__(self, num_units, forget_bias=1.0,
               state_is_tuple=True, activation=None, reuse=None):
    """Initialize the basic LSTM cell.

    Args:
      num_units: int, The number of units in the LSTM cell.
      forget_bias: float, The bias added to forget gates (see above).
        Must set to `0.0` manually when restoring from CudnnLSTM-trained
        checkpoints.
      state_is_tuple: If True, accepted and returned states are 2-tuples of
        the `c_state` and `m_state`.  If False, they are concatenated
        along the column axis.  The latter behavior will soon be deprecated.
      activation: Activation function of the inner states.  Default: `tanh`.
      reuse: (optional) Python boolean describing whether to reuse variables
        in an existing scope.  If not `True`, and the existing scope already has
        the given variables, an error is raised.

Is it a bug in my version of tensorflow? How can I give it a "name"?

piratesailor
  • 305
  • 5
  • 15
  • In the newer version, you can give a name to the BasicLSTM cell. Look [here](https://www.tensorflow.org/versions/master/api_docs/python/tf/contrib/rnn/BasicLSTMCell) – pauli Mar 26 '18 at 04:14
  • @ashwinids The old version cannot control its name? Isn't it a bug? I have installed the old version. It is impossible to shift to a newer version. Is there a way to control its name? – piratesailor Mar 26 '18 at 05:46

1 Answers1

0

I think @aswinids provided the best answer here in comments, but let me explain why it is should not be considered a bug. An LSTM cell is comprised of at least 4 variables (there are a few others used for control flow and such). There are 4 sub-network operations that occur in an LSTM. The diagram below from Colah's blog illustrates the internals of an LSTM cell (http://colah.github.io/posts/2015-08-Understanding-LSTMs/):

enter image description here

Each of the yellow boxes has a set of weights assigned to it and is effectively a single layer neural network operation (piped together in an interesting way, defined by the LSTM architecture).

A good approach to naming these would then be tf.variable_scope('some_name') such that all 4 of the variables defined in the LSTM have a common base naming structure such as:

lstm_cell/f_t
lstm_cell/i_t
lstm_cell/C_t
lstm_cell/o_t

I suspect that previously they just did this and hard coded lstm_cell or whatever name they used as the prefix for all the variables under the LSMT cell. In the later versions as @ashwinids points out, there is a name variable and I suspect that just replaced lstm_cell I used in the example here.

David Parks
  • 30,789
  • 47
  • 185
  • 328
  • The situation I encountered here is that my version of TensorFlow is an old one say v1.4. And I have the following code: lstmcell1 = tf.contrib.rnn.BasicLSTMCell( num_units=some_lstM_units) ... lstmcell2 = tf.contrib.rnn.BasicLSTMCell( num_units=some_lstM_units) Then are these two lstms the same one or they are the two different ones? Do they have the same name or different names? – piratesailor Mar 26 '18 at 22:58
  • Take a look and double check (link below). I am almost certain that the second LSTM will be the same name with something like an `_1` attached to the name, that's pretty typical TF style. If you want to give them easier to find names use `with tf.variable_scope('LSTM1'):` before the definition. https://stackoverflow.com/questions/36883949/in-tensorflow-get-the-names-of-all-the-tensors-in-a-graph – David Parks Mar 26 '18 at 23:10