Skip to content

Instantly share code, notes, and snippets.

View miki998's full-sized avatar
🐢
don't be fooled by this rabbit, i'm actually a pretty slow turtle

Michael 米高 miki998

🐢
don't be fooled by this rabbit, i'm actually a pretty slow turtle
View GitHub Profile
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)
# 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)
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)
# 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
# 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")
# 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)
# 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)
# 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
#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)
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)