Skip to content

Instantly share code, notes, and snippets.

@andreaschandra
Created August 26, 2021 07:59
Show Gist options
  • Save andreaschandra/3aec149444f264513f0e859304395e0a to your computer and use it in GitHub Desktop.
Save andreaschandra/3aec149444f264513f0e859304395e0a to your computer and use it in GitHub Desktop.
import torch
import torch.nn as nn
# declare Linear class
fc = nn.Linear(in_features=4, out_features=10)
print(fc)
> Linear(in_features=4, out_features=10, bias=True)
# Create a convolutional neural network
cnn = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3)
print(cnn)
> Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
# Create a LSTM network
lstm = nn.LSTM(input_size=32, hidden_size=128, num_layers=1, batch_first=True)
print(lstm)
> LSTM(32, 128, batch_first=True)
# PyTorch has its own style to create a deep learning architecture
# This is one of the way that you can build
class Architecture(nn.Module):
def __init__(self):
super(Architecture, self).__init__()
self.fc1 = nn.Linear(in_features=4, out_features=16)
self.fc2 = nn.Linear(in_features=16, out_features=16)
self.fc3 = nn.Linear(in_features=16, out_features=1)
def forward(self, x):
out = torch.relu(self.fc1(x))
out = torch.relu(self.fc2(out))
out = torch.sigmoid(self.fc3(out))
return out
# call model class
model = Architecture()
# create a dummy input
x = torch.rand(1, 4)
y_pred = model(x)
print(y_pred)
> tensor([[0.1576]], grad_fn=<AddmmBackward>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment