Skip to content

Instantly share code, notes, and snippets.

View madagra's full-sized avatar

Mario Dagrada madagra

View GitHub Profile
@madagra
madagra / union_type.go
Created January 11, 2024 21:12
Golang union type
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
@madagra
madagra / torch_func_opt_example.py
Created May 7, 2023 14:14
Fitting a function with functional PyTorch
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):
@madagra
madagra / functorch_simple_nn.py
Created April 9, 2023 11:26
Simple NN for fitting a function
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:
# 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
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)
@madagra
madagra / pinn_loss.py
Last active December 25, 2022 11:46
Loss function with physics-informed machine learning
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:
@madagra
madagra / select_commits.sh
Created January 4, 2022 16:43
Select commits for cherry-picking
#!/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
# 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):
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
de_loss_vec = dfdt(nn, t) - R * t * (1 - t)