This file contains 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 basic libraries | |
import numpy as np # linear algebra | |
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) | |
from collections import OrderedDict | |
# Import PyTorch | |
import torch # import main library | |
from torch.autograd import Variable | |
import torch.nn as nn # import modules | |
from torch.autograd import Function # import Function to create custom activations |
This file contains 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
# Define a transform | |
transform = transforms.Compose([transforms.ToTensor()]) | |
# Download and load the training data for Fashion MNIST | |
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform) | |
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True) |
This file contains 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
# helper function to train a model | |
def train_model(model, trainloader): | |
''' | |
Function trains the model and prints out the training log. | |
INPUT: | |
model - initialized PyTorch model ready for training. | |
trainloader - PyTorch dataloader for training data. | |
''' | |
#setup training |
This file contains 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
# simply define a silu function | |
def silu(input): | |
''' | |
Applies the Sigmoid Linear Unit (SiLU) function element-wise: | |
SiLU(x) = x * sigmoid(x) | |
''' | |
return input * torch.sigmoid(input) # use torch.sigmoid to make sure that we created the most efficient implemetation based on builtin PyTorch functions | |
# create a class wrapper from PyTorch nn.Module, so |
This file contains 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
# use SiLU with model created with Sequential | |
# initialize activation function | |
activation_function = SiLU() | |
# Initialize the model using nn.Sequential | |
model = nn.Sequential(OrderedDict([ | |
('fc1', nn.Linear(784, 256)), | |
('activation1', activation_function), # use SiLU | |
('fc2', nn.Linear(256, 128)), |
This file contains 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 class for basic fully-connected deep neural network | |
class ClassifierSiLU(nn.Module): | |
''' | |
Demo classifier model class to demonstrate SiLU | |
''' | |
def __init__(self): | |
super().__init__() | |
# initialize layers | |
self.fc1 = nn.Linear(784, 256) |
This file contains 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 soft_exponential(nn.Module): | |
''' | |
Implementation of soft exponential activation. | |
Shape: | |
- Input: (N, *) where * means, any number of additional | |
dimensions | |
- Output: (N, *), same shape as the input | |
Parameters: |
This file contains 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 class for basic fully-connected deep neural network | |
class ClassifierSExp(nn.Module): | |
''' | |
Basic fully-connected network to test Soft Exponential activation. | |
''' | |
def __init__(self): | |
super().__init__() | |
# initialize layers | |
self.fc1 = nn.Linear(784, 256) |
This file contains 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 brelu(Function): | |
''' | |
Implementation of BReLU activation function. | |
Shape: | |
- Input: (N, *) where * means, any number of additional | |
dimensions | |
- Output: (N, *), same shape as the input | |
References: |
This file contains 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 ClassifierBReLU(nn.Module): | |
''' | |
Simple fully-connected classifier model to demonstrate BReLU activation. | |
''' | |
def __init__(self): | |
super(ClassifierBReLU, self).__init__() | |
# initialize layers | |
self.fc1 = nn.Linear(784, 256) | |
self.fc2 = nn.Linear(256, 128) |
OlderNewer