【发布时间】:2021-04-17 06:27:28
【问题描述】:
我对机器学习比较陌生,正在尝试规范化一些数据。这是一个代码 sn-p。
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。
谢谢你们的帮助,伙计们!
【问题讨论】:
-
这很可能是一个错误,因为 tf == 2.4.0 的 google colab 似乎可以完美运行。如果可能,尝试升级 tf。
标签: python python-3.x numpy tensorflow anaconda