Forward pass#
# This is only valid when the package is not installed
import sys
sys.path.append('../../') # two folders up
import DeepINN as dp
import torch
Using default backend: PyTorch
Using Pytorch: 2.0.1+cu117
activation = "tanh"
initialiser = "Xavier normal"
layer_size = [1] + [5] * 3 + [1]
net = dp.nn.FullyConnected(layer_size, activation, initialiser)
net.linears
ModuleList(
(0): Linear(in_features=1, out_features=5, bias=True)
(1-2): 2 x Linear(in_features=5, out_features=5, bias=True)
(3): Linear(in_features=5, out_features=1, bias=True)
)
# net.linears[2].weight.data
# net.initialiser
input = torch.randn(3, 1)
input
tensor([[-1.0178],
[-1.9623],
[-0.3584]])
forward = net.forward(input)
forward
tensor([[0.0563],
[0.0000],
[0.0799]], grad_fn=<AddmmBackward0>)
net.activation(input)
tensor([[-0.7690],
[-0.9613],
[-0.3438]])
loss_metric = dp.backend.loss_metric("MSE")
loss_metric
MSELoss()
loss_metric(input, forward)
tensor(1.7321, grad_fn=<MseLossBackward0>)