This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import auto_diff as ad | ||
from .layer import Layer | ||
|
||
|
||
class BatchNorm(Layer): | ||
|
||
def __init__(self, | ||
momentum=0.99, | ||
epsilon=1e-3, | ||
scale=True, | ||
center=True, | ||
beta_initializer=ad.inits.zeros, | ||
gamma_initializer=ad.inits.ones, | ||
**kwargs): | ||
super(BatchNorm, self).__init__(**kwargs) | ||
self.momentum = momentum | ||
self.epsilon = epsilon | ||
self.scale = scale | ||
self.center = center | ||
self.beta_initializer = beta_initializer | ||
self.gamma_initializer = gamma_initializer | ||
self.gamma, self.beta = None, None | ||
self.moving_mean, self.moving_var = None, None | ||
|
||
def build(self, input_shape): | ||
if not self._built: | ||
if self.scale: | ||
self.gamma = self.add_weight( | ||
name='gamma', | ||
shape=(input_shape[-1],), | ||
initializer=self.gamma_initializer, | ||
trainable=True, | ||
) | ||
if self.center: | ||
self.beta = self.add_weight( | ||
name='beta', | ||
shape=(input_shape[-1],), | ||
initializer=self.beta_initializer, | ||
trainable=True, | ||
) | ||
self.moving_mean = self.add_weight( | ||
name='moving_mean', | ||
shape=(input_shape[-1],), | ||
initializer=self.gamma_initializer, | ||
trainable=False, | ||
) | ||
self.moving_var = self.add_weight( | ||
name='moving_var', | ||
shape=(input_shape[-1],), | ||
initializer=self.beta_initializer, | ||
trainable=False, | ||
) | ||
super(BatchNorm, self).build(input_shape) | ||
|
||
def compute_output_shape(self, input_shape): | ||
return input_shape | ||
|
||
def call_moving(self, inputs, moving_mean, moving_var): | ||
normal = (inputs - moving_mean) / ad.sqrt(moving_var + self.epsilon) | ||
if self.scale: | ||
normal *= self.gamma | ||
if self.center: | ||
normal += self.beta | ||
return normal | ||
|
||
def call(self, inputs, **kwargs): | ||
sum_axis = tuple(range(len(inputs.shape) - 1)) | ||
mean = ad.mean(inputs, axis=sum_axis, keepdims=True) | ||
var = ad.mean(ad.square(inputs - mean), axis=sum_axis, keepdims=True) | ||
moving_mean = self.momentum * self.moving_mean + (1.0 - self.momentum) * mean | ||
moving_var = self.momentum * self.moving_var + (1.0 - self.momentum) * var | ||
self.add_update(self.moving_mean, ad.squeeze(moving_mean, axis=sum_axis)) | ||
self.add_update(self.moving_var, ad.squeeze(moving_var, axis=sum_axis)) | ||
return ad.where( | ||
ad.in_train_phase(), | ||
self.call_moving(inputs, moving_mean, moving_var), | ||
self.call_moving(inputs, self.moving_mean, self.moving_var), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from unittest import TestCase | ||
import numpy as np | ||
import auto_diff as ad | ||
|
||
|
||
class TestBatchNorm(TestCase): | ||
|
||
def test_no_moving(self): | ||
input_layer = ad.layers.Input(shape=(None, 5)) | ||
normal_layer = ad.layers.BatchNorm()(input_layer) | ||
dense_layer = ad.layers.Dense(output_dim=2, activation=ad.acts.softmax)(normal_layer) | ||
model = ad.models.Model(inputs=input_layer, outputs=dense_layer) | ||
model.build( | ||
optimizer=ad.optims.Adam(), | ||
losses=ad.losses.cross_entropy, | ||
) | ||
|
||
input_vals = np.random.random((2, 5)) | ||
first = model.predict_on_batch(input_vals) | ||
second = model.predict_on_batch(input_vals) | ||
self.assertTrue(np.allclose(first, second)) | ||
|
||
def test_fit(self): | ||
np.random.seed(0xcafe) | ||
input_layer = ad.layers.Input(shape=(None, 5)) | ||
normal_layer = ad.layers.BatchNorm()(input_layer) | ||
dense_layer = ad.layers.Dense(output_dim=2, activation=ad.acts.softmax)(normal_layer) | ||
model = ad.models.Model(inputs=input_layer, outputs=dense_layer) | ||
model.build( | ||
optimizer=ad.optims.Adam(), | ||
losses=ad.losses.cross_entropy, | ||
) | ||
|
||
input_vals = np.random.random((2, 5)) | ||
output_vals = np.array([[0.0, 1.0], [1.0, 0.0]]) | ||
for _ in range(5000): | ||
model.fit_on_batch(input_vals, output_vals) | ||
actual = np.argmax(model.predict_on_batch(input_vals), axis=-1).tolist() | ||
self.assertEqual([1.0, 0.0], actual) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters