![](/img/trans.png)
[英]InvalidArgumentError: cannot compute MatMul as input #0(zero-based) was expected to be a float tensor but is a double tensor [Op:MatMul]
[英]InvalidArgumentError: cannot compute Sub as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:Sub]
請幫助了解錯誤原因以及如何解決。
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)
---------------------------------------------------------------------------
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]
試過 dtype arg 但同樣的錯誤。
layer = tf.keras.layers.experimental.preprocessing.Normalization(dtype='float32')
除以 1.0 解決了問題,但不確定原始原因。
x_full = np.concatenate((x_train, x_test), axis=0) / 1.0
x_train = x_train / 1.0
Keras 只適用於 float32 嗎?
原因是preprocessing.Normalization
expect float32
但您的數據是uint8
,因此出現錯誤。
這實際上是 Tensorflow 的問題,而不是 Keras 本身,因為這是更快的計算。
提醒:處理器中不同位置的 float 和 int 計算,每個處理器在不同數據類型上有不同的性能,例如 nvidia 的 gpus 使用float32
比float16
更快,而 arm cpus 使用 16 更快。
Pytorch 也需要兩個變量是相同的數據類型,否則它將不起作用。
將 integer 與 python 中的浮點數相除會自動為您提供一個新的浮點數, x_train = x_train / 1.0
將使x_train
float32
(或float64
或float16
,具體取決於您在~/.keras/keras.json
中的內容,但此處有float32
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.