Skip to content

Instantly share code, notes, and snippets.

@tlkh
Last active August 4, 2018 17:43
Show Gist options
  • Save tlkh/7354b965fc9bc32250d91d36f0a87ed1 to your computer and use it in GitHub Desktop.
Save tlkh/7354b965fc9bc32250d91d36f0a87ed1 to your computer and use it in GitHub Desktop.
Keras/Jupyter Stuff
import tensorflow as tf
import tf.keras as keras
# Configure a model for categorical classification.
model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
loss=keras.losses.categorical_crossentropy,
metrics=[keras.metrics.categorical_accuracy])
estimator = keras.estimator.model_to_estimator(model)
# tf dataset
dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32).repeat()
val_dataset = tf.data.Dataset.from_tensor_slices((val_data, val_labels))
val_dataset = val_dataset.batch(32).repeat()
model.fit(dataset, epochs=10, steps_per_epoch=30,
validation_data=val_dataset,
validation_steps=3)
def input_fn():
x = np.random.random((1024, 10))
y = np.random.randint(2, size=(1024, 1))
x = tf.cast(x, tf.float32)
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.repeat(10)
dataset = dataset.batch(32)
return dataset
strategy = tf.contrib.distribute.MirroredStrategy()
config = tf.estimator.RunConfig(train_distribute=strategy)
keras_estimator = keras.estimator.model_to_estimator(
keras_model=model,
config=config,
model_dir='/tmp/model_dir')
keras_estimator.train(input_fn=input_fn, steps=10)
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
# GPU selection
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# loading from pickle
with open("datasets/sst_train_texts.pickle", 'rb') as handle:
sst_train = pickle.load(handle)
# loading from text file
lines = [line.rstrip('\n') for line in open('filename')]
# multi-gpu model
import tensorflow as tf
from keras.utils.training_utils import multi_gpu_model
with tf.device("/cpu:0"):
# initialize the model
model = XXX
# make the model parallel
parallel_model = multi_gpu_model(model, gpus=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment