TensorFlow Installation
pip install tensorflow
Install TensorFlow
pip install tensorflow-gpu
Install TensorFlow with GPU support
python -c 'import tensorflow as tf; print(tf.__version__)'
Check TensorFlow version
python -c 'import tensorflow as tf; print(tf.config.list_physical_devices("GPU"))'
Check available GPUs
TensorFlow Basics
tf.constant(value)
Create a constant tensor
tf.Variable(value)
Create a variable tensor
tf.placeholder(dtype, shape=None, name=None)
Create a placeholder for input
tf.keras.Model()
Create a model
tf.keras.layers.Dense(units)
Add a dense layer to your model
Model Training
model.compile(optimizer, loss, metrics)
Configure the model for training
model.fit(x, y, epochs, batch_size)
Train the model
model.evaluate(x_test, y_test)
Evaluate the model
model.predict(x)
Generate predictions
TensorFlow Profiling
tf.profiler.experimental.start('logdir')
Start profiling
tf.profiler.experimental.stop()
Stop profiling
tensorboard --logdir=path/to/logs
Launch TensorBoard to visualize profiling data
TensorFlow Debugging
tf.debugging.assert_equal(x, y)
Assert tensors are equal
tf.debugging.check_numerics(tensor, message)
Check tensor for NaN or Inf
tf.print(tensor)
Print tensor values during execution
tf.debugging.set_log_device_placement(True)
Log device placement
TensorBoard
writer = tf.summary.create_file_writer('logs/')
Create a file writer for logs
with writer.as_default(): tf.summary.scalar('loss', loss, step=step)
Log scalar values
tensorboard --logdir=logs/
Launch TensorBoard server
Model Saving/Loading
model.save('path/to/model')
Save entire model
model = tf.keras.models.load_model('path/to/model')
Load entire model
model.save_weights('path/to/weights')
Save model weights
model.load_weights('path/to/weights')
Load model weights
Distributed Training
strategy = tf.distribute.MirroredStrategy()
Create distribution strategy for multiple GPUs
with strategy.scope(): model = tf.keras.Model()
Define model within strategy scope
TensorFlow Serving
docker pull tensorflow/serving
Pull TensorFlow Serving Docker image
docker run -p 8501:8501 --mount type=bind,source=/path/to/model,target=/models/my_model -e MODEL_NAME=my_model tensorflow/serving
Run TensorFlow Serving container
curl -d '{"instances": [[1.0, 2.0, 3.0]]}' -X POST http://localhost:8501/v1/models/my_model:predict
Make prediction request to TF Serving