LSTM问题 - 无法将符号张量(LSTM_9/Strided_slice:0)转换为numpy数组

发布于 2025-01-24 04:35:29 字数 7958 浏览 0 评论 0原文

我有一个3D张量,尺寸为(行,n_hour lags,n_features) 所有值均为np.float64或np.int64 LSTM架构很简单:

我已经查找了一些最终解决问题的问题,但没有一个有效。使用以下系统和软件包版本:

Windows 10 TensorFlow 2.3.0 Numpy 1.18.5 python 3.8.13`

model = Sequential()
model.add(LSTM(n_features, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=50, batch_size=6, ` validation_data(test_X,test_y),verbose=2, shuffle=False,validation_split=0.2)

但是

在尝试执行此代码时,我会收到以下错误:

---------------------------------------------------------------------------
NotImplementedError   Traceback (most recent call last)
<ipython-input-1951-b640ca9f324a> in <module>
  1 # design network
  2 model = Sequential()
----> 3 model.add(LSTM(n_features, input_shape=(train_X.shape[1], train_X.shape[2])))
  4 model.add(Dense(1))
  5 model.compile(loss='mae', optimizer='adam')

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False  # pylint: disable=protected-access
456 try:
--> 457   result = method(self, *args, **kwargs)
458 finally:
459   self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
204   # and create the node connecting the current layer
205   # to the input layer we just created.
--> 206   layer(x)
207   set_inputs = True
208 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
661 
662 if initial_state is None and constants is None:
--> 663   return super(RNN, self).__call__(inputs, **kwargs)
664 
665 # If any of `initial_state` or `constants` are specified and are Keras

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
923 # >> model = tf.keras.Model(inputs, outputs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 925   return self._functional_construction_call(inputs, args, kwargs,
926 input_list)
927 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1115   try:
   1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
-> 1117   outputs = call_fn(cast_inputs, *args, **kwargs)
   1118 
   1119   except errors.OperatorNotAllowedInGraphError as e:

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent_v2.py in call(self, inputs, mask, training, initial_state)
   1106 
   1107 # LSTM does not support constants. Ignore it during process.
-> 1108 inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
   1109 
   1110 if isinstance(mask, list):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _process_inputs(self, inputs, initial_state, constants)
860 initial_state = self.states
861 elif initial_state is None:
--> 862   initial_state = self.get_initial_state(inputs)
863 
864 if len(initial_state) != len(self.states):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in get_initial_state(self, inputs)
643 dtype = inputs.dtype
644 if get_initial_state_fn:
--> 645   init_state = get_initial_state_fn(
646   inputs=None, batch_size=batch_size, dtype=dtype)
647 else:

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in get_initial_state(self, inputs, batch_size, dtype)
   2521 
   2522   def get_initial_state(self, inputs=None, batch_size=None, dtype=None):
-> 2523 return list(_generate_zero_filled_state_for_cell(
   2524 self, inputs, batch_size, dtype))
   2525 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _generate_zero_filled_state_for_cell(cell, inputs, batch_size, dtype)
   2966 batch_size = array_ops.shape(inputs)[0]
   2967 dtype = inputs.dtype
-> 2968   return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
   2969 
   2970 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _generate_zero_filled_state(batch_size_tensor, state_size, dtype)
   2982 
   2983   if nest.is_sequence(state_size):
-> 2984 return nest.map_structure(create_zeros, state_size)
   2985   else:
   2986 return create_zeros(state_size)

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
633 
634   return pack_sequence_as(
--> 635   structure[0], [func(*x) for x in entries],
636   expand_composites=expand_composites)
637 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
633 
634   return pack_sequence_as(
--> 635   structure[0], [func(*x) for x in entries],
636   expand_composites=expand_composites)
637 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in create_zeros(unnested_state_size)
   2979 flat_dims = tensor_shape.as_shape(unnested_state_size).as_list()
   2980 init_state_size = [batch_size_tensor] + flat_dims
-> 2981 return array_ops.zeros(init_state_size, dtype=dtype)
   2982 
   2983   if nest.is_sequence(state_size):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201   return target(*args, **kwargs)
202 except (TypeError, ValueError):
203   # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in wrapped(*args, **kwargs)
   2745 
   2746   def wrapped(*args, **kwargs):
-> 2747 tensor = fun(*args, **kwargs)
   2748 tensor._is_zeros_tensor = True
   2749 return tensor

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in zeros(shape, dtype, name)
   2792   # Create a constant if it won't be very big. Otherwise create a fill
   2793   # op to prevent serialized GraphDefs from becoming too large.
-> 2794   output = _constant_if_small(zero, shape, dtype, name)
   2795   if output is not None:
   2796 return output

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in _constant_if_small(value, shape, dtype, name)
   2730 def _constant_if_small(value, shape, dtype, name):
   2731   try:
-> 2732 if np.prod(shape) < 1000:
   2733   return constant(value, shape=shape, dtype=dtype, name=name)
   2734   except TypeError:

<__array_function__ internals> in prod(*args, **kwargs)

~\anaconda3\envs\gpu\lib\site-packages\numpy\core\fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
   3049 -------
   3050 number_of_dimensions : int
-> 3051 The number of dimensions in `a`.  Scalars are zero-dimensional.
   3052 
   3053 See Also

~\anaconda3\envs\gpu\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
 84 # support a dtype.
 85 if dtype is not None:
