Last active
May 7, 2020 03:19
-
-
Save PulkitS01/961cde8f6ebe63517f21af223981b132 to your computer and use it in GitHub Desktop.
Introduction to PyTorch
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
# initializing two arrays | |
a = np.array(2) | |
b = np.array(1) | |
print(a,b) |
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
# initializing a numpy array | |
a = np.array(1) | |
# initializing a tensor | |
b = torch.tensor(1) | |
print(a) | |
print(b) |
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
# back propagating | |
c.backward() | |
# computing gradients | |
print(a.grad) |
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
# concatenating horizontally | |
torch.cat((a,b),dim=1) |
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
# concatenating vertically | |
torch.cat((a,b)) |
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
# initializing a numpy array | |
a = np.array([[1,2],[3,4]]) | |
print(a, '\n') | |
# converting the numpy array to tensor | |
tensor = torch.from_numpy(a) | |
print(tensor) |
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
# loading dataset | |
train = pd.read_csv('train_LbELtWX/train.csv') | |
test = pd.read_csv('test_ScVgIM0/test.csv') | |
sample_submission = pd.read_csv('sample_submission_I5njJSF.csv') | |
train.head() |
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
# back propagating | |
c.backward() | |
# computing gradients | |
print(a.grad) |
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
# initializing two tensors | |
a = torch.tensor([[1,2],[3,4]]) | |
b = torch.tensor([[5,6],[7,8]]) | |
print(a, '\n') | |
print(b) |
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
#Input tensor | |
X = torch.Tensor([[1,0,1,0],[1,0,1,1],[0,1,0,1]]) | |
#Output | |
y = torch.Tensor([[1],[1],[0]]) | |
print(X, '\n') | |
print(y) |
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
# importing libraries | |
import numpy as np | |
import torch |
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
# importing the libraries | |
import pandas as pd | |
import numpy as np | |
from skimage.io import imread | |
import matplotlib.pyplot as plt | |
%matplotlib inline | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score |
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
# plotting the training and validation loss | |
plt.plot(train_losses, label='Training loss') | |
plt.plot(val_losses, label='Validation loss') | |
plt.legend() | |
plt.show() |
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
# addition | |
print(a+b) | |
# subtraction | |
print(b-a) | |
# multiplication | |
print(a*b) | |
# division | |
print(a/b) |
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
# addition | |
print(a+b) | |
# subtraction | |
print(b-a) | |
# multiplication | |
print(a*b) | |
# division | |
print(a/b) |
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
# matrix of zeros | |
a = np.zeros((3,3)) | |
print(a) | |
print(a.shape) |
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
# matrix addition | |
print(np.add(a,b), '\n') | |
# matrix subtraction | |
print(np.subtract(a,b), '\n') | |
# matrix multiplication | |
print(np.dot(a,b), '\n') | |
# matrix multiplication | |
print(np.divide(a,b)) |
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
# matrix addition | |
print(torch.add(a,b), '\n') | |
# matrix subtraction | |
print(torch.sub(a,b), '\n') | |
# matrix multiplication | |
print(torch.mm(a,b), '\n') | |
# matrix division | |
print(torch.div(a,b)) |
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
# matrix of zeros | |
a = torch.zeros((3,3)) | |
print(a) | |
print(a.shape) |
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 model | |
model = Sequential(Linear(input_num_units, hidden_num_units), | |
ReLU(), | |
Linear(hidden_num_units, output_num_units)) | |
# loss function | |
loss_fn = CrossEntropyLoss() | |
# define optimization algorithm | |
optimizer = Adam(model.parameters(), lr=learning_rate) |
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
train_losses = [] | |
val_losses = [] | |
for epoch in range(epochs): | |
avg_cost = 0 | |
x, y = Variable(torch.from_numpy(train_x)), Variable(torch.from_numpy(train_y), requires_grad=False) | |
x_val, y_val = Variable(torch.from_numpy(val_x)), Variable(torch.from_numpy(val_y), requires_grad=False) | |
pred = model(x) | |
pred_val = model(x_val) | |
# get loss | |
loss = loss_fn(pred, y) | |
loss_val = loss_fn(pred_val, y_val) | |
train_losses.append(loss) | |
val_losses.append(loss_val) | |
# perform backpropagation | |
loss.backward() | |
optimizer.step() | |
avg_cost = avg_cost + loss.data | |
if (epoch%2 != 0): | |
print(epoch+1, avg_cost) |
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
# number of neurons in each layer | |
input_num_units = 28*28 | |
hidden_num_units = 500 | |
output_num_units = 10 | |
# set remaining variables | |
epochs = 20 | |
learning_rate = 0.0005 |
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
for i in range(epoch): | |
#Forward Propogation | |
hidden_layer_input1 = torch.mm(X, wh) | |
hidden_layer_input = hidden_layer_input1 + bh | |
hidden_layer_activations = sigmoid(hidden_layer_input) | |
output_layer_input1 = torch.mm(hidden_layer_activations, wout) | |
output_layer_input = output_layer_input1 + bout | |
output = sigmoid(output_layer_input) | |
#Backpropagation | |
E = y-output | |
slope_output_layer = derivatives_sigmoid(output) | |
slope_hidden_layer = derivatives_sigmoid(hidden_layer_activations) | |
d_output = E * slope_output_layer | |
Error_at_hidden_layer = torch.mm(d_output, wout.t()) | |
d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer | |
wout += torch.mm(hidden_layer_activations.t(), d_output) *lr | |
bout += d_output.sum() *lr | |
wh += torch.mm(X.t(), d_hiddenlayer) *lr | |
bh += d_output.sum() *lr |
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
print('actual :\n', y, '\n') | |
print('predicted :\n', output) |
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
# importing the optim module | |
from torch import optim | |
# adam | |
## adam = optim.Adam(model.parameters(), lr=learning_rate) | |
# sgd | |
## SGD = optim.SGD(model.parameters(), lr=learning_rate) |
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
# print an image | |
img_name = rng.choice(train['id']) | |
filepath = 'train_LbELtWX/train/' + str(img_name) + '.png' | |
img = imread(filepath, as_gray=True) | |
img = img.astype('float32') | |
plt.figure(figsize=(5,5)) | |
plt.imshow(img, cmap='gray') |
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
# getting the prediction for test images | |
prediction = np.argmax(model(torch.from_numpy(test_x)).data.numpy(), axis=1) |
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
# replacing the label with prediction | |
sample_submission['label'] = prediction | |
sample_submission.head() |
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
# setting the random seed for numpy | |
np.random.seed(42) | |
# matrix of random numbers | |
a = np.random.randn(3,3) | |
a |
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
# setting the random seed for pytorch | |
torch.manual_seed(42) | |
# matrix of random numbers | |
a = torch.randn(3,3) | |
a |
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
# setting the random seed for numpy and initializing two matrices | |
np.random.seed(42) | |
a = np.random.randn(3,3) | |
b = np.random.randn(3,3) |
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
# random number generator | |
seed = 128 | |
rng = np.random.RandomState(seed) |
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
# setting the random seed for pytorch and initializing two tensors | |
torch.manual_seed(42) | |
a = torch.randn(3,3) | |
b = torch.randn(3,3) |
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
# setting the random seed for pytorch | |
torch.manual_seed(42) | |
# initializing tensor | |
a = torch.randn(2,4) | |
print(a) | |
a.shape |
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
# reshaping tensor | |
b = a.reshape(1,8) | |
print(b) | |
b.shape |
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
# first five rows of sample submission file | |
sample_submission.head() |
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
# saving the file | |
sample_submission.to_csv('submission.csv', index=False) |
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
#Sigmoid Function | |
def sigmoid (x): | |
return 1/(1 + torch.exp(-x)) | |
#Derivative of Sigmoid Function/ | |
def derivatives_sigmoid(x): | |
return sigmoid(x) * (1 - sigmoid(x)) |
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
train_y = train['label'].values |
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
# initializing two tensors | |
a = torch.tensor(2) | |
b = torch.tensor(1) | |
print(a,b) |
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
# initializing a tensor | |
a = torch.ones((2,2), requires_grad=True) | |
a |
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
# performing operations on the tensor | |
b = a + 5 | |
c = b.mean() | |
print(b,c) |
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
# loading test images | |
test_img = [] | |
for img_name in test['id']: | |
image_path = 'test_ScVgIM0/test/' + str(img_name) + '.png' | |
img = imread(image_path, as_gray=True) | |
img = img.astype('float32') | |
test_img.append(img) | |
test_x = np.array(test_img) | |
test_x.shape |
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
# converting the images to 1-D | |
test_x = test_x/train_x.max() | |
test_x = test_x.reshape(-1, 28*28).astype('float32') | |
test_x.shape |
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 torch | |
from torch.autograd import Variable | |
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential | |
from torch.optim import Adam |
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
# get training accuracy | |
x, y = Variable(torch.from_numpy(train_x)), Variable(torch.from_numpy(train_y), requires_grad=False) | |
pred = model(x) | |
final_pred = np.argmax(pred.data.numpy(), axis=1) | |
accuracy_score(train_y, final_pred) |
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
# loading training images | |
train_img = [] | |
for img_name in train['id']: | |
image_path = 'train_LbELtWX/train/' + str(img_name) + '.png' | |
img = imread(image_path, as_gray=True) | |
img = img.astype('float32') | |
train_img.append(img) | |
train_x = np.array(train_img) | |
train_x.shape |
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
train_x = train_x/train_x.max() | |
train_x = train_x.reshape(-1, 28*28).astype('float32') | |
train_x.shape |
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
# original matrix | |
print(a, '\n') | |
# matrix transpose | |
print(np.transpose(a)) |
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
# original matrix | |
print(a, '\n') | |
# matrix transpose | |
torch.t(a) |
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
type(a), type(b) |
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
# get validation accuracy | |
x, y = Variable(torch.from_numpy(val_x)), Variable(torch.from_numpy(val_y), requires_grad=False) | |
pred = model(x) | |
final_pred = np.argmax(pred.data.numpy(), axis=1) | |
accuracy_score(val_y, final_pred) |
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 validation set | |
train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size = 0.1, stratify = train_y) | |
(train_x.shape, train_y.shape), (val_x.shape, val_y.shape) |
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
#Variable initialization | |
epoch=7000 #Setting training iterations | |
lr=0.1 #Setting learning rate | |
inputlayer_neurons = X.shape[1] #number of features in data set | |
hiddenlayer_neurons = 3 #number of hidden layer neurons | |
output_neurons = 1 #number of neurons in output layer | |
#weight and bias initialization | |
wh=torch.randn(inputlayer_neurons, hiddenlayer_neurons).type(torch.FloatTensor) | |
bh=torch.randn(1, hiddenlayer_neurons).type(torch.FloatTensor) | |
wout=torch.randn(hiddenlayer_neurons, output_neurons) | |
bout=torch.randn(1, output_neurons) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment