Étant donné les valeurs d'entrée [1, 5]
et en les normalisant, devrait donner quelque chose comme [-1, 1]
if I understand correctly, parce quecouche BatchNormalization en Keras donne des valeurs de sortie inattendues
mean = 3
var = 4
result = (x - mean)/sqrt(var)
Cependant cet exemple minimal
import numpy as np
import keras
from keras.models import Model
from keras.layers import Input
from keras.layers.normalization import BatchNormalization
from keras import backend as K
shape = (1,2,1)
input = Input(shape=shape)
x = BatchNormalization(center=False)(input) # no beta
model = Model(inputs=input, outputs=x)
model.compile(loss='mse', optimizer='sgd')
# training with dummy data
training_in = [np.random.random(size=(10, *shape))]
training_out = [np.random.random(size=(10, *shape))]
model.fit(training_in, training_out, epochs=10)
data_in = np.array([[[[1], [5]]]], dtype=np.float32)
data_out = model.predict(data_in)
print('gamma :', K.eval(model.layers[1].gamma))
#print('beta :', K.eval(model.layers[1].beta))
print('moving_mean:', K.eval(model.layers[1].moving_mean))
print('moving_variance:', K.eval(model.layers[1].moving_variance))
print('epsilon :', model.layers[1].epsilon)
print('data_in :', data_in)
print('data_out:', data_out)
produit la sortie suivante :
gamma : [ 0.80644524]
moving_mean: [ 0.05885344]
moving_variance: [ 0.91000736]
epsilon : 0.001
data_in : [[[[ 1.]
[ 5.]]]]
data_out: [[[[ 0.79519051]
[ 4.17485714]]]]
Donc c'est [0.79519051, 4.17485714]
au lieu de [-1, 1]
. J'ai regardé source, et les valeurs semblent être transmises à tf.nn.batch_normalization. Et ce looks comme le résultat devrait être ce que j'excepte, mais évidemment ce n'est pas le cas.
Alors, comment les valeurs de sortie sont-elles calculées?
Impressionnant, merci beaucoup. Pourriez-vous également me dire où bêta s'inscrit dans la formule quand 'center = True'?Ma conjecture aurait été 'sortie = gamma * (input - moving_mean)/sqrt (moving_variance) + beta' mais quand je [activer centre] (https://ideone.com/TNRlFH) la [sortie] (https: // ideone.com/d3rROd) ne correspond pas. –
Il devrait être comme vous le dites selon tensorflow, compte tenu de [ceci] (https://www.tensorflow.org/api_docs/python/tf/nn/batch_normalization). [Keras] (https://keras.io/layers/normalization/) a l'air de faire 'output = gamma * ((input-moving_mean)/sqrt (moving_variance) + beta)' dans leur doc. Cependant, aucun ne correspond exactement, je ne sais pas pourquoi ... – gdelab
J'ai trouvé le problème. Nous n'utilisions pas epsilon. La formule entièrement correcte est 'result = gamma * (input - moving_mean)/sqrt (moving_variance + epsilon) + beta'. –