Skip to content

Instantly share code, notes, and snippets.

@jitvimol
Forked from new5558/keras_example.py
Last active August 13, 2022 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitvimol/41ea63cf5fa59810893a614878dfa018 to your computer and use it in GitHub Desktop.
Save jitvimol/41ea63cf5fa59810893a614878dfa018 to your computer and use it in GitHub Desktop.
[Singularity Tutorial] #python
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
# Model / data parameters
num_classes = 10
input_shape = (28, 28, 1)
# the data, split between train and test sets
# (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = np.random.randint(0, 255, (60000, 28, 28, 1))
y_train = np.random.randint(0, 10, (60000, ))
x_test = np.random.randint(0, 255, (300, 28, 28, 1))
y_test = np.random.randint(0, 10, (300, ))
# print(y_train.shape, y_train, x_train)
# Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = keras.Sequential(
[
keras.Input(shape=input_shape),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)
model.summary()
batch_size = 128
epochs = 15
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
model.save("convnet.h5")
#!/bin/bash
#SBATCH -p dgx #HPC-partition
#SBATCH -N 1 #number-of-node
#SBATCH -t 00:10:00 #time-to-use
#SBATCH -J keras-singularity-test #job-name
#SBATCH -A INSERT_PROJECTNAME #project-account
CUDAxxxx=11
module purge
module load Singularity
singularity exec --nv newsif.sif python keras_example.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment