style/variable name changes
@@ -308,7 +308,8 @@ def solve_system(
valid_generator = ExampleGenerator(32, t_min, t_max, method='equally-spaced')
if not optimizer:
all_parameters = []
- for net in nets: all_parameters += list(net.parameters())
+ for net in nets:
+ all_parameters += list(net.parameters())
optimizer = optim.Adam(all_parameters, lr=0.001)
if not criterion:
criterion = nn.MSELoss()
@@ -339,7 +340,8 @@ def solve_system(
batch_start, batch_end = 0, batch_size
while batch_start < n_examples_train:
- if batch_end >= n_examples_train: batch_end = n_examples_train
+ if batch_end >= n_examples_train:
+ batch_end = n_examples_train
batch_idx = idx[batch_start:batch_end]
ts = train_examples[batch_idx]
@@ -350,9 +352,10 @@ def solve_system(
v_i = conditions[i].enforce(nets[i], ts)
vs.append(v_i)
- Fvts = ode_system(*vs, ts)
+ fvts = ode_system(*vs, ts)
loss = 0.0
- for Fvt in Fvts: loss += criterion(Fvt, train_zeros)
+ for fvt in fvts:
+ loss += criterion(fvt, train_zeros)
train_loss_epoch += loss.item() * (batch_end-batch_start)/n_examples_train # assume the loss is a mean over all examples
optimizer.zero_grad()
@@ -371,9 +374,10 @@ def solve_system(
if conditions[i]:
v_i = conditions[i].enforce(nets[i], ts)
vs.append(v_i)
- Fvts = ode_system(*vs, ts)
+ fvts = ode_system(*vs, ts)
valid_loss_epoch = 0.0
- for Fvt in Fvts: valid_loss_epoch += criterion(Fvt, valid_zeros)
+ for fvt in fvts:
+ valid_loss_epoch += criterion(fvt, valid_zeros)
valid_loss_epoch = valid_loss_epoch.item()
loss_history['valid'].append(valid_loss_epoch)
@@ -382,16 +386,19 @@ def solve_system(
monitor.check(nets, conditions, loss_history)
def solution(ts, as_type='tf'):
- if not isinstance(ts, torch.Tensor): ts = torch.tensor([ts], dtype=torch.float32)
+ if not isinstance(ts, torch.Tensor):
+ ts = torch.tensor([ts], dtype=torch.float32)
ts = ts.reshape(-1, 1)
- results = []
+ vs = []
for i in range(len(conditions)):
- xs = conditions[i].enforce(nets[i], ts)
- if as_type == 'tf': results.append(xs)
- elif as_type == 'np': results.append(xs.detach().numpy().flatten())
+ v_i = conditions[i].enforce(nets[i], ts)
+ if as_type == 'tf':
+ vs.append(v_i)
+ elif as_type == 'np':
+ vs.append(v_i.detach().numpy().flatten())
else:
raise ValueError("The valid return types are 'tf' and 'np'.")
- return results
+ return vs
if return_internal:
return solution, loss_history, internal