---> 86 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
 87 else:
 88 return reduction(axis=axis, out=out, **passkwargs)

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\framework\ops.py in __array__(self)
843 
844   def __array__(self):
--> 845 raise NotImplementedError(
846 "Cannot convert a symbolic Tensor ({}) to a numpy array."
847 " This error may indicate that you're trying to pass a Tensor to"

NotImplementedError: Cannot convert a symbolic Tensor (lstm_9/strided_slice:0) to a numpy 
array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is 
not supported

I have a 3D tensor where the dimensions are (rows, n_hour lags, n_features)
All values are either np.float64 or np.int64
The LSTM architecture is simple:

I already looked up some eventual problem solving threads but none was effective. Following system and package versions are used:

Windows 10
tensorflow 2.3.0
numpy 1.18.5
python 3.8.13

model = Sequential()
model.add(LSTM(n_features, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=50, batch_size=6, ` validation_data(test_X,test_y),verbose=2, shuffle=False,validation_split=0.2)

`

Yet when trying to execute this code, i receive following error:

---------------------------------------------------------------------------
NotImplementedError   Traceback (most recent call last)
<ipython-input-1951-b640ca9f324a> in <module>
  1 # design network
  2 model = Sequential()
----> 3 model.add(LSTM(n_features, input_shape=(train_X.shape[1], train_X.shape[2])))
  4 model.add(Dense(1))
  5 model.compile(loss='mae', optimizer='adam')

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False  # pylint: disable=protected-access
456 try:
--> 457   result = method(self, *args, **kwargs)
458 finally:
459   self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
204   # and create the node connecting the current layer
205   # to the input layer we just created.
--> 206   layer(x)
207   set_inputs = True
208 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
661 
662 if initial_state is None and constants is None:
--> 663   return super(RNN, self).__call__(inputs, **kwargs)
664 
665 # If any of `initial_state` or `constants` are specified and are Keras

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
923 # >> model = tf.keras.Model(inputs, outputs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 925   return self._functional_construction_call(inputs, args, kwargs,
926 input_list)
927 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1115   try:
   1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
-> 1117   outputs = call_fn(cast_inputs, *args, **kwargs)
   1118 
   1119   except errors.OperatorNotAllowedInGraphError as e:

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent_v2.py in call(self, inputs, mask, training, initial_state)
   1106 
   1107 # LSTM does not support constants. Ignore it during process.
-> 1108 inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
   1109 
   1110 if isinstance(mask, list):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _process_inputs(self, inputs, initial_state, constants)
860 initial_state = self.states
861 elif initial_state is None:
--> 862   initial_state = self.get_initial_state(inputs)
863 
864 if len(initial_state) != len(self.states):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in get_initial_state(self, inputs)
643 dtype = inputs.dtype
644 if get_initial_state_fn:
--> 645   init_state = get_initial_state_fn(
646   inputs=None, batch_size=batch_size, dtype=dtype)
647 else:

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in get_initial_state(self, inputs, batch_size, dtype)
   2521 
   2522   def get_initial_state(self, inputs=None, batch_size=None, dtype=None):
-> 2523 return list(_generate_zero_filled_state_for_cell(
   2524 self, inputs, batch_size, dtype))
   2525 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _generate_zero_filled_state_for_cell(cell, inputs, batch_size, dtype)
   2966 batch_size = array_ops.shape(inputs)[0]
   2967 dtype = inputs.dtype
-> 2968   return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
   2969 
   2970 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in _generate_zero_filled_state(batch_size_tensor, state_size, dtype)
   2982 
   2983   if nest.is_sequence(state_size):
-> 2984 return nest.map_structure(create_zeros, state_size)
   2985   else:
   2986 return create_zeros(state_size)

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
633 
634   return pack_sequence_as(
--> 635   structure[0], [func(*x) for x in entries],
636   expand_composites=expand_composites)
637 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
633 
634   return pack_sequence_as(
--> 635   structure[0], [func(*x) for x in entries],
636   expand_composites=expand_composites)
637 

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in create_zeros(unnested_state_size)
   2979 flat_dims = tensor_shape.as_shape(unnested_state_size).as_list()
   2980 init_state_size = [batch_size_tensor] + flat_dims
-> 2981 return array_ops.zeros(init_state_size, dtype=dtype)
   2982 
   2983   if nest.is_sequence(state_size):

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201   return target(*args, **kwargs)
202 except (TypeError, ValueError):
203   # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in wrapped(*args, **kwargs)
   2745 
   2746   def wrapped(*args, **kwargs):
-> 2747 tensor = fun(*args, **kwargs)
   2748 tensor._is_zeros_tensor = True
   2749 return tensor

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in zeros(shape, dtype, name)
   2792   # Create a constant if it won't be very big. Otherwise create a fill
   2793   # op to prevent serialized GraphDefs from becoming too large.
-> 2794   output = _constant_if_small(zero, shape, dtype, name)
   2795   if output is not None:
   2796 return output

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\ops\array_ops.py in _constant_if_small(value, shape, dtype, name)
   2730 def _constant_if_small(value, shape, dtype, name):
   2731   try:
-> 2732 if np.prod(shape) < 1000:
   2733   return constant(value, shape=shape, dtype=dtype, name=name)
   2734   except TypeError:

<__array_function__ internals> in prod(*args, **kwargs)

~\anaconda3\envs\gpu\lib\site-packages\numpy\core\fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
   3049 -------
   3050 number_of_dimensions : int
-> 3051 The number of dimensions in `a`.  Scalars are zero-dimensional.
   3052 
   3053 See Also

~\anaconda3\envs\gpu\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
 84 # support a dtype.
 85 if dtype is not None:
---> 86 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
 87 else:
 88 return reduction(axis=axis, out=out, **passkwargs)

~\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\framework\ops.py in __array__(self)
843 
844   def __array__(self):
--> 845 raise NotImplementedError(
846 "Cannot convert a symbolic Tensor ({}) to a numpy array."
847 " This error may indicate that you're trying to pass a Tensor to"

NotImplementedError: Cannot convert a symbolic Tensor (lstm_9/strided_slice:0) to a numpy 
array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is 
not supported

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文