pdfcoffee
Chapter 2In our toy example we use NumPy for generating training data x and labelsy, and we transform it into tf.data.Dataset with tf.data.Dataset.from_tensor_slices(). Then we apply a shuffle to avoid bias in training acrossGPUs and then generate SIZE_BATCHES batches:import tensorflow as tfimport numpy as npfrom tensorflow import kerasN_TRAIN_EXAMPLES = 1024*1024N_FEATURES = 10SIZE_BATCHES = 256# 10 random floats in the half-open interval [0.0, 1.0).x = np.random.random((N_TRAIN_EXAMPLES, N_FEATURES))y = np.random.randint(2, size=(N_TRAIN_EXAMPLES, 1))x = tf.dtypes.cast(x, tf.float32)print (x)dataset = tf.data.Dataset.from_tensor_slices((x, y))dataset = dataset.shuffle(buffer_size=N_TRAIN_EXAMPLES).batch(SIZE_BATCHES)2. In order to distribute some computations to GPUs, we instantiate adistribution = tf.distribute.MirroredStrategy() object, whichsupports synchronous distributed training on multiple GPUs on onemachine. Then, we move the creation and compilation of the Keras modelinside the strategy.scope(). Note that each variable in the model ismirrored across all the replicas. Let's see it in our toy example:# this is the distribution strategydistribution = tf.distribute.MirroredStrategy()# this piece of code is distributed to multiple GPUswith distribution.scope():model = tf.keras.Sequential()model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(N_FEATURES,)))model.add(tf.keras.layers.Dense(1, activation='sigmoid'))optimizer = tf.keras.optimizers.SGD(0.2)model.compile(loss='binary_crossentropy', optimizer=optimizer)model.summary()# Optimize in the usual way but in reality you are using GPUs.model.fit(dataset, epochs=5, steps_per_epoch=10)[ 77 ]
TensorFlow 1.x and 2.xNote that each batch of the given input is divided equally among the multipleGPUs. For instance, if using MirroredStrategy() with two GPUs, each batch ofsize 256 will be divided among the two GPUs, with each of them receiving 128 inputexamples for each step. In addition, note that each GPU will optimize on thereceived batches and the TensorFlow backend will combine all these independentoptimizations on our behalf. If you want to know more, you can have a look to thenotebook online (https://colab.research.google.com/drive/1mf-PK0a20CkObnT0hCl9VPEje1szhHat#scrollTo=wYar3A0vBVtZ) where I explain how to use GPUsin Colab with a Keras model built for MNIST classification. The notebook is availablein the GitHub repository.In short, using multiple GPUs is very easy and requires minimal changes to thetf.keras code used for a single server.MultiWorkerMirroredStrategyThis strategy implements synchronous distributed training across multiple workers,each one with potentially multiple GPUs. As of September 2019 the strategy worksonly with Estimators and it has experimental support for tf.keras. This strategyshould be used if you are aiming at scaling beyond a single machine with highperformance. Data must be loaded with tf.Dataset and shared across workersso that each worker can read a unique subset.TPUStrategyThis strategy implements synchronous distributed training on TPUs. TPUs areGoogle's specialized ASICs chips designed to significantly accelerate machinelearning workloads in a way often more efficient than GPUs. We will talk more aboutTPUs during Chapter 16, Tensor Processing Unit. According to this public information(https://github.com/tensorflow/tensorflow/issues/24412):"the gist is that we intend to announce support for TPUStrategy alongsideTensorflow 2.1. Tensorflow 2.0 will work under limited use-cases but has manyimprovements (bug fixes, performance improvements) that we're including inTensorflow 2.1, so we don't consider it ready yet."ParameterServerStrategyThis strategy implements either multi-GPU synchronous local training orasynchronous multi-machine training. For local training on one machine, thevariables of the models are placed on the CPU and operations are replicated acrossall local GPUs.[ 78 ]
- Page 61 and 62: Neural Network Foundations with Ten
- Page 63 and 64: Neural Network Foundations with Ten
- Page 65 and 66: Neural Network Foundations with Ten
- Page 67 and 68: Neural Network Foundations with Ten
- Page 69 and 70: Neural Network Foundations with Ten
- Page 71 and 72: Neural Network Foundations with Ten
- Page 73 and 74: Neural Network Foundations with Ten
- Page 75 and 76: Neural Network Foundations with Ten
- Page 77 and 78: Neural Network Foundations with Ten
- Page 79 and 80: Neural Network Foundations with Ten
- Page 81 and 82: Neural Network Foundations with Ten
- Page 83 and 84: Neural Network Foundations with Ten
- Page 86 and 87: TensorFlow 1.x and 2.xThe intent of
- Page 88 and 89: An example to start withWe'll consi
- Page 90 and 91: Chapter 23. Placeholders: Placehold
- Page 92 and 93: • To create random values from a
- Page 94 and 95: To know the value, we need to creat
- Page 96 and 97: Chapter 2Both PyTorch and TensorFlo
- Page 98 and 99: Chapter 2state = [tf.zeros([100, 10
- Page 100 and 101: Chapter 2For now, there's no need t
- Page 102 and 103: Chapter 2Let's see an example of a
- Page 104 and 105: Chapter 2If you want to save a mode
- Page 106 and 107: Chapter 2supervised=True)train_data
- Page 108 and 109: Chapter 2There, tf.feature_column.n
- Page 110 and 111: Chapter 2print (dz_dx)print (dy_dx)
- Page 114 and 115: Chapter 2For multi-machine training
- Page 116 and 117: Chapter 25. Use tf.layers modules t
- Page 118 and 119: Chapter 2Keras or tf.keras?Another
- Page 120: • tf.data can be used to load mod
- Page 123 and 124: RegressionLet us imagine a simpler
- Page 125 and 126: RegressionTake a look at the last t
- Page 127 and 128: Regression3. Now, we calculate the
- Page 129 and 130: RegressionIn the next section we wi
- Page 131 and 132: Regression2. Now, we define the fea
- Page 133 and 134: Regression2. Download the dataset:(
- Page 135 and 136: RegressionThe following is the Tens
- Page 137 and 138: RegressionIn regression the aim is
- Page 139 and 140: RegressionThe Estimator outputs the
- Page 141 and 142: RegressionThe following is the grap
- Page 143 and 144: RegressionReferencesHere are some g
- Page 145 and 146: Convolutional Neural NetworksIn thi
- Page 147 and 148: Convolutional Neural NetworksIn thi
- Page 149 and 150: Convolutional Neural NetworksIn oth
- Page 151 and 152: Convolutional Neural NetworksThen w
- Page 153 and 154: Convolutional Neural NetworksHoweve
- Page 155 and 156: Convolutional Neural NetworksPlotti
- Page 157 and 158: Convolutional Neural NetworksIn gen
- Page 159 and 160: Convolutional Neural NetworksOur ne
- Page 161 and 162: Convolutional Neural NetworksThese
Chapter 2
In our toy example we use NumPy for generating training data x and labels
y, and we transform it into tf.data.Dataset with tf.data.Dataset.from_
tensor_slices(). Then we apply a shuffle to avoid bias in training across
GPUs and then generate SIZE_BATCHES batches:
import tensorflow as tf
import numpy as np
from tensorflow import keras
N_TRAIN_EXAMPLES = 1024*1024
N_FEATURES = 10
SIZE_BATCHES = 256
# 10 random floats in the half-open interval [0.0, 1.0).
x = np.random.random((N_TRAIN_EXAMPLES, N_FEATURES))
y = np.random.randint(2, size=(N_TRAIN_EXAMPLES, 1))
x = tf.dtypes.cast(x, tf.float32)
print (x)
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.shuffle(buffer_size=N_TRAIN_EXAMPLES).
batch(SIZE_BATCHES)
2. In order to distribute some computations to GPUs, we instantiate a
distribution = tf.distribute.MirroredStrategy() object, which
supports synchronous distributed training on multiple GPUs on one
machine. Then, we move the creation and compilation of the Keras model
inside the strategy.scope(). Note that each variable in the model is
mirrored across all the replicas. Let's see it in our toy example:
# this is the distribution strategy
distribution = tf.distribute.MirroredStrategy()
# this piece of code is distributed to multiple GPUs
with distribution.scope():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(16, activation='relu', input_
shape=(N_FEATURES,)))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
optimizer = tf.keras.optimizers.SGD(0.2)
model.compile(loss='binary_crossentropy', optimizer=optimizer)
model.summary()
# Optimize in the usual way but in reality you are using GPUs.
model.fit(dataset, epochs=5, steps_per_epoch=10)
[ 77 ]