使用非符号张量的输入调用层 up_sampling2d_1。接收类型:<class 'numpy.ndarray'>
Layer up_sampling2d_1 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>
我正在测试 UpSampling2D 层在 Keras 中的工作方式,所以我只是想传递不同的输入来检查输出。在检查官方文档 here 中给出的示例时,我无法重现该示例,它给了我错误。
文档中的代码:
>>> input_shape = (2, 2, 1, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[ 0 1 2]]
[[ 3 4 5]]]
[[[ 6 7 8]]
[[ 9 10 11]]]]
>>> y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
>>> print(y)
tf.Tensor(
[[[[ 0 1 2]
[ 0 1 2]]
[[ 3 4 5]
[ 3 4 5]]]
[[[ 6 7 8]
[ 6 7 8]]
[[ 9 10 11]
[ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int64)
错误:
ValueError Traceback (most recent call last)
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
441 try:
--> 442 K.is_keras_tensor(x)
443 except ValueError:
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
467 tf.SparseTensor)):
--> 468 raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
469 'Expected a symbolic tensor instance.')
ValueError: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`. Expected a symbolic tensor instance.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-9-73f9365e21ac> in <module>
6
7
----> 8 y = keras.layers.UpSampling2D(size=(1, 2))(x)
9 print(y)
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
573 # Raise exceptions in case the input is not compatible
574 # with the input_spec specified in the layer constructor.
--> 575 self.assert_input_compatibility(inputs)
576
577 # Collect input shapes to build layer.
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
446 'Received type: ' +
447 str(type(x)) + '. Full input: ' +
--> 448 str(inputs) + '. All inputs to the layer '
449 'should be tensors.')
450
ValueError: Layer up_sampling2d_2 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[[[ 0, 1, 2]],
[[ 3, 4, 5]]],
[[[ 6, 7, 8]],
[[ 9, 10, 11]]]])]. All inputs to the layer should be tensors.
我应该如何在不创建模型并测试它的情况下测试这一层,就像文档中给出的那样。
您的错误来自输入 numpy.ndarray
而不是 tensor
,您可以试试这个:
a = tf.constant([2,2,1,3])
x = tf.reshape(tf.range(tf.math.reduce_prod(a, 0)) , a.numpy())
y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
print(y)
输出:
tf.Tensor(
[[[[ 0 1 2]
[ 0 1 2]]
[[ 3 4 5]
[ 3 4 5]]]
[[[ 6 7 8]
[ 6 7 8]]
[[ 9 10 11]
[ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int32)
我正在测试 UpSampling2D 层在 Keras 中的工作方式,所以我只是想传递不同的输入来检查输出。在检查官方文档 here 中给出的示例时,我无法重现该示例,它给了我错误。
文档中的代码:
>>> input_shape = (2, 2, 1, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[ 0 1 2]]
[[ 3 4 5]]]
[[[ 6 7 8]]
[[ 9 10 11]]]]
>>> y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
>>> print(y)
tf.Tensor(
[[[[ 0 1 2]
[ 0 1 2]]
[[ 3 4 5]
[ 3 4 5]]]
[[[ 6 7 8]
[ 6 7 8]]
[[ 9 10 11]
[ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int64)
错误:
ValueError Traceback (most recent call last)
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
441 try:
--> 442 K.is_keras_tensor(x)
443 except ValueError:
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
467 tf.SparseTensor)):
--> 468 raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
469 'Expected a symbolic tensor instance.')
ValueError: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`. Expected a symbolic tensor instance.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-9-73f9365e21ac> in <module>
6
7
----> 8 y = keras.layers.UpSampling2D(size=(1, 2))(x)
9 print(y)
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
573 # Raise exceptions in case the input is not compatible
574 # with the input_spec specified in the layer constructor.
--> 575 self.assert_input_compatibility(inputs)
576
577 # Collect input shapes to build layer.
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
446 'Received type: ' +
447 str(type(x)) + '. Full input: ' +
--> 448 str(inputs) + '. All inputs to the layer '
449 'should be tensors.')
450
ValueError: Layer up_sampling2d_2 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[[[ 0, 1, 2]],
[[ 3, 4, 5]]],
[[[ 6, 7, 8]],
[[ 9, 10, 11]]]])]. All inputs to the layer should be tensors.
我应该如何在不创建模型并测试它的情况下测试这一层,就像文档中给出的那样。
您的错误来自输入 numpy.ndarray
而不是 tensor
,您可以试试这个:
a = tf.constant([2,2,1,3])
x = tf.reshape(tf.range(tf.math.reduce_prod(a, 0)) , a.numpy())
y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
print(y)
输出:
tf.Tensor(
[[[[ 0 1 2]
[ 0 1 2]]
[[ 3 4 5]
[ 3 4 5]]]
[[[ 6 7 8]
[ 6 7 8]]
[[ 9 10 11]
[ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int32)