fashion mnist example

This commit is contained in:
Kashif Rasul
2023-12-23 11:22:23 +01:00
parent f4709cb807
commit 0ed2de6a61
5 changed files with 40 additions and 10 deletions

View File

@@ -14,10 +14,16 @@ Run the example with:
python main.py
```
By default the example runs on the CPU. To run on the GPU, use:
By default, the example runs on the CPU. To run on the GPU, use:
```
python main.py --gpu
```
To run the PyTorch or Jax examples install the respective framework.
To run the example with the Fashion-MNIST dataset, use:
```
python main.py --fashion_mnist
```

View File

@@ -54,9 +54,12 @@ if __name__ == "__main__":
batch_size = 256
num_epochs = 10
learning_rate = 1e-1
fashion_mnist = False
# Load the data
train_images, train_labels, test_images, test_labels = mnist.mnist()
train_images, train_labels, test_images, test_labels = (
mnist.fashion_mnist() if fashion_mnist else mnist.mnist()
)
# Load the model
key, subkey = jax.random.split(jax.random.PRNGKey(seed))

View File

@@ -45,7 +45,7 @@ def batch_iterate(batch_size, X, y):
yield X[ids], y[ids]
def main():
def main(args):
seed = 0
num_layers = 2
hidden_dim = 32
@@ -57,7 +57,9 @@ def main():
np.random.seed(seed)
# Load the data
train_images, train_labels, test_images, test_labels = map(mx.array, mnist.mnist())
train_images, train_labels, test_images, test_labels = map(
mx.array, mnist.fashion_mnist() if args.fashion_mnist else mnist.mnist()
)
# Load the model
model = MLP(num_layers, train_images.shape[-1], hidden_dim, num_classes)
@@ -83,7 +85,10 @@ def main():
if __name__ == "__main__":
parser = argparse.ArgumentParser("Train a simple MLP on MNIST with MLX.")
parser.add_argument("--gpu", action="store_true", help="Use the Metal back-end.")
parser.add_argument(
"--fashion_mnist", action="store_false", help="Use Fashion-MNIST."
)
args = parser.parse_args()
if not args.gpu:
mx.set_default_device(mx.cpu)
main()
main(args)

View File

@@ -8,7 +8,9 @@ from urllib import request
import numpy as np
def mnist(save_dir="/tmp"):
def mnist(
save_dir="/tmp", base_url="http://yann.lecun.com/exdb/mnist/", filename="mnist.pkl"
):
"""
Load the MNIST dataset in 4 tensors: train images, train labels,
test images, and test labels.
@@ -20,7 +22,6 @@ def mnist(save_dir="/tmp"):
"""
def download_and_save(save_file):
base_url = "http://yann.lecun.com/exdb/mnist/"
filename = [
["training_images", "train-images-idx3-ubyte.gz"],
["test_images", "t10k-images-idx3-ubyte.gz"],
@@ -45,13 +46,15 @@ def mnist(save_dir="/tmp"):
with open(save_file, "wb") as f:
pickle.dump(mnist, f)
save_file = os.path.join(save_dir, "mnist.pkl")
save_file = os.path.join(save_dir, filename)
if not os.path.exists(save_file):
download_and_save(save_file)
with open(save_file, "rb") as f:
mnist = pickle.load(f)
preproc = lambda x: x.astype(np.float32) / 255.0
def preproc(x):
return x.astype(np.float32) / 255.0
mnist["training_images"] = preproc(mnist["training_images"])
mnist["test_images"] = preproc(mnist["test_images"])
return (
@@ -62,6 +65,14 @@ def mnist(save_dir="/tmp"):
)
def fashion_mnist(save_dir="/tmp"):
return mnist(
save_dir,
base_url="http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/",
filename="fashion_mnist.pkl",
)
if __name__ == "__main__":
train_x, train_y, test_x, test_y = mnist()
assert train_x.shape == (60000, 28 * 28), "Wrong training set size"

View File

@@ -49,6 +49,9 @@ def batch_iterate(batch_size, X, y, device):
if __name__ == "__main__":
parser = argparse.ArgumentParser("Train a simple MLP on MNIST with PyTorch.")
parser.add_argument("--gpu", action="store_true", help="Use the Metal back-end.")
parser.add_argument(
"--fashion_mnist", action="store_false", help="Use Fashion-MNIST."
)
args = parser.parse_args()
if not args.gpu:
@@ -71,7 +74,9 @@ if __name__ == "__main__":
else:
return torch.from_numpy(x.astype(int)).to(device)
train_images, train_labels, test_images, test_labels = map(to_tensor, mnist.mnist())
train_images, train_labels, test_images, test_labels = map(
to_tensor, mnist.fashion_mnist() if args.fashion_mnist else mnist.mnist()
)
# Load the model
model = MLP(num_layers, train_images.shape[-1], hidden_dim, num_classes).to(device)