Pretrained AlexNet


I’m new at this of Neural Networks. I want to use a pretrained AlexNet and train it with MNIST dataset, however in all the code examples that I’ve seen for that, they only use one new image each time, and I would like to put the entire dataset, instead of a single image.
That’s my code (not working) at this moment.

import torch, torchvision
from tensorflow import keras
from torch import nn
from torchvision.transforms import ToTensor
from import DataLoader, Dataset
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np

!pip install alexnet_pytorch

objects = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = objects.load_data()
print(training_images.shape, test_images.shape)
print(training_labels.shape, test_labels.shape)

num_classes = 10
f, ax = plt.subplots(1, num_classes, figsize=(12,12))
for i in range(0, num_classes):
sample = training_images[training_labels==i][0]
ax[i].imshow(sample, cmap=‘gray’)
ax[i].set_title(‘Label: {}’.format(i), fontsize=16)

training_labels = keras.utils.to_categorical(training_labels, num_classes)
test_labels = keras.utils.to_categorical(test_labels, num_classes)

for i in range(0, 9):

training_images_normalized = training_images/255.0
test_images_normalized = test_images/255.0

training_images_normalized = training_images_normalized.reshape(training_images_normalized.shape[0], -1)
test_images_normalized = training_images_normalized.reshape(test_images_normalized.shape[0], -1)

Create the model—> where I’m not getting!

from matplotlib import transforms
from alexnet_pytorch import AlexNet
from torchvision import models
import torchvision.models as models
from torchvision import transforms

alexnet = models.alexnet(pretrained=True, progress=True)

model = AlexNet.from_pretrained(‘alexnet’)
for i in range(len(training_images_normalized)):
img = training_images_normalized[i]
x = np.expand_dims(img, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print(‘Predicted:’, decode_predictions(preds, top=3)[0])

Thanks in advance!

What kind of error are you seeing?
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

Hey Daniela,
From what I can understand, you want to fine-tune/train Alex Net on the MNIST dataset, but the example you have shared performs inference, that is why they take one image at a time. This simple tutorial can help in understanding how to train a neural network on the MNIST dataset. You can also replace the neural network Net there with Alex Net and train the Alex Net model.