0
$\begingroup$

Write a function that can shift an MNIST image in any direction (left, right, up, or down) by one pixel.⁠6 Then, for each image in the training set, create four shifted copies (one per direction) and add them to the training set. Finally, train your best model on this expanded training set and measure its accuracy on the test set. You should observe that your model performs even better now! This technique of artificially growing the training set is called data augmentation or training set expansion.

I am reading HOML book chapter two exercise 2 this is my attempt so far:

from scipy.ndimage.interpolation import shift
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_val_score

def shift_image(image, direction):
    image = image.reshape((28, 28))
    if direction == "right":
        return shift(image, [0, 1], cval=0).reshape(1, -1)
    elif direction == "left":
        return shift(image, [0, -1], cval=0).reshape(1, -1)
    elif direction == "up":
        return shift(image, [-1, 0], cval=0).reshape(1, -1)
    elif direction == "down":
        return shift(image, [1, 0], cval=0).reshape(1, -1)

X_train_augmented = []
y_train_augmented = []

for img, label in zip(X_train, y_train):
    X_train_augmented.append(img)
    y_train_augmented.append(label)
    for direction in ["right", "left", "up", "down"]:
        X_train_augmented.append(shift_image(img, direction))
        y_train_augmented.append(label)

X_train_augmented = np.vstack(X_train_augmented)
y_train_augmented = np.array(y_train_augmented)

sgd_clf = SGDClassifier()
sgd_clf.fit(X_train_augmented, y_train_augmented)
cross_val_score(sgd_clf, X_train_augmented, y_train_augmented, cv=3, scoring="accuracy")

however if we see the performance compared to a regular SGDclassifier in the data augmentatrion one ahs lower accuracy :

array([0.83068, 0.76707, 0.82064]) #sgd with augmented data performance 
array([0.8788 , 0.8797 , 0.89235])# regular SDG performance
$\endgroup$
1

1 Answer 1

2
$\begingroup$

You should measure the performance on the same dataset to be a good head-to-head comparison.

I suggest you create a non-augmented test set first, then train a simple SDG from the train set, and then augment the train set and train a new SDG, and then compare the performance of both these models on the held-out test set.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.