Skip to content

Commit

Permalink
added save load test for gaussian kernel init
Browse files Browse the repository at this point in the history
  • Loading branch information
grantbuster committed May 7, 2024
1 parent 61eb6a4 commit c32187c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Test the custom tensorflow utilities
"""
import os
from tempfile import TemporaryDirectory
import numpy as np
import pytest
import tensorflow as tf
Expand All @@ -16,6 +18,7 @@
GaussianKernelInit2D,
)
from phygnn.layers.handlers import HiddenLayers, Layers
from phygnn import TfModel


@pytest.mark.parametrize(
Expand Down Expand Up @@ -472,3 +475,21 @@ def test_gaussian_kernel():

assert kernels[1].max() < kernels[0].max()
assert kernels[1].min() > kernels[0].min()

layers = [{'class': 'Conv2D', 'filters': 16, 'kernel_size': 3,
'kernel_initializer': GaussianKernelInit2D()}]
model1 = TfModel.build(['a'], ['b'], hidden_layers=layers,
input_layer=False, output_layer=False)
x_in = np.random.uniform(0, 1, (10, 12, 12, 1))
out1 = model1.predict(x_in)
kernel1 = model1.layers[0].weights[0][:, :, 0, 0].numpy()

with TemporaryDirectory() as td:
model_path = os.path.join(td, 'test_model')
model1.save_model(model_path)
model2 = TfModel.load(model_path)

kernel2 = model2.layers[0].weights[0][:, :, 0, 0].numpy()
out2 = model2.predict(x_in)
assert np.allclose(kernel1, kernel2)
assert np.allclose(out1, out2)

0 comments on commit c32187c

Please sign in to comment.