# (C) Copyright IBM Corp. 2019, 2020, 2021, 2022.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from unittest import TestCase
import os
from simulai.regression import DenseNetwork, ConvolutionalNetwork
from simulai.models import ModelMaker
from simulai.math.progression import gp
from simulai.metrics import L2Norm
from simulai.special import Scattering, bidimensional_map_nonlin_3, time_function
# NOTE: This tests intend just verify the execution success rather than the
# approximation error
[docs]class TestAutoencoder(TestCase):
[docs] def setUp(self) -> None:
pass
'''Testing the convolutional network execution and making
important checkpoints
'''
# Now we will use a bottleneck network in the middle of the process
[docs] def test_convolution_2D(self):
# Generating manufactured data
Nx = int(64)
Ny = int(64)
Nt = int(1e3)
x = np.linspace(0, 1, Nx)
y = np.linspace(0, 1, Ny)
t = np.linspace(0, 100, Nt)
T, X, Y = np.meshgrid(t, x, y, indexing='ij')
generator = Scattering(root=time_function,
scatter_op=bidimensional_map_nonlin_3)
Z_ = generator.exec(data=T, scatter_data=(X, Y, 0.5, 0.5))
Z_ *= generator.exec(data=T, scatter_data=(X, Y, 0.25, 0.25))
data = np.concatenate([Z_[..., None], 2*Z_[..., None], 3*Z_[..., None]], axis=-1)
n_samples = data.shape[0]
train_size = int(0.6 * n_samples)
train_data = data[:train_size, ...]
test_data = data[train_size:, ...]
n_variables = data.shape[-1]
latent_dim = 16
target_data = np.ones((train_size, latent_dim))
# Configuring the network algorithms
arch_conv2D = [{'kind': 'conv', 'filters': n_variables, 'kernel_size': (3, 3), 'strides': [4, 4]},
{'kind': 'batch_normalization', 'axis': -1},
{'kind': 'relu'},
{'kind': 'conv', 'filters': 8, 'kernel_size': (3, 3), 'strides': [4, 4]},
{'kind': 'batch_normalization', 'axis': -1},
{'kind': 'relu'},
{'kind': 'conv', 'filters': latent_dim, 'kernel_size': (3, 3), 'strides': [2, 2]},
{'kind': 'batch_normalization', 'axis': -1},
{'kind': 'relu'}]
encoder_setup = {
'space_dimension': '2D',
'process': 'convolution'
}
conv_net = ConvolutionalNetwork(architecture=arch_conv2D, config=encoder_setup,
model_id='conv2D')
optimizers_list = {"Adam": {"maxiter": 100}}
residuals_type = ['surrogate']
losses = ['square-mean']
model = ModelMaker(regressions=[conv_net], optimizers_list=optimizers_list,
residuals_type=residuals_type, losses=losses,
data_residuals=['code'],
regularizations=[{'l2_reg': 1e-5, 'l1_reg': 0}])
model.fit(input_data_list=[train_data],
target_data_list=[target_data])