After training a model, how can I save the parameters of the network (e.g., weights and biases) and apply them to a new equal network BUT where I want to change only the first two weights?
I would like to see how the loss changes when only the first two weights varies (providing a meshgrid space) and all the others are fixed.
- How to fix the weights and biases except two?
- How to provide the domain in which the two free weights can span?
class Net(nn.Module):
def __init__(self, layer_sizes, activation=torch.tanh):
super(Net, self).__init__()
self.layer_sizes = layer_sizes
self.layer_list = [x]
self.activation = activation
self.l1 = nn.Linear(layer_sizes[0], layer_sizes[1])
self.l2 = nn.Linear(layer_sizes[1], layer_sizes[2])
self.l3 = nn.Linear(layer_sizes[2], layer_sizes[3])
self.l4 = nn.Linear(layer_sizes[3], layer_sizes[4])
def forward(self, x):
x = self.l1(x)
#print(x.dtype)
x = self.activation(x)
x = self.l2(x)
x = self.activation(x)
x = self.l3(x)
x = self.activation(x)
x = self.l4(x)
return x
device = 'cpu'
layer_sizes = [1,32,32,32,1]
activation = torch.tanh
net_u = Net(layer_sizes, activation).to(device)
... # training
# saving
info2exp = []
for layer in nets[0].children():
if isinstance(layer, nn.Linear):
info2exp.append(layer.state_dict())
torch.save([data, epochs, info2exp], r'./dataStorage/test_v1.pt')
How to load them to a new network? Like this?
net_new = Net(layer_sizes, activation).to(device)
net_new.layers.load_state_dict(net_u.layers.state_dict())
print((net_new.layers[0].weight == net_u.layers[0].weight).all())
- How to fix the weights and biases except two?
- How to provide the domain in which the two free weights can span?
Thanks!