Keras Tutorial - 获取归一化层错误

Keras Tutorial - error in get normalization layer

我正在学习我的第一个使用 Keras 制作分类器的教程 (https://www.tensorflow.org/tutorials/structured_data/preprocessing_layers)

我正在一步一步地遵循每条指令,但我使用的是我自己的数据集。

我有一列 ('speed') 具有浮点值。

这是本教程提出的用于获取归一化层的代码:

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)>

(当然,我期待不同的数值)

我不理解这个错误。 这个问题是否与我使用 Floats 而不是 Ints 有关? 还是我的列值插入错误?我很确定 none 行在 'speed' 列中包含空值或类似值。

我正在使用 TensorFlow 2.2.0,python3.7

谢谢大家

我不太确定,但是升级 tensorflow 有:

pip3 install tensorflow --upgrade 

解决了。

或者至少,我可以跳到教程的下一段:

# Numeric features.
for header in ['speed']: #and other columns
  numeric_col = tf.keras.Input(shape=(1,), name=header)
  normalization_layer = get_normalization_layer(header, train_ds)
  encoded_numeric_col = normalization_layer(numeric_col)
  all_inputs.append(numeric_col)
  encoded_features.append(encoded_numeric_col)

调用与上面相同的方法而没有出错。

请注意,它仍然无法处理与“作为整数的分类特征”相关的段落,但我认为自己很满意,因为我没有这样的特征。