簡體   English   中英

keras.標准化列

[英]keras.Normalization column wise

我想為我的 keras model 添加一個規范化層。我在一個更簡單的例子中測試它,但我不明白結果。

我做了一個簡單的測試:

normalizer = Normalization(axis=-1)
normalizer.adapt(x_train[:3])
print(x_train[:3])
print(normalizer(x_train[:3]))

我得到了以下結果:

[[ 82.83  31.04  47.   151.    17.88   0.    58.  ]
 [ 59.71  19.01  50.   141.     6.08   0.    60.  ]
 [133.33  62.68  84.   279.    15.17   0.    65.  ]]
tf.Tensor(
[[-0.2968958  -0.3549137  -0.79461485 -0.62603205  0.95840394  0.
  -1.0190493 ]
 [-1.0490034  -1.0080925  -0.6158265  -0.7851927  -1.3798107   0.
  -0.3396831 ]
 [ 1.3458993   1.3630061   1.4104416   1.411225    0.42140734  0.
   1.3587323 ]], shape=(3, 7), dtype=float32)

我的問題是:如果第三行第一列中的元素是其列中的最大值,它不應該在歸一化的 output 中為 1 嗎?

更新

很明顯,我對 min_max 比例感到困惑。

現在,我遇到的問題是,如果我對整個訓練數據集使用 adapt:

normalizer = Normalization(axis=-1)
normalizer.adapt(x_train)
print(x_train[:3])
print(normalizer(x_train[:3]))

然后,第二列總是給我 nan 值:

[[ 82.83  31.04  47.   151.    17.88   0.    58.  ]
 [ 59.71  19.01  50.   141.     6.08   0.    60.  ]
 [133.33  62.68  84.   279.    15.17   0.    65.  ]]
tf.Tensor(
[[-0.51946616         nan -1.4330941  -0.5569647   0.8550693  -0.05900022
  -0.17098609]
 [-1.3537331          nan -1.2127512  -0.62386954 -0.8509362  -0.05900022
  -0.1282853 ]
 [ 1.3027862          nan  1.2844696   0.29941723  0.4632664  -0.05900022
  -0.02153332]], shape=(3, 7), dtype=float32)

為什么該列具有 nan 值?

您可能會將這一層與最小-最大縮放混淆。 文檔清楚 state :

該層會將輸入移動並縮放到以 0 為中心、標准差為 1 的分布中。它通過預先計算數據的均值和方差,並在運行時調用 (input - mean) / sqrt(var) 來實現這一點

import tensorflow as tf

normalizer = tf.keras.layers.Normalization(axis=-1)
x_train = tf.constant([[82.83, 31.04, 47., 151., 17.88, 0., 58.],
                        [59.71, 19.01, 50., 141., 6.08, 0., 60.],
                        [133.33, 62.68, 84., 279., 15.17, 0., 65.]])
normalizer.adapt(x_train)
norm_x = normalizer(x_train)
print(tf.reduce_mean(norm_x), tf.math.reduce_std(norm_x))
tf.Tensor(6.81196e-08, shape=(), dtype=float32) tf.Tensor(0.9258201, shape=(), dtype=float32)

有了更多數據,您應該接近均值 0 和標准差 1。查看此帖子以了解最小-最大縮放比例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM