Created
November 19, 2018 18:09
-
-
Save qxcv/2f4a81449a9bc5b02e4e8d0f1d7d82d9 to your computer and use it in GitHub Desktop.
Script for comparing performance of CPU & GPU on a simple MLP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# (this script also requires tensorflow-gpu to be installed & working) | |
# increase hidden size/batch size to close gap between CPU and GPU | |
DISABLE_GPU = True | |
HIDDEN_UNITS = 64 | |
BATCH_SIZE = 64 | |
if DISABLE_GPU: | |
import os | |
os.environ['CUDA_VISIBLE_DEVICES'] = '' | |
from tensorflow.contrib import keras | |
import numpy as np | |
import timeit | |
model = keras.models.Sequential([ | |
keras.layers.Dense(HIDDEN_UNITS, input_dim=128), | |
keras.layers.Dense(HIDDEN_UNITS), | |
keras.layers.Dense(1) | |
]) | |
model.compile(loss='mse', optimizer='adam') | |
X = np.random.random((BATCH_SIZE, 128)) | |
y = np.random.random((BATCH_SIZE, 1)) | |
# warmup | |
model.train_on_batch(X, y) | |
# time it | |
elapsed = timeit.timeit('model.train_on_batch(X, y)', globals=globals(), number=1000) | |
print('Took %.3gs' % elapsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment