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])))
Exemple #2
0
        '../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):
    # Create object for NnImg2Num class
    model = MyImg2Num()
    epoch = i * 10
    model.epoch = epoch
    startTime = time.time()
    model.train()
    opTime.append(time.time() - startTime)
    correctness, total = 0, 0
    epochs.append(epoch)
    for batch_idx, (data, label) in enumerate(TestLoader):
        output = model.forward(data)
        if torch.equal(output, label):
            correctness += 1
        total += 1
    eRate.append((total - correctness) / total)
    print("Error Rate: ", (total - correctness) / total * 100, "%")
Exemple #3
0
import matplotlib.pyplot as plt
import numpy as np

from my_img2num import MyImg2Num
from nn_img2num import NnImg2Num

train_batch = 1
test_batch = 10000
epochs = 1

my_nn = MyImg2Num(train_batch, test_batch)
train_error, test_error, epoch_scale = my_nn.train(epochs)
accuracy = my_nn.test_accuracy()
print(f'accuracy on test: {accuracy}')
mse = my_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)

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')
Exemple #4
0
def main():
    model_my = MyImg2Num()
    # model_my = NnImg2Num()
    model_my.train()
    print('done')
Exemple #5
0
from my_img2num import MyImg2Num
from nn_img2num import NnImg2Num

myi2n = MyImg2Num()
myi2n.train()
nni2n = NnImg2Num()
nni2n.train()
Exemple #6
0
def main():
    start_time = time.time()
    myNN = MyImg2Num()
    #myNN = NnImg2Num()
    myNN.train()
    print('time elapsed is {} seconds'.format(time.time() - start_time))
from my_img2num import MyImg2Num
import matplotlib.pyplot as plt
from nn_img2num import NNImg2Num
plt.ioff()
print('running self nn')
my_img_2num = MyImg2Num()
my_time,my_train_loss, my_test_loss, my_accuracy = my_img_2num.train(True)
print(my_time)
print('running library nn')
nn_img = NNImg2Num()
nn_time, nn_train_loss, nn_test_loss, nn_accuracy = nn_img.train(True)
print(nn_time)
data = [my_time, nn_time]
plt.boxplot(data)
plt.xticks(range(1, 3), ['MyImg2Num', 'NNImg2Num'])
plt.ylabel('Running Time in Seconds')
plt.legend()
plt.savefig('efficiency.png')
plt.clf()

plt.plot(range(30), nn_accuracy, 'r*--', label='Accuracy of torch nn')
plt.plot(range(30), my_accuracy, 'b|--', label='Accuracy of my nn')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig('accuracy.png')
plt.clf()

plt.plot(range(30), my_train_loss, 'r*--', label='Training loss of my nn')
plt.plot(range(30), my_test_loss, 'ro-.', label='Testing loss of my nn')
plt.plot(range(30), nn_train_loss, 'b1--', label='Training loss of torch nn')
Exemple #8
0
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()