def main(): # prepare sample input for forwad() train_loader = torchvision.datasets.MNIST('../data_MNIST', train=True, download=True, transform=True, target_transform=True) epoch = 10 learning_rate = 0.5 batch_size = 100 numCls = 10 iteration = len(train_loader) / batch_size input = (torch.squeeze( train_loader.train_data.view(iteration, batch_size, 1, 784), 2)).type(torch.FloatTensor) # Test NnImg2Num net = NnImg2Num() net.train() print('forward output', net.forward(input[0])) print('call output', net(input[0])) # Test MyImg2Num netMy = MyImg2Num() netMy.train() print('forward output', netMy.forward((input[0]))) print('call output', netMy((input[0])))
train_mse = np.array(train_mse) train_accuracy = np.array(train_accuracy) test_mse = np.array(test_mse) test_accuracy = np.array(test_accuracy) plt.plot(epoch_scale, train_mse, label='train mse') plt.plot(epoch_scale, test_mse, label='test mse') plt.xlabel('epochs') plt.ylabel('mse') plt.title('MyNN MSE vs Epochs') plt.legend() plt.show() py_nn = NnImg2Num(train_batch, test_batch) train_error, test_error, epoch_scale = py_nn.train(epochs) accuracy = py_nn.test_accuracy() print(f'accuracy on test: {accuracy}') mse = py_nn.test_mse() print(f'mse on test: {mse}') train_mse, train_accuracy = train_error test_mse, test_accuracy = test_error train_mse = np.array(train_mse) train_accuracy = np.array(train_accuracy) test_mse = np.array(test_mse) test_accuracy = np.array(test_accuracy)
from my_img2num import MyImg2Num from nn_img2num import NnImg2Num myi2n = MyImg2Num() myi2n.train() nni2n = NnImg2Num() nni2n.train()
from nn_img2num import NnImg2Num from my_img2num import MyImg2Num nn_mnist = NnImg2Num() nn_mnist.train() # my_mnist = MyImg2Num() # my_mnist.train()
import torch from torchvision import datasets, transforms import time import matplotlib.pyplot as plt from nn_img2num import NnImg2Num # Create object for NnImg2Num class model = NnImg2Num() # Load test data TestLoader = torch.utils.data.DataLoader( datasets.MNIST( '../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), #transforms.Normalize((0.1307,), (0.3801,)) ])), batch_size=1, shuffle=True) # Initiate error rate, operation time and epochs eRate, opTime, epochs = [], [], [] # Train model using different epoch for i in range(1, 10): epoch = i * 10 startTime = time.time() model.epoch = epoch model.train() opTime.append(time.time() - startTime)
import torch import torchvision import torchvision.transforms as transforms from my_img2num import MyImg2num from nn_img2num import NnImg2Num from matplotlib import pyplot as plt import numpy as np import torch.nn as nn from neural_network import NeuralNetwork as NN #myimg2num = MyImg2num() #myimg2num.train() nnimg2num = NnImg2Num() nnimg2num.train() transform = transforms.ToTensor() test_set = torchvision.datasets.MNIST('/tmp', train=False, transform=transform, download=True) testloader = torch.utils.data.DataLoader(test_set, batch_size=10000, shuffle=False) dataiter = iter(testloader) images, data = dataiter.next() counter = 0 for i in range(10000): a = nnimg2num.forward(images[i, 0, :, :])
def main(): # model_my = MyImg2Num() model_my = NnImg2Num() model_my.train() print('done')
import torchvision import torch import torchvision.transforms as transforms from my_img2num import MyImg2Num from nn_img2num import NnImg2Num device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu") print(device) ## TRAIN ON MY NETWORK mynet = MyImg2Num() #mynet.to(device) mynet.train() # TRAIN ON TORCH NETWORK net = NnImg2Num() #net.to(device) net.train()