Skip to content

Instantly share code, notes, and snippets.

View dvgodoy's full-sized avatar

Daniel Voigt Godoy dvgodoy

View GitHub Profile
torch.manual_seed(42)
x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()
# Builds dataset with ALL data
dataset = TensorDataset(x_tensor, y_tensor)
# Splits randomly into train and validation datasets
train_dataset, val_dataset = random_split(dataset, [80, 20])
import torch
import torch.optim as optim
import torch.nn as nn
from torchviz import make_dot
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Our data was in Numpy arrays, but we need to transform them into PyTorch's Tensors
# and then we send them to the chosen device
x_train_tensor = torch.from_numpy(x_train).float().to(device)
# Data Generation
np.random.seed(42)
x = np.random.rand(100, 1)
y = 1 + 2 * x + .1 * np.random.randn(100, 1)
# Shuffles the indices
idx = np.arange(100)
np.random.shuffle(idx)
# Uses first 80 random indices for train
@dvgodoy
dvgodoy / draw_neural_net.py
Last active December 16, 2022 06:44
Draw neural network diagram with Matplotlib
## Gist originally developed by @craffel and improved by @ljhuang2017
import matplotlib.pyplot as plt
import numpy as np
def draw_neural_net(ax, left, right, bottom, top, layer_sizes, coefs_, intercepts_, n_iter_, loss_):
'''
Draw a neural network cartoon using matplotilb.
:usage:
#!pip install ruptures
import ruptures as rpt
from ruptures.exceptions import BadSegmentationParameters
def change_points(data, model='l1', pen=3):
signal = data.copy()
# If there are no valid data points, returns one regime
# that starts at 0, ends at the full length, and is nan
if np.all(np.isnan(signal)):
return np.array([0, len(signal)]), np.array([np.nan])
from scipy import interpolate
def fill_values(data,
max_contiguous_na=5,
periods_for_extrapolation=5,
method='slinear'):
time = np.arange(len(data))
signal = data.copy()
# If there are no valid data points, there's nothing to fill
if np.all(np.isnan(signal)):
@dvgodoy
dvgodoy / surface.py
Last active November 24, 2022 16:54
#!pip install area
from area import area
from functools import partial
def make_polygon(coords, size):
left_long, bottom_lat = coords
return np.array([[[left_long, bottom_lat],
[left_long, bottom_lat + size],
[left_long + size, bottom_lat + size],
[left_long + size, bottom_lat],
@dvgodoy
dvgodoy / data.py
Last active November 24, 2022 16:45
import numpy as np
import netCDF4 as nc
import requests
import os
def download(url=None, cached_etag=None):
try:
if url is None:
base = b'https://www.ncei.noaa.gov/data/noaa-global-surface-temperature/v5/access/gridded/'
resp = requests.get(base)
def series_changes(filled, model='l1', pen=3):
# Applies the change_points function over every filled series
changes = np.apply_along_axis(func1d=lambda s: change_points(s, model, pen),
arr=filled.reshape(filled.shape[0], -1).T,
axis=1)
changes = changes.reshape(*filled.shape[1:], 2)
return changes
def series_evolution(gridded, changes, offset=True, keep_missing=True):
# Applies the change_evolution function over every grid box
def change_evolution(changes, offset=True):
breaks, averages = changes
# Creates a full series using the break points and using the average
# of each regime
avg_series = np.concatenate([[averages[i]] * (breaks[i + 1] - breaks[i])
for i in range(len(averages))])
# If offset, shifts the starting point to zero
if offset:
avg_series -= avg_series[0]
# If the first break is other than zero, concatenates empty data at