【问题标题】:Keras BatchNormalizing does not produce expected outputKeras BatchNormalizing 不会产生预期的输出
【发布时间】:2021-01-20 18:32:26
【问题描述】:

我正在尝试在 Numpy 中从 Keras 重新创建 BatchNormalizing 层:(Python)

model = Sequential()
model.add(BatchNormalization(axis=1, center=False, scale=False))
model.compile(optimizer='adam', loss='mse', metrics=['mse'])

scale = np.linspace(0, 100, 1000)
x_train = np.sin(scale) + 2.5
y_train = np.sin(scale)
print(x_train.shape)
print(x_train.shape)

model.fit(x_train, y_train, epochs=100, batch_size=100, shuffle=True, verbose=2)

x_test = np.array([1])

mean = model.layers[0].get_weights()[0]
var = model.layers[0].get_weights()[1]

print('mean', np.mean(x_train), 'mean_tf', mean)
print('var', np.var(x_train), 'var_tf', var)

print('result_tf', model.predict(x_test))
print('result_pred', (x_test - mean) / var)



为什么我没有得到相同的结果?

居中和缩放 =True 时相同。但我想保持简单。我已经得到了所有其他层,例如密集或 LSTM。

【问题讨论】:

标签: python numpy tensorflow keras


【解决方案1】:

尝试使用 print('result_pred', (x_test - mean) / np.sqrt(var)) 并为进一步解释检查我在此答案 stackoverflow.com/a/65744394/10733051 中的编辑

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 2021-03-06
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多