This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
de_loss_vec = dfdt(nn, t) - R * t * (1 - t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final_loss = \ | |
# average over all the colocation points | |
de_loss_vec.pow(2).mean() + \ | |
# simply square the boundary contribution which is a single value | |
boundary_loss ** 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a 5-layers PINN with 5 neurons per layer | |
nn_approximator = PINN(5, 5) | |
# hyperparameters and optimizer | |
max_epochs = 10_000 | |
learning_rate = 0.01 | |
optimizer = torch.optim.Adam(nn_approximator.parameters(), lr=learning_rate) | |
# optimization loop | |
for epoch in range(max_epochs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# select a list of commits starting from head and put the in a file | |
# this is useful when selecting a set of commits to cherry-pick | |
git log --oneline | head -25 | tac | awk '{print $1}' | sed ':label1 ; N ; $! b label1 ; s/\n/\ /g' > commits.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
R = 1.0 # rate of maximum population growth parameterizing the equation | |
X_BOUNDARY = 0.0 # boundary condition coordinate | |
F_BOUNDARY = 0.5 # boundary condition value | |
def loss_fn(params: torch.Tensor, x: torch.Tensor) -> torch.Tensor: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functorch import make_functional, grad, vmap | |
# create the PINN model and make it functional using functorch utilities | |
model = NNApproximator() | |
fmodel, params = make_functional(model) | |
def f(x: torch.Tensor, params: torch.Tensor) -> torch.Tensor: | |
# only a single element is supported thus unsqueeze must be applied | |
# for batching multiple inputs, `vmap` must be used as below | |
x_ = x.unsqueeze(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# choose the configuration | |
batch_size = 30 # number of colocation points sampled in the domain | |
num_iter = 100 # maximum number of iterations | |
learning_rate = 1e-1 # learning rate | |
domain = (-5.0, 5.0) # logistic equation domain | |
# choose optimizer with functional API using functorch | |
optimizer = torchopt.FuncOptimizer(torchopt.adam(lr=learning_rate)) | |
# train the model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleNN(nn.Module): | |
def __init__( | |
self, | |
num_hidden: int = 1, | |
dim_hidden: int = 1, | |
act: nn.Module = nn.Tanh(), | |
) -> None: | |
"""Basic neural network with linear layers and non-linear activation function | |
Args: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import torch | |
from torch import nn | |
from torch import Tensor | |
from torch.func import functional_call | |
import torchopt | |
import matplotlib.pyplot as plt | |
class SimpleNN(nn.Module): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Message struct { | |
Sender string `json:"sender"` | |
Receiver string `json:"receiver"` | |
Body string `json:"body"` | |
Time string `json:"time"` | |
} | |
type User struct { | |
isOnline bool | |
msgCh chan Message |