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 = torch.from_numpy(X_train).float() | |
train_y = torch.from_numpy(targets_train).long() | |
test_x = torch.from_numpy(X_test).float() | |
test_y = torch.from_numpy(targets_test).long() | |
batch_size = 100 #We pick beforehand a batch_size that we will use for the training | |
# Pytorch train and test sets | |
train = torch.utils.data.TensorDataset(train_x,train_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
# CNN model training | |
count = 0 | |
loss_list = [] | |
iteration_list = [] | |
accuracy_list = [] | |
for epoch in range(num_epochs): | |
for i, (images, labels) in enumerate(train_loader): | |
train = Variable(images.view(100,3,16,16,16)) | |
labels = Variable(labels) |
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
num_classes = 10 | |
# Create CNN Model | |
class CNNModel(nn.Module): | |
def __init__(self): | |
super(CNNModel, self).__init__() | |
self.conv_layer1 = self._conv_layer_set(3, 32) | |
self.conv_layer2 = self._conv_layer_set(32, 64) | |
self.fc1 = nn.Linear(2**3*64, 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
# importing the libraries | |
import pandas as pd | |
import numpy as np | |
from tqdm import tqdm | |
import os | |
# for reading and displaying images | |
from skimage.io import imread | |
import matplotlib.pyplot as plt |
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
# visualization loss | |
plt.plot(iteration_list,loss_list) | |
plt.xlabel("Number of iteration") | |
plt.ylabel("Loss") | |
plt.title("CNN: Loss vs Number of iteration") | |
plt.show() | |
# visualization accuracy | |
plt.plot(iteration_list,accuracy_list,color = "red") | |
plt.xlabel("Number of iteration") |
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
# CNN model training | |
count = 0 | |
loss_list = [] | |
iteration_list = [] | |
accuracy_list = [] | |
for epoch in range(num_epochs): | |
for i, (images, labels) in enumerate(train_loader): | |
train = Variable(images.view(100,1,28,28)) | |
labels = Variable(labels) |
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
# batch_size, epoch and iteration | |
batch_size = 100 | |
# Pytorch train and test sets | |
train = torch.utils.data.TensorDataset(train_x,y_train) | |
test = torch.utils.data.TensorDataset(X_cv,y_cv) | |
# data loader | |
train_loader = torch.utils.data.DataLoader(train, batch_size = batch_size, shuffle = 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
# Create CNN Model | |
class CNNModel(nn.Module): | |
def __init__(self): | |
super(CNNModel, self).__init__() | |
# Convolution 1 | |
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, stride=1, padding=0) | |
self.relu1 = nn.ReLU() | |
# Max pool 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
#Formatting on training set | |
train_x = X_train.reshape(33600, 1, 28, 28) | |
train_x = torch.from_numpy(train_x).float() | |
# converting the target into torch format | |
y_train = torch.from_numpy(np.array(y_train)) | |
# shape of training data | |
train_x.shape, y_train.shape | |
#Formatting on testing set | |
X_cv = X_cv.reshape(8400, 1, 28, 28) |
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
from sklearn.model_selection import train_test_split | |
X_train, X_cv, y_train, y_cv = train_test_split(df_features, df_label, | |
test_size = 0.2, | |
random_state = 1212) | |
X_train = np.array(X_train).reshape(33600, 784) #(33600, 784) | |
X_cv = np.array(X_cv).reshape(8400, 784) #(8400, 784) |