Skip to content

Instantly share code, notes, and snippets.

View anderzzz's full-sized avatar
🌅
Robotu, robotu, compute!

Anders Ohrn anderzzz

🌅
Robotu, robotu, compute!
  • Switzerland
View GitHub Profile
@anderzzz
anderzzz / paper_snippet_disk_trace.py
Last active February 25, 2020 03:37
Bokeh snippet to draw disk trace in paper trade analysis
#
# Inner loop to draw trace of disk of import/export fraction
#
# Create Bokeh figure
p = figure(plot_width=650, plot_height=500, toolbar_location='above')
# Pick color for country
color = brewer['PRGn'][n_countries][k_country]
#
# Main data transformation part for Export-Import flow stacked bar
#
# Loop over paper types to create the export and import column data sources
data_to_dict = [('country', countries)]
for item in items_names:
d8 = df.loc[df['Item'] == item]
d8_exp = d8.loc[d8['Element'] == 'Export Quantity']
d8_imp = d8.loc[d8['Element'] == 'Import Quantity']
@anderzzz
anderzzz / temporal_11_london.py
Last active June 7, 2020 20:35
1D Convolution on time dimension
import torch
import torch.nn.functional as F
class Time1dConvGLU(torch.nn.Module):
def __init__(self, channel_inputs, channel_outputs, time_convolution_length):
super(Time1dConvGLU, self).__init__()
self.one_d_conv_1 = torch.nn.Conv1d(in_channels=channel_inputs,
out_channels=2 * channel_outputs,
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, TAGConv
class SpatialGraphConv(torch.nn.Module):
def __init__(self, channel_inputs, channel_outputs, graph_conv_type, graph_conv_kwargs):
super(SpatialGraphConv, self).__init__()
@anderzzz
anderzzz / london_training.py
Created June 8, 2020 23:12
The essentials of the training for London bike forecast
import torch
import torch.nn.functional as F
from torch_geometric.data import DataLoader
from my_stuff import STGCN
from my_stuff import LondonBikeDataset
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
london_bike_data = LondonBikeDataset()
@anderzzz
anderzzz / retransforms.py
Created October 20, 2020 08:08
Pre-processing classes
class StandardTransform(object):
'''Standard Image Transforms, typically instantiated and provided to the DataSet class
'''
def __init__(self, min_dim=299, to_tensor=True, square=False,
normalize=True, norm_mean=[0.485, 0.456, 0.406], norm_std=[0.229, 0.224, 0.225]):
self.transforms = []
self.transforms.append(transforms.ToPILImage())
self.transforms.append(transforms.Resize(min_dim))
@anderzzz
anderzzz / encoder_init.py
Created October 30, 2020 14:15
First part of encoder based on VGG16
import torch
from torch import nn
from torchvision import models
class EncoderVGG(nn.Module):
'''Encoder of image based on the architecture of VGG-16 with batch normalization.
Args:
pretrained_params (bool, optional): If the network should be populated with pre-trained VGG parameters.
Defaults to True.
@anderzzz
anderzzz / _encodify.py
Created October 30, 2020 14:23
Encodification method to handle pooling indices
def _encodify_(self, encoder):
'''Create list of modules for encoder based on the architecture in VGG template model.
In the encoder-decoder architecture, the unpooling operations in the decoder require pooling
indices from the corresponding pooling operation in the encoder. In VGG template, these indices
are not returned. Hence the need for this method to extent the pooling operations.
Args:
encoder : the template VGG model
@anderzzz
anderzzz / encode_forward.py
Created October 30, 2020 14:27
Forward method for encoder
def forward(self, x):
'''Execute the encoder on the image input
Args:
x (Tensor): image tensor
Returns:
x_code (Tensor): code tensor
pool_indices (list): Pool indices tensors in order of the pooling modules
@anderzzz
anderzzz / decoder_init.py
Created October 30, 2020 14:41
Initialization part of decoder based on VGG
class DecoderVGG(nn.Module):
'''Decoder of code based on the architecture of VGG-16 with batch normalization.
Args:
encoder: The encoder instance of `EncoderVGG` that is to be inverted into a decoder
'''
channels_in = EncoderVGG.channels_code
channels_out = 3