我对机器学习比较陌生,正在尝试标准化一些数据。这是一个代码片段。
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers.experimental import preprocessing
data = np.array([[0.1, 0.2, 0.3], [0.8, 0.9, 1.0], [1.5, 1.6, 1.7],])
layer = preprocessing.Normalization()
layer.adapt(data)
normalized_data = layer(data)
但是,当我运行代码时,Python 告诉我对象数组方法没有生成数组。
ValueError Traceback (most recent call last)
<ipython-input-2-97aa44cf6880> in <module>()
5 data = np.array([[0.1, 0.2, 0.3], [0.8, 0.9, 1.0], [1.5, 1.6, 1.7],])
6 layer = preprocessing.Normalization()
----> 7 layer.adapt(data)
8 normalized_data = layer(data)
~\.conda\envs\py35\lib\site-packages\tensorflow\python\keras\engine\base_preprocessing_layer.py in adapt(self, data, reset_state)
214
215 updates = self._combiner.extract(accumulator)
--> 216 self._set_state_variables(updates)
217
218 def _set_state_variables(self, updates):
~\.conda\envs\py35\lib\site-packages\tensorflow\python\keras\engine\base_preprocessing_layer.py in _set_state_variables(self, updates)
235 with ops.init_scope():
236 for var_name, value in updates.items():
--> 237 self.state_variables[var_name].assign(value)
238
239
~\.conda\envs\py35\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in assign(self, value, use_locking, name, read_value)
855 # initialize the variable.
856 with _handle_graph(self.handle):
--> 857 value_tensor = ops.convert_to_tensor(value, dtype=self.dtype)
858 self._shape.assert_is_compatible_with(value_tensor.shape)
859 assign_op = gen_resource_variable_ops.assign_variable_op(
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497
1498 if ret is None:
-> 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py in _default_conversion_function(***failed resolving arguments***)
50 def _default_conversion_function(value, dtype, name, as_ref):
51 del as_ref # Unused.
---> 52 return constant_op.constant(value, dtype, name=name)
53
54
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
262 """
263 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264 allow_broadcast=True)
265
266
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
273 with trace.Trace("tf.constant"):
274 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276
277 g = ops.get_default_graph()
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299 """Implementation of eager constant."""
--> 300 t = convert_to_eager_tensor(value, ctx, dtype)
301 if shape is None:
302 return t
~\.conda\envs\py35\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: object __array__ method not producing an array
我做了一些研究,但找不到我的问题的任何答案。 我究竟做错了什么?我在 Win10 上运行 Tensorflow 版本 2.3.0、Numpy 1.14.2、Python 3.5。
谢谢大家的帮助!
我能够在 Colab 上使用
TF 2.3
和 numpy 1.18.5
中的 TF 2.4.1
和 1.19.5
执行您的代码。
import numpy as np
import tensorflow as tf
print(tf.__version__)
print(np.__version__)
from tensorflow.keras.layers.experimental import preprocessing
data = np.array([[0.1, 0.2, 0.3], [0.8, 0.9, 1.0], [1.5, 1.6, 1.7],])
layer = preprocessing.Normalization()
layer.adapt(data)
normalized_data = layer(data)
print(normalized_data)
输出:
2.3.0
1.18.5
tf.Tensor(
[[-1.2247449 -1.2247449 -1.2247449]
[ 0. 0. 0. ]
[ 1.2247449 1.224745 1.224745 ]], shape=(3, 3), dtype=float32)
对于使用 Windows 的任何人:我遇到了同样的问题,并通过以下步骤解决了它:
这解决了我的问题。之前,我只是默认安装了 Tensorflow。但似乎与其他库有冲突,尤其是与我之前安装的 Numpy 版本。