Created
July 2, 2020 15:23
-
-
Save somyamohanty/f64271e774f68962f1a66df822e494a3 to your computer and use it in GitHub Desktop.
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
import tensorflow as tf | |
physical_devices = tf.config.experimental.list_physical_devices('GPU') | |
for physical_device in physical_devices: | |
tf.config.experimental.set_memory_growth(physical_device, True) | |
gpus = tf.config.experimental.list_physical_devices('GPU') | |
if gpus: | |
# Restrict TensorFlow to only allocate 1GB * 2 of memory on the first GPU | |
try: | |
tf.config.experimental.set_virtual_device_configuration( | |
gpus[0], | |
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024 * 2)]) | |
logical_gpus = tf.config.experimental.list_logical_devices('GPU') | |
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") | |
except RuntimeError as e: | |
# Virtual devices must be set before GPUs have been initialized | |
print(e) | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
from tensorflow.keras.datasets import imdb | |
from tensorflow.keras.preprocessing import sequence | |
max_features = 2000 | |
max_len = 500 | |
(X_train, Y_train), (X_test, Y_test) = imdb.load_data(num_words=max_features) | |
X_train = sequence.pad_sequences(X_train, maxlen=max_len) | |
X_test = sequence.pad_sequences(X_test, maxlen=max_len) | |
model = tf.keras.models.Sequential() | |
model.add(layers.Embedding(max_features, | |
128, | |
input_length=max_len, | |
name='embed')) | |
model.add(layers.Conv1D(32, 7, activation='relu')) | |
model.add(layers.MaxPooling1D(5)) | |
model.add(layers.Conv1D(32, 7, activation='relu')) | |
model.add(layers.GlobalMaxPooling1D()) | |
model.add(layers.Dense(1)) | |
model.summary() | |
model.compile(optimizer='rmsprop', | |
loss='binary_crossentropy', | |
metrics=['acc']) | |
callbacks = [ | |
tf.keras.callbacks.TensorBoard( | |
log_dir="my-log-dir", | |
histogram_freq=1, | |
embeddings_freq=1, | |
) | |
] | |
history = model.fit(X_train, Y_train, | |
epochs=20, | |
batch_size=8, | |
validation_split=0.2, | |
callbacks=callbacks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment