【发布时间】:2021-03-10 02:06:35
【问题描述】:
我正在学习我的第一个使用 Keras 制作分类器的教程 (https://www.tensorflow.org/tutorials/structured_data/preprocessing_layers)
我正在一步一步地遵循每条指令,但我使用的是我自己的数据集。
我有一列(“速度”)包含浮点值。
这是教程提出的获取规范化层的代码:
def get_normalization_layer(name, dataset):
# Create a Normalization layer for our feature.
normalizer = preprocessing.Normalization()
# Prepare a Dataset that only yields our feature.
feature_ds = dataset.map(lambda x, y: x[name])
# Learn the statistics of the data.
normalizer.adapt(feature_ds)
return normalizer
然后,它将此方法应用于他们的列“PhotoAmt”(宠物的照片数量)。我以同样的方式将它应用到我的“速度”列中。
speed_col = train_features['speed']
layer = get_normalization_layer('speed', train_ds)
layer(speed_col)
我了解他们的“PhotoAmt”列具有 Int 值。
我收到以下错误:
/Users/myname/Library/Python/3.7/lib/python/site-packages/tensorflow/python/keras/layers/preprocessing/normalization.py:184: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
accumulator.mean * accumulator.count for accumulator in accumulators
Traceback (most recent call last):
File "keras_models.py", line 59, in <module>
layer = get_normalization_layer('speed', train_ds)
File "keras_models.py", line 54, in get_normalization_layer
normalizer.adapt(feature_ds)
File "/Users/myname/Library/Python/3.7/lib/python/site-packages/tensorflow/python/keras/engine/base_preprocessing_layer.py", line 188, in adapt
accumulator = self._combiner.compute(data_element, accumulator)
File "/Users/myname/Library/Python/3.7/lib/python/site-packages/tensorflow/python/keras/layers/preprocessing/normalization.py", line 173, in compute
return self.merge([accumulator, sanitized_accumulator])
File "/Users/myname/Library/Python/3.7/lib/python/site-packages/tensorflow/python/keras/layers/preprocessing/normalization.py", line 184, in merge
accumulator.mean * accumulator.count for accumulator in accumulators
ValueError: operands could not be broadcast together with shapes (5,) (2,)
虽然本教程的预期输出是:
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[ 1.045485 ],
[-1.1339161 ],
[-0.19988704],
[ 0.11145599],
[ 0.42279902]], dtype=float32)>
(当然,我期待不同的数值)
我不明白这个错误。 这个问题与我使用浮点数而不是整数有关吗? 还是我的列值插入错误?我很确定“速度”列中没有任何行包含空值或类似值。
我使用的是 TensorFlow 2.2.0、python 3.7
谢谢大家。
【问题讨论】:
标签: keras tensorflow2.0 normalization valueerror