Normalization of input data in Keras

我只是一个虾纸丫 提交于 2020-05-13 05:34:32

问题


One common task in DL is that you normalize input samples to zero mean and unit variance. One can "manually" perform the normalization using code like this:

mean = np.mean(X, axis = 0)
std = np.std(X, axis = 0)
X = [(x - mean)/std for x in X]

However, then one must keep the mean and std values around, to normalize the testing data, in addition to the Keras model being trained. Since the mean and std are learnable parameters, perhaps Keras can learn them? Something like this:

m = Sequential()
m.add(SomeKerasLayzerForNormalizing(...))
m.add(Conv2D(20, (5, 5), input_shape = (21, 100, 3), padding = 'valid'))
... rest of network
m.add(Dense(1, activation = 'sigmoid'))

I hope you understand what I'm getting at.


回答1:


There's BatchNormalization, which learns mean and standard deviation of the input. I haven't tried using it as the first layer of the network, but as I understand it, it should do something very similar to what you're looking for.




回答2:


Add BatchNormalization as the first layer and it works as expected.

I also had the same question and added it to test as suggested by @johannes and it worked as expected. Just writing this answer to help someone save some time.




回答3:


Maybe you can use sklearn.preprocessing.StandardScaler to scale you data, This object allow you to save the scaling parameters in an object, Then you can use Mixin types inputs into you model, lets say:

  1. Your_model
  2. [param1_scaler, param2_scaler]

Here is a link https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/

https://keras.io/getting-started/functional-api-guide/



来源:https://stackoverflow.com/questions/55924789/normalization-of-input-data-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!