【问题标题】:Keras Tutorial - error in get normalization layerKeras 教程 - 获取规范化层时出错
【发布时间】: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


    【解决方案1】:

    我不确定方法,但升级 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)
    

    调用与上面相同的方法而不会出错。

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

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2017-10-16
      • 2018-12-23
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多