keras.Normalization 按列
keras.Normalization column wise
我想为我的 keras 模型添加一个规范化层。
我在一个更简单的例子中测试它,但我不明白结果。
我做了一个简单的测试:
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)
我的问题是:如果第三行第一列的元素是其列的最大值,那么在归一化输出中它不应该是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?
您可能会将此层与 min-max 缩放混淆。 docs 明确指出:
This layer will shift and scale inputs into a distribution centered around 0 with standard deviation 1. It accomplishes this by precomputing the mean and variance of the data, and calling (input - mean) / sqrt(var) at runtime
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。检查此 以进行 min-max 缩放。
我想为我的 keras 模型添加一个规范化层。 我在一个更简单的例子中测试它,但我不明白结果。
我做了一个简单的测试:
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)
我的问题是:如果第三行第一列的元素是其列的最大值,那么在归一化输出中它不应该是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?
您可能会将此层与 min-max 缩放混淆。 docs 明确指出:
This layer will shift and scale inputs into a distribution centered around 0 with standard deviation 1. It accomplishes this by precomputing the mean and variance of the data, and calling (input - mean) / sqrt(var) at runtime
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。检查此