I am trying to run an RNN model from here. Since this code is from nearly 7 years ago, it uses the old tensorflow version 1 (which does not run automatically).
My code:
def __init__(self, sess, stock_count,
lstm_size=128,
num_layers=1,
num_steps=30,
input_size=1,
embed_size=None,
logs_dir="logs",
plots_dir="images"):
"""
Construct a RNN model using LSTM cell.
Args:
sess:
stock_count (int): num. of stocks we are going to train with.
lstm_size (int)
num_layers (int): num. of LSTM cell layers.
num_steps (int)
input_size (int)
keep_prob (int): (1.0 - dropout rate.) for a LSTM cell.
embed_size (int): length of embedding vector, only used when stock_count > 1.
checkpoint_dir (str)
"""
self.sess = sess
self.stock_count = stock_count
self.lstm_size = lstm_size
self.num_layers = num_layers
self.num_steps = num_steps
self.input_size = input_size
self.use_embed = (embed_size is not None) and (embed_size > 0)
self.embed_size = embed_size or -1
self.logs_dir = logs_dir
self.plots_dir = plots_dir
self.build_graph()
def build_graph(self):
"""
The model asks for five things to be trained:
- learning_rate
- keep_prob: 1 - dropout rate
- symbols: a list of stock symbols associated with each sample
- input: training data X
- targets: training label y
"""
# inputs.shape = (number of examples, number of input, dimension of each input).
self.learning_rate = tf.compat.v1.placeholder(tf.float32, None, name="learning_rate")
self.keep_prob = tf.compat.v1.placeholder(tf.float32, None, name="keep_prob")
# Stock symbols are mapped to integers.
self.symbols = tf.compat.v1.placeholder(tf.int32, [None, 1], name='stock_labels')
self.inputs = tf.compat.v1.placeholder(tf.float32, [None, self.num_steps, self.input_size], name="inputs")
self.targets = tf.compat.v1.placeholder(tf.float32, [None, self.input_size], name="targets")
def _create_one_cell():
lstm_cell = tf.keras.layers.LSTM(self.lstm_size)
lstm_cell = tf.nn.RNNCellDropoutWrapper(lstm_cell, output_keep_prob=self.keep_prob)
return lstm_cell
cell = tf.keras.layers.RNN(
[tf.keras.layers.LSTMCell(self.lstm_size) for _ in range(self.num_layers)],
return_state=True,
return_sequences=True
) if self.num_layers > 1 else tf.keras.layers.LSTM(self.lstm_size, return_sequences=True)
if self.embed_size > 0 and self.stock_count > 1:
self.embed_matrix = tf.Variable(
tf.random_uniform([self.stock_count, self.embed_size], -1.0, 1.0),
name="embed_matrix"
)
# stock_label_embeds.shape = (batch_size, embedding_size)
stacked_symbols = tf.tile(self.symbols, [1, self.num_steps], name='stacked_stock_labels')
stacked_embeds = tf.nn.embedding_lookup(self.embed_matrix, stacked_symbols)
# After concat, inputs.shape = (batch_size, num_steps, input_size + embed_size)
self.inputs_with_embed = tf.concat([self.inputs, stacked_embeds], axis=2, name="inputs_with_embed")
self.embed_matrix_summ = tf.summary.histogram("embed_matrix", self.embed_matrix)
else:
self.inputs_with_embed = tf.identity(self.inputs)
self.embed_matrix_summ = None
print("inputs.shape:", self.inputs.shape) # (None, num_steps, 1)
print("inputs_with_embed.shape:", self.inputs_with_embed.shape) # (None, num_steps, 1)
# Run RNN model
**val, state_ = cell(self.inputs_with_embed)** # ERROR LINE!!!
After multiple attempts of debugging, I run val, state_ = cell(self.inputs_with_embed)
, and get the following error:
ERROR:tensorflow:==================================
Object was never used (type <class 'tensorflow.python.framework.ops.Operation'>):
<tf.Operation 'lstm/lstm_cell/Assert/Assert' type=Assert>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\ops\weak_tensor_ops.py", line 88, in wrapper
return op(*args, **kwargs) File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\ops\numpy_ops\np_array_ops.py", line 316, in diag
control_flow_assert.Assert( File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\traceback_utils.py", line 155, in error_handler
del filtered_tb File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\dispatch.py", line 1260, in op_dispatch_handler
return dispatch_target(*args, **kwargs) File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\tf_should_use.py", line 288, in wrapped
return _add_should_use_warning(fn(*args, **kwargs),
==================================
E0419 19:11:02.670499 50456 tf_should_use.py:80] ==================================
Object was never used (type <class 'tensorflow.python.framework.ops.Operation'>):
<tf.Operation 'lstm/lstm_cell/Assert/Assert' type=Assert>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\ops\weak_tensor_ops.py", line 88, in wrapper
return op(*args, **kwargs) File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\ops\numpy_ops\np_array_ops.py", line 316, in diag
control_flow_assert.Assert( File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\traceback_utils.py", line 155, in error_handler
del filtered_tb File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\dispatch.py", line 1260, in op_dispatch_handler
return dispatch_target(*args, **kwargs) File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\util\tf_should_use.py", line 288, in wrapped
return _add_should_use_warning(fn(*args, **kwargs),
==================================
Traceback (most recent call last):
File "C:\Users\mgzchange2000\Desktop\stock-rnn-master-mgz\main.py", line 111, in <module>
tf.compat.v1.app.run()
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\platform\app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\absl\app.py", line 308, in run
_run_main(main, args)
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\absl\app.py", line 254, in _run_main
sys.exit(main(argv))
^^^^^^^^^^
File "C:\Users\mgzchange2000\Desktop\stock-rnn-master-mgz\main.py", line 84, in main
rnn_model = LstmRNN(
^^^^^^^^
File "C:\Users\mgzchange2000\Desktop\stock-rnn-master-mgz\model_rnn.py", line 54, in __init__
self.build_graph()
File "C:\Users\mgzchange2000\Desktop\stock-rnn-master-mgz\model_rnn.py", line 108, in build_graph
**val, state_ = cell(self.inputs_with_embed)**
^^^^^^^^^^^
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\framework\tensor.py", line 323, in __iter__
self._disallow_iteration()
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\framework\tensor.py", line 319, in _disallow_iteration
self._disallow("Iterating over a symbolic `tf.Tensor`")
File "C:\Users\mgzchange2000\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\framework\tensor.py", line 303, in _disallow
**raise errors.OperatorNotAllowedInGraphError(
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed. You can attempt the following resolutions to the problem: If you are running in Graph mode, use Eager execution mode or decorate this function with @tf.function. If you are using AutoGraph, you can try decorating this function with @tf.function. If that does not work, then you may be using an unsupported feature or your source code may not be visible to AutoGraph. See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code for more information.**
I am relatively new to stack overflow, as I am just getting used to this environment. If there is anything I could improve on, please don't hesitate to let me know. This is a perfect learning opportunity for me.
After downloading the original code from https://github.com/lilianweng/STOCK-rnn, I have temporarily changed all v1 legacy code to tf.compat.v1.____
(e.g., tf.compat.v1.placeholder
).
But particularly, I see that tf.compat.v1.nn.rnn_cell.DropoutWrapper
is deprecated, so I changed to tf.nn.RNNCellDropoutWrapper
instead.
Regardless if I use LSTMCell
orLSTM
, I get the the error message as shown above.
I am using Python 3.11.5, and tensorflow 2.16.1.
Could someone help take a look?
Very thankful to be joining Stack Overflow for the first time!
Just found it! Never mind! I did the following and it worked:
fmap = cell(self.inputs_with_embed)
val, state_ = tf.split(fmap, num_or_size_splits=2)