Skip to content

Commit

Permalink
fix a type float32 or float64 bug when loading B from FFT run
Browse files Browse the repository at this point in the history
  • Loading branch information
enigne committed Nov 11, 2024
1 parent 563e9f9 commit 3d4934c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions pinnicle/nn/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def __init__(self, parameters=NNParameter()):
if self.parameters.fft :
print(f"add Fourier feature transform to input transform")
if self.parameters.B is not None:
self.B = bkd.as_tensor(self.parameters.B)
self.B = bkd.as_tensor(self.parameters.B, dtype=dde.config.default_float())
else:
self.B = bkd.as_tensor(np.random.normal(0.0, self.parameters.sigma, [len(self.parameters.input_variables), self.parameters.num_fourier_feature]))
self.B = bkd.as_tensor(np.random.normal(0.0, self.parameters.sigma, [len(self.parameters.input_variables), self.parameters.num_fourier_feature]), dtype=dde.config.default_float())
def wrapper(x):
"""a wrapper function to add fourier feature transform to the input
"""
Expand All @@ -36,8 +36,8 @@ def wrapper(x):
print(f"add input transform with {self.parameters.input_lb} and {self.parameters.input_ub}")
# force the input and output lb and ub to be tensors
if bkd.backend_name == "pytorch":
self.parameters.input_lb = bkd.as_tensor(self.parameters.input_lb)
self.parameters.input_ub = bkd.as_tensor(self.parameters.input_ub)
self.parameters.input_lb = bkd.as_tensor(self.parameters.input_lb, dtype=dde.config.default_float())
self.parameters.input_ub = bkd.as_tensor(self.parameters.input_ub, dtype=dde.config.default_float())
# add input transform
self._add_input_transform(minmax_scale)

Expand All @@ -46,8 +46,8 @@ def wrapper(x):
print(f"add output transform with {self.parameters.output_lb} and {self.parameters.output_ub}")
# force the input and output lb and ub to be tensors
if bkd.backend_name == "pytorch":
self.parameters.output_lb = bkd.as_tensor(self.parameters.output_lb)
self.parameters.output_ub = bkd.as_tensor(self.parameters.output_ub)
self.parameters.output_lb = bkd.as_tensor(self.parameters.output_lb, dtype=dde.config.default_float())
self.parameters.output_ub = bkd.as_tensor(self.parameters.output_ub, dtype=dde.config.default_float())
# add output transform
self._add_output_transform(up_scale)

Expand Down
13 changes: 8 additions & 5 deletions tests/test_nn.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import pinnicle as pinn
from pinnicle.nn.helper import minmax_scale, up_scale, fourier_feature
from pinnicle.parameter import NNParameter
import deepxde as dde
import deepxde.backend as bkd
from deepxde.backend import backend_name
import pytest
import numpy as np

dde.config.set_default_float('float64')

def test_minmax_scale():
lb = 1.0
ub = 10.0
Expand All @@ -22,8 +25,8 @@ def test_upscale():

@pytest.mark.skipif(backend_name=="jax", reason="bkd.matmul is not implemented for jax")
def test_fourier_feature():
x = bkd.reshape(bkd.as_tensor((np.linspace(1,100, 100)), dtype=bkd.data_type_dict['float64']), [50,2])
B = bkd.as_tensor(np.random.normal(0.0, 10.0, [x.shape[1], 2]), dtype=bkd.data_type_dict['float64'])
x = bkd.reshape(bkd.as_tensor((np.linspace(1,100, 100)), dtype=dde.config.default_float()), [50,2])
B = bkd.as_tensor(np.random.normal(0.0, 10.0, [x.shape[1], 2]), dtype=dde.config.default_float())
y = bkd.to_numpy(fourier_feature(x, B))
z = y**2
assert np.all((z[:,1]+z[:,3]) < 1.0+100**np.finfo(float).eps)
Expand All @@ -50,7 +53,7 @@ def test_input_fft_nn():
d.input_lb = 1.0
d.input_ub = 10.0
p = pinn.nn.FNN(d)
x = bkd.reshape(bkd.as_tensor(np.linspace(d.input_lb, d.input_ub, 100)), [100,1])
x = bkd.reshape(bkd.as_tensor(np.linspace(d.input_lb, d.input_ub, 100), dtype=dde.config.default_float()), [100,1])
y = bkd.to_numpy(p.net._input_transform(x))
z = y**2
assert np.all(abs(z[:,1:10]+z[:,11:20]) <= 1.0+np.finfo(float).eps)
Expand All @@ -71,7 +74,7 @@ def test_input_scale_nn():
d.input_lb = 1.0
d.input_ub = 10.0
p = pinn.nn.FNN(d)
x = bkd.as_tensor(np.linspace(d.input_lb, d.input_ub, 100))
x = bkd.as_tensor(np.linspace(d.input_lb, d.input_ub, 100), dtype=dde.config.default_float())
y = bkd.to_numpy(p.net._input_transform(x))
assert np.all(abs(y) <= 1.0+np.finfo(float).eps)

Expand All @@ -85,7 +88,7 @@ def test_output_scale_nn():
d.output_lb = 1.0
d.output_ub = 10.0
p = pinn.nn.FNN(d)
x = bkd.as_tensor(np.linspace(-1.0, 1.0, 100))
x = bkd.as_tensor(np.linspace(-1.0, 1.0, 100), dtype=dde.config.default_float())
y = [0.0]
out = bkd.to_numpy(p.net._output_transform(y,x))
assert np.all(out >= 1.0 - 1.0*np.finfo(float).eps)
Expand Down

0 comments on commit 3d4934c

Please sign in to comment.