1+ from neuralpy .models import Sequential
2+ from neuralpy .layers .linear import Dense
3+ from neuralpy .layers .convolutional import Conv2D
4+ from neuralpy .layers .activation_functions import ReLU ,Softmax
5+ from neuralpy .layers .pooling import MaxPool2D
6+ from neuralpy .layers .other import Flatten
7+ from neuralpy .loss_functions import CrossEntropyLoss
8+ from neuralpy .optimizer import SGD ,Adam
9+
10+ import torch
11+ import torchvision .datasets as datasets
12+ import torchvision .transforms as transforms
13+
14+ # Create a Sequential model Instance
15+ model = Sequential ()
16+
17+ model .add (Conv2D (input_shape = (1 ,224 ,224 ), filters = 96 , kernel_size = 11 , stride = 4 ))
18+ model .add (ReLU ())
19+ model .add (MaxPool2D (kernel_size = 3 , stride = 2 ))
20+ model .add (ReLU ())
21+ model .add (Conv2D (filters = 256 , kernel_size = 5 , stride = 1 , padding = 2 ))
22+ model .add (ReLU ())
23+ model .add (MaxPool2D (kernel_size = 3 , stride = 2 ))
24+ model .add (ReLU ())
25+ model .add (Conv2D (filters = 384 , kernel_size = 3 , stride = 1 , padding = 1 ))
26+ model .add (ReLU ())
27+ model .add (Conv2D (filters = 384 , kernel_size = 3 , stride = 1 , padding = 1 ))
28+ model .add (ReLU ())
29+ model .add (Conv2D (filters = 256 , kernel_size = 3 , stride = 1 , padding = 1 ))
30+ model .add (ReLU ())
31+ model .add (MaxPool2D (kernel_size = 3 , stride = 2 ))
32+ model .add (ReLU ())
33+ model .add (Flatten ())
34+ model .add (Dense (n_nodes = 4096 ))
35+ model .add (ReLU ())
36+ model .add (Dense (n_nodes = 4096 ))
37+ model .add (ReLU ())
38+ model .add (Dense (n_nodes = 10 ))
39+ model .add (Softmax ())
40+
41+ model .build ()
42+ model .compile (optimizer = Adam (), loss_function = CrossEntropyLoss (), metrics = ["accuracy" ])
43+ print (model .summary ())
44+
45+ # Get the training Data
46+ train_set = datasets .MNIST (
47+ root = './data'
48+ ,train = True
49+ ,download = True
50+ ,transform = transforms .Compose ([
51+ transforms .CenterCrop (224 ),
52+ transforms .ToTensor ()
53+ ])
54+ )
55+
56+ # Load the dataset from pytorch's Dataloader function
57+ train_loader = torch .utils .data .DataLoader (train_set , batch_size = 1000 )
58+ #Get the data
59+ mnist_data = next (iter (train_loader ))
60+ #Split into train and test set
61+ train_imgs = mnist_data [0 ][:500 ]
62+ train_labels = mnist_data [1 ][:500 ]
63+ test_imgs = mnist_data [0 ][500 :]
64+ test_labels = mnist_data [1 ][500 :]
65+ # Train Model
66+ model .fit (train_data = (train_imgs ,train_labels ), epochs = 5 , validation_data = (test_imgs ,test_labels ),batch_size = 1 )
0 commit comments