We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
ValueError: Cell returns tuple of states, but the flag state_is_tuple is not set. State size is: (LSTMStateTuple(c=128, h=128), LSTMStateTuple(c=128, h=128))
Hey guys,

I've just set up my conda environment and packages. When I running the bazel test //magenta/... command, the test //magenta/models/shared:events_rnn_graph_test  failed. I am new to this project so hopefully someone could point me to the right direction! For your info, I have installed all the required packages according to setup.py, and confirmed installation with 'pip freeze' and 'conda list' command. 

Thanks in advance!
Simon 

Bellow is the error message in the log file:

`ERROR: testBuildGraphWithAttention (__main__.EventSequenceRNNGraphTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph_test.py", line 58, in testBuildGraphWithAttention
    'train', self.config, sequence_example_file_paths=['test'])
  File "/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph.py", line 98, in build_graph
    attn_length=hparams.attn_length)
  File "/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph.py", line 47, in make_rnn_cell
    cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)
  File "/Users/simonttk/anaconda2/envs/magenta/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 1077, in __init__
    % str(cell.state_size))
ValueError: Cell returns tuple of states, but the flag state_is_tuple is not set. State size is: (LSTMStateTuple(c=128, h=128), LSTMStateTuple(c=128, h=128))`




--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `magenta/models/shared/events_rnn_graph.py`
Content:
```
1 # Copyright 2016 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Provides function to build an event sequence RNN model's graph."""
15 
16 # internal imports
17 import tensorflow as tf
18 import magenta
19 
20 
21 def make_rnn_cell(rnn_layer_sizes,
22                   dropout_keep_prob=1.0,
23                   attn_length=0,
24                   base_cell=tf.contrib.rnn.BasicLSTMCell):
25   """Makes a RNN cell from the given hyperparameters.
26 
27   Args:
28     rnn_layer_sizes: A list of integer sizes (in units) for each layer of the
29         RNN.
30     dropout_keep_prob: The float probability to keep the output of any given
31         sub-cell.
32     attn_length: The size of the attention vector.
33     base_cell: The base tf.contrib.rnn.RNNCell to use for sub-cells.
34 
35   Returns:
36       A tf.contrib.rnn.MultiRNNCell based on the given hyperparameters.
37   """
38   cells = []
39   for num_units in rnn_layer_sizes:
40     cell = base_cell(num_units)
41     cell = tf.contrib.rnn.DropoutWrapper(
42         cell, output_keep_prob=dropout_keep_prob)
43     cells.append(cell)
44 
45   cell = tf.contrib.rnn.MultiRNNCell(cells)
46   if attn_length:
47     cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)
48 
49   return cell
50 
51 
52 def build_graph(mode, config, sequence_example_file_paths=None):
53   """Builds the TensorFlow graph.
54 
55   Args:
56     mode: 'train', 'eval', or 'generate'. Only mode related ops are added to
57         the graph.
58     config: An EventSequenceRnnConfig containing the encoder/decoder and HParams
59         to use.
60     sequence_example_file_paths: A list of paths to TFRecord files containing
61         tf.train.SequenceExample protos. Only needed for training and
62         evaluation. May be a sharded file of the form.
63 
64   Returns:
65     A tf.Graph instance which contains the TF ops.
66 
67   Raises:
68     ValueError: If mode is not 'train', 'eval', or 'generate'.
69   """
70   if mode not in ('train', 'eval', 'generate'):
71     raise ValueError("The mode parameter must be 'train', 'eval', "
72                      "or 'generate'. The mode parameter was: %s" % mode)
73 
74   hparams = config.hparams
75   encoder_decoder = config.encoder_decoder
76 
77   tf.logging.info('hparams = %s', hparams.values())
78 
79   input_size = encoder_decoder.input_size
80   num_classes = encoder_decoder.num_classes
81   no_event_label = encoder_decoder.default_event_label
82 
83   with tf.Graph().as_default() as graph:
84     inputs, labels, lengths, = None, None, None
85 
86     if mode == 'train' or mode == 'eval':
87       inputs, labels, lengths = magenta.common.get_padded_batch(
88           sequence_example_file_paths, hparams.batch_size, input_size)
89 
90     elif mode == 'generate':
91       inputs = tf.placeholder(tf.float32, [hparams.batch_size, None,
92                                            input_size])
93 
94     cell = make_rnn_cell(
95         hparams.rnn_layer_sizes,
96         dropout_keep_prob=(
97             1.0 if mode == 'generate' else hparams.dropout_keep_prob),
98         attn_length=hparams.attn_length)
99 
100     initial_state = cell.zero_state(hparams.batch_size, tf.float32)
101 
102     outputs, final_state = tf.nn.dynamic_rnn(
103         cell, inputs, initial_state=initial_state, swap_memory=True)
104 
105     outputs_flat = tf.reshape(outputs, [-1, cell.output_size])
106     logits_flat = tf.contrib.layers.linear(outputs_flat, num_classes)
107 
108     if mode == 'train' or mode == 'eval':
109       labels_flat = tf.reshape(labels, [-1])
110       mask = tf.sequence_mask(lengths)
111       if hparams.skip_first_n_losses:
112         skip = tf.minimum(lengths, hparams.skip_first_n_losses)
113         skip_mask = tf.sequence_mask(skip, maxlen=tf.reduce_max(lengths))
114         mask = tf.logical_and(mask, tf.logical_not(skip_mask))
115       mask = tf.cast(mask, tf.float32)
116       mask_flat = tf.reshape(mask, [-1])
117 
118       num_logits = tf.to_float(tf.reduce_sum(lengths))
119 
120       with tf.control_dependencies(
121           [tf.Assert(tf.greater(num_logits, 0.), [num_logits])]):
122         softmax_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
123             labels=labels_flat, logits=logits_flat)
124       loss = tf.reduce_sum(mask_flat * softmax_cross_entropy) / num_logits
125       perplexity = (tf.reduce_sum(mask_flat * tf.exp(softmax_cross_entropy)) /
126                     num_logits)
127 
128       correct_predictions = tf.to_float(
129           tf.nn.in_top_k(logits_flat, labels_flat, 1)) * mask_flat
130       accuracy = tf.reduce_sum(correct_predictions) / num_logits * 100
131 
132       event_positions = (
133           tf.to_float(tf.not_equal(labels_flat, no_event_label)) * mask_flat)
134       event_accuracy = (
135           tf.reduce_sum(tf.multiply(correct_predictions, event_positions)) /
136           tf.reduce_sum(event_positions) * 100)
137 
138       no_event_positions = (
139           tf.to_float(tf.equal(labels_flat, no_event_label)) * mask_flat)
140       no_event_accuracy = (
141           tf.reduce_sum(tf.multiply(correct_predictions, no_event_positions)) /
142           tf.reduce_sum(no_event_positions) * 100)
143 
144       global_step = tf.Variable(0, trainable=False, name='global_step')
145 
146       tf.add_to_collection('loss', loss)
147       tf.add_to_collection('perplexity', perplexity)
148       tf.add_to_collection('accuracy', accuracy)
149       tf.add_to_collection('global_step', global_step)
150 
151       summaries = [
152           tf.summary.scalar('loss', loss),
153           tf.summary.scalar('perplexity', perplexity),
154           tf.summary.scalar('accuracy', accuracy),
155           tf.summary.scalar(
156               'event_accuracy', event_accuracy),
157           tf.summary.scalar(
158               'no_event_accuracy', no_event_accuracy),
159       ]
160 
161       if mode == 'train':
162         learning_rate = tf.train.exponential_decay(
163             hparams.initial_learning_rate, global_step, hparams.decay_steps,
164             hparams.decay_rate, staircase=True, name='learning_rate')
165 
166         opt = tf.train.AdamOptimizer(learning_rate)
167         params = tf.trainable_variables()
168         gradients = tf.gradients(loss, params)
169         clipped_gradients, _ = tf.clip_by_global_norm(gradients,
170                                                       hparams.clip_norm)
171         train_op = opt.apply_gradients(zip(clipped_gradients, params),
172                                        global_step)
173         tf.add_to_collection('learning_rate', learning_rate)
174         tf.add_to_collection('train_op', train_op)
175 
176         summaries.append(tf.summary.scalar(
177             'learning_rate', learning_rate))
178 
179       if mode == 'eval':
180         summary_op = tf.summary.merge(summaries)
181         tf.add_to_collection('summary_op', summary_op)
182 
183     elif mode == 'generate':
184       temperature = tf.placeholder(tf.float32, [])
185       softmax_flat = tf.nn.softmax(
186           tf.div(logits_flat, tf.fill([num_classes], temperature)))
187       softmax = tf.reshape(softmax_flat, [hparams.batch_size, -1, num_classes])
188 
189       tf.add_to_collection('inputs', inputs)
190       tf.add_to_collection('initial_state', initial_state)
191       tf.add_to_collection('final_state', final_state)
192       tf.add_to_collection('temperature', temperature)
193       tf.add_to_collection('softmax', softmax)
194 
195   return graph
196
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/magenta/models/shared/events_rnn_graph.py b/magenta/models/shared/events_rnn_graph.py
--- a/magenta/models/shared/events_rnn_graph.py
+++ b/magenta/models/shared/events_rnn_graph.py
@@ -44,7 +44,8 @@
 
   cell = tf.contrib.rnn.MultiRNNCell(cells)
   if attn_length:
-    cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)
+    cell = tf.contrib.rnn.AttentionCellWrapper(
+        cell, attn_length, state_is_tuple=True)
 
   return cell

{"golden_diff": "diff --git a/magenta/models/shared/events_rnn_graph.py b/magenta/models/shared/events_rnn_graph.py\n--- a/magenta/models/shared/events_rnn_graph.py\n+++ b/magenta/models/shared/events_rnn_graph.py\n@@ -44,7 +44,8 @@\n \n   cell = tf.contrib.rnn.MultiRNNCell(cells)\n   if attn_length:\n-    cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)\n+    cell = tf.contrib.rnn.AttentionCellWrapper(\n+        cell, attn_length, state_is_tuple=True)\n \n   return cell\n", "issue": "ValueError: Cell returns tuple of states, but the flag state_is_tuple is not set. State size is: (LSTMStateTuple(c=128, h=128), LSTMStateTuple(c=128, h=128))\nHey guys,\r\n\r\nI've just set up my conda environment and packages. When I running the bazel test //magenta/... command, the test //magenta/models/shared:events_rnn_graph_test  failed. I am new to this project so hopefully someone could point me to the right direction! For your info, I have installed all the required packages according to setup.py, and confirmed installation with 'pip freeze' and 'conda list' command. \r\n\r\nThanks in advance!\r\nSimon \r\n\r\nBellow is the error message in the log file:\r\n\r\n`ERROR: testBuildGraphWithAttention (__main__.EventSequenceRNNGraphTest)\r\n----------------------------------------------------------------------\r\nTraceback (most recent call last):\r\n  File \"/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph_test.py\", line 58, in testBuildGraphWithAttention\r\n    'train', self.config, sequence_example_file_paths=['test'])\r\n  File \"/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph.py\", line 98, in build_graph\r\n    attn_length=hparams.attn_length)\r\n  File \"/private/var/tmp/_bazel_simonttk/2d57163c72209284de52b06652358cc7/execroot/magenta/bazel-out/local-opt/bin/magenta/models/shared/events_rnn_graph_test.runfiles/__main__/magenta/models/shared/events_rnn_graph.py\", line 47, in make_rnn_cell\r\n    cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)\r\n  File \"/Users/simonttk/anaconda2/envs/magenta/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py\", line 1077, in __init__\r\n    % str(cell.state_size))\r\nValueError: Cell returns tuple of states, but the flag state_is_tuple is not set. State size is: (LSTMStateTuple(c=128, h=128), LSTMStateTuple(c=128, h=128))`\r\n\r\n\r\n\n", "before_files": [{"content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Provides function to build an event sequence RNN model's graph.\"\"\"\n\n# internal imports\nimport tensorflow as tf\nimport magenta\n\n\ndef make_rnn_cell(rnn_layer_sizes,\n                  dropout_keep_prob=1.0,\n                  attn_length=0,\n                  base_cell=tf.contrib.rnn.BasicLSTMCell):\n  \"\"\"Makes a RNN cell from the given hyperparameters.\n\n  Args:\n    rnn_layer_sizes: A list of integer sizes (in units) for each layer of the\n        RNN.\n    dropout_keep_prob: The float probability to keep the output of any given\n        sub-cell.\n    attn_length: The size of the attention vector.\n    base_cell: The base tf.contrib.rnn.RNNCell to use for sub-cells.\n\n  Returns:\n      A tf.contrib.rnn.MultiRNNCell based on the given hyperparameters.\n  \"\"\"\n  cells = []\n  for num_units in rnn_layer_sizes:\n    cell = base_cell(num_units)\n    cell = tf.contrib.rnn.DropoutWrapper(\n        cell, output_keep_prob=dropout_keep_prob)\n    cells.append(cell)\n\n  cell = tf.contrib.rnn.MultiRNNCell(cells)\n  if attn_length:\n    cell = tf.contrib.rnn.AttentionCellWrapper(cell, attn_length)\n\n  return cell\n\n\ndef build_graph(mode, config, sequence_example_file_paths=None):\n  \"\"\"Builds the TensorFlow graph.\n\n  Args:\n    mode: 'train', 'eval', or 'generate'. Only mode related ops are added to\n        the graph.\n    config: An EventSequenceRnnConfig containing the encoder/decoder and HParams\n        to use.\n    sequence_example_file_paths: A list of paths to TFRecord files containing\n        tf.train.SequenceExample protos. Only needed for training and\n        evaluation. May be a sharded file of the form.\n\n  Returns:\n    A tf.Graph instance which contains the TF ops.\n\n  Raises:\n    ValueError: If mode is not 'train', 'eval', or 'generate'.\n  \"\"\"\n  if mode not in ('train', 'eval', 'generate'):\n    raise ValueError(\"The mode parameter must be 'train', 'eval', \"\n                     \"or 'generate'. The mode parameter was: %s\" % mode)\n\n  hparams = config.hparams\n  encoder_decoder = config.encoder_decoder\n\n  tf.logging.info('hparams = %s', hparams.values())\n\n  input_size = encoder_decoder.input_size\n  num_classes = encoder_decoder.num_classes\n  no_event_label = encoder_decoder.default_event_label\n\n  with tf.Graph().as_default() as graph:\n    inputs, labels, lengths, = None, None, None\n\n    if mode == 'train' or mode == 'eval':\n      inputs, labels, lengths = magenta.common.get_padded_batch(\n          sequence_example_file_paths, hparams.batch_size, input_size)\n\n    elif mode == 'generate':\n      inputs = tf.placeholder(tf.float32, [hparams.batch_size, None,\n                                           input_size])\n\n    cell = make_rnn_cell(\n        hparams.rnn_layer_sizes,\n        dropout_keep_prob=(\n            1.0 if mode == 'generate' else hparams.dropout_keep_prob),\n        attn_length=hparams.attn_length)\n\n    initial_state = cell.zero_state(hparams.batch_size, tf.float32)\n\n    outputs, final_state = tf.nn.dynamic_rnn(\n        cell, inputs, initial_state=initial_state, swap_memory=True)\n\n    outputs_flat = tf.reshape(outputs, [-1, cell.output_size])\n    logits_flat = tf.contrib.layers.linear(outputs_flat, num_classes)\n\n    if mode == 'train' or mode == 'eval':\n      labels_flat = tf.reshape(labels, [-1])\n      mask = tf.sequence_mask(lengths)\n      if hparams.skip_first_n_losses:\n        skip = tf.minimum(lengths, hparams.skip_first_n_losses)\n        skip_mask = tf.sequence_mask(skip, maxlen=tf.reduce_max(lengths))\n        mask = tf.logical_and(mask, tf.logical_not(skip_mask))\n      mask = tf.cast(mask, tf.float32)\n      mask_flat = tf.reshape(mask, [-1])\n\n      num_logits = tf.to_float(tf.reduce_sum(lengths))\n\n      with tf.control_dependencies(\n          [tf.Assert(tf.greater(num_logits, 0.), [num_logits])]):\n        softmax_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(\n            labels=labels_flat, logits=logits_flat)\n      loss = tf.reduce_sum(mask_flat * softmax_cross_entropy) / num_logits\n      perplexity = (tf.reduce_sum(mask_flat * tf.exp(softmax_cross_entropy)) /\n                    num_logits)\n\n      correct_predictions = tf.to_float(\n          tf.nn.in_top_k(logits_flat, labels_flat, 1)) * mask_flat\n      accuracy = tf.reduce_sum(correct_predictions) / num_logits * 100\n\n      event_positions = (\n          tf.to_float(tf.not_equal(labels_flat, no_event_label)) * mask_flat)\n      event_accuracy = (\n          tf.reduce_sum(tf.multiply(correct_predictions, event_positions)) /\n          tf.reduce_sum(event_positions) * 100)\n\n      no_event_positions = (\n          tf.to_float(tf.equal(labels_flat, no_event_label)) * mask_flat)\n      no_event_accuracy = (\n          tf.reduce_sum(tf.multiply(correct_predictions, no_event_positions)) /\n          tf.reduce_sum(no_event_positions) * 100)\n\n      global_step = tf.Variable(0, trainable=False, name='global_step')\n\n      tf.add_to_collection('loss', loss)\n      tf.add_to_collection('perplexity', perplexity)\n      tf.add_to_collection('accuracy', accuracy)\n      tf.add_to_collection('global_step', global_step)\n\n      summaries = [\n          tf.summary.scalar('loss', loss),\n          tf.summary.scalar('perplexity', perplexity),\n          tf.summary.scalar('accuracy', accuracy),\n          tf.summary.scalar(\n              'event_accuracy', event_accuracy),\n          tf.summary.scalar(\n              'no_event_accuracy', no_event_accuracy),\n      ]\n\n      if mode == 'train':\n        learning_rate = tf.train.exponential_decay(\n            hparams.initial_learning_rate, global_step, hparams.decay_steps,\n            hparams.decay_rate, staircase=True, name='learning_rate')\n\n        opt = tf.train.AdamOptimizer(learning_rate)\n        params = tf.trainable_variables()\n        gradients = tf.gradients(loss, params)\n        clipped_gradients, _ = tf.clip_by_global_norm(gradients,\n                                                      hparams.clip_norm)\n        train_op = opt.apply_gradients(zip(clipped_gradients, params),\n                                       global_step)\n        tf.add_to_collection('learning_rate', learning_rate)\n        tf.add_to_collection('train_op', train_op)\n\n        summaries.append(tf.summary.scalar(\n            'learning_rate', learning_rate))\n\n      if mode == 'eval':\n        summary_op = tf.summary.merge(summaries)\n        tf.add_to_collection('summary_op', summary_op)\n\n    elif mode == 'generate':\n      temperature = tf.placeholder(tf.float32, [])\n      softmax_flat = tf.nn.softmax(\n          tf.div(logits_flat, tf.fill([num_classes], temperature)))\n      softmax = tf.reshape(softmax_flat, [hparams.batch_size, -1, num_classes])\n\n      tf.add_to_collection('inputs', inputs)\n      tf.add_to_collection('initial_state', initial_state)\n      tf.add_to_collection('final_state', final_state)\n      tf.add_to_collection('temperature', temperature)\n      tf.add_to_collection('softmax', softmax)\n\n  return graph\n", "path": "magenta/models/shared/events_rnn_graph.py"}], "after_files": [{"content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Provides function to build an event sequence RNN model's graph.\"\"\"\n\n# internal imports\nimport tensorflow as tf\nimport magenta\n\n\ndef make_rnn_cell(rnn_layer_sizes,\n                  dropout_keep_prob=1.0,\n                  attn_length=0,\n                  base_cell=tf.contrib.rnn.BasicLSTMCell):\n  \"\"\"Makes a RNN cell from the given hyperparameters.\n\n  Args:\n    rnn_layer_sizes: A list of integer sizes (in units) for each layer of the\n        RNN.\n    dropout_keep_prob: The float probability to keep the output of any given\n        sub-cell.\n    attn_length: The size of the attention vector.\n    base_cell: The base tf.contrib.rnn.RNNCell to use for sub-cells.\n\n  Returns:\n      A tf.contrib.rnn.MultiRNNCell based on the given hyperparameters.\n  \"\"\"\n  cells = []\n  for num_units in rnn_layer_sizes:\n    cell = base_cell(num_units)\n    cell = tf.contrib.rnn.DropoutWrapper(\n        cell, output_keep_prob=dropout_keep_prob)\n    cells.append(cell)\n\n  cell = tf.contrib.rnn.MultiRNNCell(cells)\n  if attn_length:\n    cell = tf.contrib.rnn.AttentionCellWrapper(\n        cell, attn_length, state_is_tuple=True)\n\n  return cell\n\n\ndef build_graph(mode, config, sequence_example_file_paths=None):\n  \"\"\"Builds the TensorFlow graph.\n\n  Args:\n    mode: 'train', 'eval', or 'generate'. Only mode related ops are added to\n        the graph.\n    config: An EventSequenceRnnConfig containing the encoder/decoder and HParams\n        to use.\n    sequence_example_file_paths: A list of paths to TFRecord files containing\n        tf.train.SequenceExample protos. Only needed for training and\n        evaluation. May be a sharded file of the form.\n\n  Returns:\n    A tf.Graph instance which contains the TF ops.\n\n  Raises:\n    ValueError: If mode is not 'train', 'eval', or 'generate'.\n  \"\"\"\n  if mode not in ('train', 'eval', 'generate'):\n    raise ValueError(\"The mode parameter must be 'train', 'eval', \"\n                     \"or 'generate'. The mode parameter was: %s\" % mode)\n\n  hparams = config.hparams\n  encoder_decoder = config.encoder_decoder\n\n  tf.logging.info('hparams = %s', hparams.values())\n\n  input_size = encoder_decoder.input_size\n  num_classes = encoder_decoder.num_classes\n  no_event_label = encoder_decoder.default_event_label\n\n  with tf.Graph().as_default() as graph:\n    inputs, labels, lengths, = None, None, None\n\n    if mode == 'train' or mode == 'eval':\n      inputs, labels, lengths = magenta.common.get_padded_batch(\n          sequence_example_file_paths, hparams.batch_size, input_size)\n\n    elif mode == 'generate':\n      inputs = tf.placeholder(tf.float32, [hparams.batch_size, None,\n                                           input_size])\n\n    cell = make_rnn_cell(\n        hparams.rnn_layer_sizes,\n        dropout_keep_prob=(\n            1.0 if mode == 'generate' else hparams.dropout_keep_prob),\n        attn_length=hparams.attn_length)\n\n    initial_state = cell.zero_state(hparams.batch_size, tf.float32)\n\n    outputs, final_state = tf.nn.dynamic_rnn(\n        cell, inputs, initial_state=initial_state, swap_memory=True)\n\n    outputs_flat = tf.reshape(outputs, [-1, cell.output_size])\n    logits_flat = tf.contrib.layers.linear(outputs_flat, num_classes)\n\n    if mode == 'train' or mode == 'eval':\n      labels_flat = tf.reshape(labels, [-1])\n      mask = tf.sequence_mask(lengths)\n      if hparams.skip_first_n_losses:\n        skip = tf.minimum(lengths, hparams.skip_first_n_losses)\n        skip_mask = tf.sequence_mask(skip, maxlen=tf.reduce_max(lengths))\n        mask = tf.logical_and(mask, tf.logical_not(skip_mask))\n      mask = tf.cast(mask, tf.float32)\n      mask_flat = tf.reshape(mask, [-1])\n\n      num_logits = tf.to_float(tf.reduce_sum(lengths))\n\n      with tf.control_dependencies(\n          [tf.Assert(tf.greater(num_logits, 0.), [num_logits])]):\n        softmax_cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(\n            labels=labels_flat, logits=logits_flat)\n      loss = tf.reduce_sum(mask_flat * softmax_cross_entropy) / num_logits\n      perplexity = (tf.reduce_sum(mask_flat * tf.exp(softmax_cross_entropy)) /\n                    num_logits)\n\n      correct_predictions = tf.to_float(\n          tf.nn.in_top_k(logits_flat, labels_flat, 1)) * mask_flat\n      accuracy = tf.reduce_sum(correct_predictions) / num_logits * 100\n\n      event_positions = (\n          tf.to_float(tf.not_equal(labels_flat, no_event_label)) * mask_flat)\n      event_accuracy = (\n          tf.reduce_sum(tf.multiply(correct_predictions, event_positions)) /\n          tf.reduce_sum(event_positions) * 100)\n\n      no_event_positions = (\n          tf.to_float(tf.equal(labels_flat, no_event_label)) * mask_flat)\n      no_event_accuracy = (\n          tf.reduce_sum(tf.multiply(correct_predictions, no_event_positions)) /\n          tf.reduce_sum(no_event_positions) * 100)\n\n      global_step = tf.Variable(0, trainable=False, name='global_step')\n\n      tf.add_to_collection('loss', loss)\n      tf.add_to_collection('perplexity', perplexity)\n      tf.add_to_collection('accuracy', accuracy)\n      tf.add_to_collection('global_step', global_step)\n\n      summaries = [\n          tf.summary.scalar('loss', loss),\n          tf.summary.scalar('perplexity', perplexity),\n          tf.summary.scalar('accuracy', accuracy),\n          tf.summary.scalar(\n              'event_accuracy', event_accuracy),\n          tf.summary.scalar(\n              'no_event_accuracy', no_event_accuracy),\n      ]\n\n      if mode == 'train':\n        learning_rate = tf.train.exponential_decay(\n            hparams.initial_learning_rate, global_step, hparams.decay_steps,\n            hparams.decay_rate, staircase=True, name='learning_rate')\n\n        opt = tf.train.AdamOptimizer(learning_rate)\n        params = tf.trainable_variables()\n        gradients = tf.gradients(loss, params)\n        clipped_gradients, _ = tf.clip_by_global_norm(gradients,\n                                                      hparams.clip_norm)\n        train_op = opt.apply_gradients(zip(clipped_gradients, params),\n                                       global_step)\n        tf.add_to_collection('learning_rate', learning_rate)\n        tf.add_to_collection('train_op', train_op)\n\n        summaries.append(tf.summary.scalar(\n            'learning_rate', learning_rate))\n\n      if mode == 'eval':\n        summary_op = tf.summary.merge(summaries)\n        tf.add_to_collection('summary_op', summary_op)\n\n    elif mode == 'generate':\n      temperature = tf.placeholder(tf.float32, [])\n      softmax_flat = tf.nn.softmax(\n          tf.div(logits_flat, tf.fill([num_classes], temperature)))\n      softmax = tf.reshape(softmax_flat, [hparams.batch_size, -1, num_classes])\n\n      tf.add_to_collection('inputs', inputs)\n      tf.add_to_collection('initial_state', initial_state)\n      tf.add_to_collection('final_state', final_state)\n      tf.add_to_collection('temperature', temperature)\n      tf.add_to_collection('softmax', softmax)\n\n  return graph\n", "path": "magenta/models/shared/events_rnn_graph.py"}]}