tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError:不允许迭代符号`tf.Tensor`

问题描述 投票:0回答:1

我正在尝试从这里运行 RNN 模型。由于此代码是近 7 年前的代码,因此它使用旧的 TensorFlow 版本 1(不会自动运行)。

我的代码:

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!!!

经过多次尝试调试,我运行

val, state_ = cell(self.inputs_with_embed)
,并得到以下错误:

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.**

我对堆栈溢出比较陌生,因为我刚刚习惯这个环境。如果有什么我可以改进的地方,请随时告诉我。这对我来说是一个完美的学习机会。

从【https://github.com/lilianweng/STOCK-rnn 下载原始代码后,我暂时将所有 v1 遗留代码更改为

tf.compat.v1.____
(例如,
tf.compat.v1.placeholder
)。

但特别是,我发现

tf.compat.v1.nn.rnn_cell.DropoutWrapper
已被弃用,所以我改为
tf.nn.RNNCellDropoutWrapper

无论我使用

LSTMCell
还是
LSTM
,我都会收到如上所示的错误消息。

我正在使用Python 3.11.5和tensorflow 2.16.1。

有人帮忙看一下吗?

非常感谢第一次加入 Stack Overflow!

python-3.x lstm tensorflow2.0 recurrent-neural-network
1个回答
0
投票

刚刚找到了!没关系!我做了以下工作并且有效:

fmap = cell(self.inputs_with_embed) 
val, state_ = tf.split(fmap, num_or_size_splits=2)
© www.soinside.com 2019 - 2024. All rights reserved.