简体   繁体   English

InvalidArgumentError: 无法计算 Sub 作为输入 #1(从零开始)应该是一个 uint8 张量,但它是一个浮点张量 [Op:Sub]

[英]InvalidArgumentError: cannot compute Sub as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:Sub]

Question

Please help understand the cause of the error and how to resolve.请帮助了解错误原因以及如何解决。

Code代码

import tensorflow as tf
import numpy as np

fashion_mnist = tf.keras.datasets.fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_full = np.concatenate((x_train, x_test), axis=0)

layer = tf.keras.layers.experimental.preprocessing.Normalization()
layer.adapt(x_full)
layer(x_train)

Error错误

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-16-699c47b6db55> in <module>
----> 1 ds = layer(x_train)

~/conda/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    966           with base_layer_utils.autocast_context_manager(
    967               self._compute_dtype):
--> 968             outputs = self.call(cast_inputs, *args, **kwargs)
    969           self._handle_activity_regularization(inputs, outputs)
    970           self._set_mask_metadata(inputs, outputs, input_masks)

~/conda/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/keras/layers/preprocessing/normalization.py in call(self, inputs)
    109     mean = array_ops.reshape(self.mean, self._broadcast_shape)
    110     variance = array_ops.reshape(self.variance, self._broadcast_shape)
--> 111     return (inputs - mean) / math_ops.sqrt(variance)
    112 
    113   def compute_output_shape(self, input_shape):

~/conda/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
    982     with ops.name_scope(None, op_name, [x, y]) as name:
    983       if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
--> 984         return func(x, y, name=name)
    985       elif not isinstance(y, sparse_tensor.SparseTensor):
    986         try:

~/conda/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in sub(x, y, name)
  10098         pass  # Add nodes to the TensorFlow graph.
  10099     except _core._NotOkStatusException as e:
> 10100       _ops.raise_from_not_ok_status(e, name)
  10101   # Add nodes to the TensorFlow graph.
  10102   _, _, _op, _outputs = _op_def_library._apply_op_helper(

~/conda/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6651   message = e.message + (" name: " + name if name is not None else "")
   6652   # pylint: disable=protected-access
-> 6653   six.raise_from(core._status_to_exception(e.code, message), None)
   6654   # pylint: enable=protected-access
   6655 

~/conda/envs/tensorflow/lib/python3.7/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute Sub as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:Sub]

Attempts尝试

Tried dtype arg but same error.试过 dtype arg 但同样的错误。

layer = tf.keras.layers.experimental.preprocessing.Normalization(dtype='float32')

Divide by 1.0 fixed the issue but not sure the original cause.除以 1.0 解决了问题,但不确定原始原因。

x_full = np.concatenate((x_train, x_test), axis=0) / 1.0
x_train = x_train / 1.0

Does Keras only works with float32? Keras 只适用于 float32 吗?

Related issues相关问题

The cause is preprocessing.Normalization expect float32 but your data was uint8 and thus that error.原因是preprocessing.Normalization expect float32但您的数据是uint8 ,因此出现错误。

It's actually Tensorflow's problem not Keras itself as this is faster computation.这实际上是 Tensorflow 的问题,而不是 Keras 本身,因为这是更快的计算。

Reminder: float and int compute in different places in the processor and each processor have different performance on different data types, for example nvidia's gpus are faster with float32 than float16 while arm cpus are faster with the 16.提醒:处理器中不同位置的 float 和 int 计算,每个处理器在不同数据类型上有不同的性能,例如 nvidia 的 gpus 使用float32float16更快,而 arm cpus 使用 16 更快。

Pytorch too need two variables to be same data type or it won't work. Pytorch 也需要两个变量是相同的数据类型,否则它将不起作用。

Divide an integer with a float in python automatically give you a new float, x_train = x_train / 1.0 will make x_train float32 (or float64 or float16 depending on what you have in ~/.keras/keras.json but you have float32 here).将 integer 与 python 中的浮点数相除会自动为您提供一个新的浮点数, x_train = x_train / 1.0将使x_train float32 (或float64float16 ,具体取决于您在~/.keras/keras.json中的内容,但此处有float32 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError: 无法计算 MatMul 作为输入 #0(从零开始)应该是一个浮点张量,但它是一个双张量 [Op:MatMul] - InvalidArgumentError: cannot compute MatMul as input #0(zero-based) was expected to be a float tensor but is a double tensor [Op:MatMul] Tensorflow 无法计算 Addv2,因为输入 #1(从零开始)应该是双张量,但它是浮点张量 [Op:Addv] - Tensorflow cannot compute Addv2 as input #1(zero-based) was expected to be a double tensor but it is a float tensor [Op:Addv] 无法计算 Add 作为输入 #1(从零开始)应该是一个 int32 张量,但它是一个双张量 [Op:Add] - cannot compute Add as input #1(zero-based) was expected to be a int32 tensor but is a double tensor [Op:Add] '无法计算 Pack 作为输入 #1(从零开始)应该是一个浮点张量,但它是一个 int32 张量 [Op:Pack] 名称:packed'。 tf.squeeze 出错 - 'cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed'. Error with tf.squeeze Tensorflow将uint8张量视为float32张量 - Tensorflow viewing a uint8 tensor as a float32 tensor 修剪(文本):期望参数#0(从零开始)是一个张量; got list (['烤蚂蚁是哥伦比亚很受欢迎的小吃']) - pruned(text): expected argument #0(zero-based) to be a Tensor; got list (['Roasted ants are a popular snack in Columbia']) InvalidArgumentError:无法将张量添加到批次:元素数量不匹配。 形状是:[张量]:[4],[批次]:[5] [Op:IteratorGetNext] - InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [4], [batch]: [5] [Op:IteratorGetNext] InvalidArgumentError:reshape 的输入是一个具有 405000 个值的张量,但请求的形状有 1920000 [Op:Reshape] - InvalidArgumentError:Input to reshape is a tensor with 405000 values, but the requested shape has 1920000[Op:Reshape] InvalidArgumentError:reshape 的输入是具有 27000 个值的张量,但请求的形状有 810000 [Op:Reshape] - InvalidArgumentError: Input to reshape is a tensor with 27000 values, but the requested shape has 810000 [Op:Reshape] 如何在ml引擎中构建用于对象检测预测的uint8 numpy数组输入张量 - How to build uint8 numpy array input tensor for object detection prediction in ml engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM