Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
Correcting code
  • Loading branch information
dimerf99 committed Dec 16, 2024
1 parent 20cc8ab commit c9e7038
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions tedeous/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def create_random_fn(eps: float) -> callable:
Returns:
callable: creating random params function.
"""

def randomize_params(m):
if (isinstance(m, torch.nn.Linear) or isinstance(m, torch.nn.Conv2d)) and m.bias is not None:
m.weight.data = m.weight.data + \
Expand Down Expand Up @@ -72,9 +73,9 @@ def lambda_print(lam: torch.Tensor, keys: List) -> None:


def bcs_reshape(
bval: torch.Tensor,
true_bval: torch.Tensor,
bval_length: List) -> Tuple[dict, dict, dict, dict]:
bval: torch.Tensor,
true_bval: torch.Tensor,
bval_length: List) -> Tuple[dict, dict, dict, dict]:
""" Preprocessing for lambda evaluating.
Args:
Expand All @@ -91,7 +92,7 @@ def bcs_reshape(
bval_diff = bval - true_bval

bcs = torch.cat([bval_diff[0:bval_length[i], i].reshape(-1)
for i in range(bval_diff.shape[-1])])
for i in range(bval_diff.shape[-1])])

return bcs

Expand Down Expand Up @@ -136,8 +137,8 @@ def mat_op_coeff(equation: Any) -> Any:


def model_mat(model: torch.Tensor,
domain: Any,
cache_model: torch.nn.Module=None) -> Tuple[torch.Tensor, torch.nn.Module]:
domain: Any,
cache_model: torch.nn.Module = None) -> Tuple[torch.Tensor, torch.nn.Module]:
""" Create model for *NN or autograd* modes from grid
and model of *mat* mode.
Expand Down Expand Up @@ -169,9 +170,9 @@ def model_mat(model: torch.Tensor,


def save_model_nn(
cache_dir: str,
model: torch.nn.Module,
name: Union[str, None] = None) -> None:
cache_dir: str,
model: torch.nn.Module,
name: Union[str, None] = None) -> None:
"""
Saves model in a cache (uses for 'NN' and 'autograd' methods).
Args:
Expand All @@ -187,24 +188,24 @@ def save_model_nn(
os.mkdir(cache_dir)

parameters_dict = {'model': model.to('cpu'),
'model_state_dict': model.state_dict()}
'model_state_dict': model.state_dict()}

try:
torch.save(parameters_dict, cache_dir + '\\' + name + '.tar')
print(f'model is saved in cache dir: {cache_dir}')
except RuntimeError:
torch.save(parameters_dict, cache_dir + '\\' + name + '.tar',
_use_new_zipfile_serialization=False) # cyrillic in path
_use_new_zipfile_serialization=False) # cyrillic in path
print(f'model is saved in cache: {cache_dir}')
except:
print(f'Cannot save model in cache: {cache_dir}')


def save_model_mat(cache_dir: str,
model: torch.Tensor,
domain: Any,
cache_model: Union[torch.nn.Module, None] = None,
name: Union[str, None] = None) -> None:
model: torch.Tensor,
domain: Any,
cache_model: Union[torch.nn.Module, None] = None,
name: Union[str, None] = None) -> None:
""" Saves model in a cache (uses for 'mat' method).
Args:
Expand Down Expand Up @@ -232,7 +233,7 @@ def closure():
loss = optimizer.step(closure)
t += 1
print('Interpolate from trained model t={}, loss={}'.format(
t, loss))
t, loss))

save_model_nn(cache_dir, net_autograd, name=name)

Expand Down Expand Up @@ -296,26 +297,20 @@ def exact_solution_data(grid, datapath, n_dim_in, n_dim_out):
test_data = np.loadtxt(datapath, comments="%", encoding='utf-8').astype(np.float32)
test_data = torch.from_numpy(test_data)

if n_dim_out == 1:
function = test_data[:, n_dim_in:]
grid_data = torch.stack([coord for coord in test_data[:, :n_dim_in]])

grid_data = grid_data.cpu().numpy()
function = function.cpu().numpy()
grid = grid.cpu().numpy()
exact_func = test_data[:, n_dim_in:]
grid_data = torch.stack([coord for coord in test_data[:, :n_dim_in]])

exact = scipy.interpolate.griddata(grid_data, function, grid, method='nearest').reshape(-1)
grid_data = grid_data.cpu().numpy()
exact_func = exact_func.cpu().numpy()
grid = grid.cpu().numpy()

if n_dim_out == 1:
exact = scipy.interpolate.griddata(grid_data, exact_func, grid, method='nearest').reshape(-1)
else:
functions = test_data[:, n_dim_in:]
grid_data = torch.stack([coord for coord in test_data[:, :n_dim_in]])

grid_data = grid_data.cpu().numpy()
functions = functions.cpu().numpy()
grid = grid.cpu().numpy()

exact = np.array([scipy.interpolate.griddata(grid_data, functions[:, i_dim], grid, method='nearest').reshape(-1)
for i_dim in range(n_dim_out)])
exact = np.array(
[scipy.interpolate.griddata(grid_data, exact_func[:, i_dim], grid, method='nearest').reshape(-1)
for i_dim in range(n_dim_out)]
)

exact = torch.from_numpy(exact).to(device_origin)
return exact
return exact

0 comments on commit c9e7038

Please sign in to comment.