NotImplementedError:无法将符号张量 (lstm_4/strided_slice:0) 转换为 numpy 数组
Posted
技术标签:
【中文标题】NotImplementedError:无法将符号张量 (lstm_4/strided_slice:0) 转换为 numpy 数组【英文标题】:NotImplementedError: Cannot convert a symbolic Tensor (lstm_4/strided_slice:0) to a numpy array 【发布时间】:2022-01-12 19:02:54 【问题描述】:Numpy 版本 1.19.2,tensorflow 版本 2.3.0。
我正在尝试创建一个 LSTM 网络:
model=Sequential()
model.add(Embedding(input_dim=vocab_size,output_dim=embed_dim,input_length=max_rev_len,embeddings_initializer=Constant(embed_matrix)))
model.add(LSTM(100))
model.add(Flatten())
model.add(Dense(16,activation='relu'))
model.add(Dropout(0.70))
model.add(Dense(1,activation='sigmoid'))
当我运行单元格时(我在 Windows 11 上使用 jupyter notebook),我收到错误:
NotImplementedError Traceback (most recent call last)
<ipython-input-57-d024ff74859f> in <module>
5 model.add(Embedding(input_dim=vocab_size,output_dim=embed_dim,input_length=max_rev_len,embeddings_initializer=Constant(embed_matrix)))
6
----> 7 model.add(LSTM(100))
8 model.add(Flatten())
9
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
219 # If the model is being built continuously on top of an input layer:
220 # refresh its output.
--> 221 output_tensor = layer(self.outputs[0])
222 if len(nest.flatten(output_tensor)) != 1:
223 raise ValueError(SINGLE_LAYER_OUTPUT_ERROR_MSG)
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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:
C:\ProgramData\Anaconda3\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):
C:\ProgramData\Anaconda3\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):
C:\ProgramData\Anaconda3\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:
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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)
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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):
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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
C:\ProgramData\Anaconda3\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)
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
3049 ... # total product 1*2*3 = 6
3050 array([1, 2, 6])
-> 3051 >>> a = np.array([[1, 2, 3], [4, 5, 6]])
3052 >>> np.cumprod(a, dtype=float) # specify type of output
3053 array([ 1., 2., 6., 24., 120., 720.])
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
84 else:
85 return reduction(axis=axis, out=out, **passkwargs)
---> 86
87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
88
C:\ProgramData\Anaconda3\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_4/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
我尝试了here 提到的解决方案,但即使降级 numpy 也无济于事。我不确定这是 tensorflow 错误还是我做错了什么。请指教!
【问题讨论】:
【参考方案1】:我相信这是一个错误。我设法通过升级到 tensorflow-gpu==2.7 来解决它。 我也有 python 3.9.7 和最新的 numpy。
最好的
【讨论】:
以上是关于NotImplementedError:无法将符号张量 (lstm_4/strided_slice:0) 转换为 numpy 数组的主要内容,如果未能解决你的问题,请参考以下文章
无法将符号张量 (lstm_15/strided_slice:0) 转换为 numpy 数组
Python NotImplementedError:无法在进程之间传递池对象
关于PyTorch继承nn.Module出现raise NotImplementedError的问题解决方案