TensorFlow

A concise yet comprehensive cheatsheet

TensorFlow Basics

Command / ConceptDescription
import tensorflow as tfImport TensorFlow library
tf.versionCheck TensorFlow version
tf.constant([1, 2, 3])Create constant tensor
tf.Variable(10)Create a variable
tf.convert_to_tensor([1,2,3])Convert list to tensor
tf.rank(tensor)Get rank (dimensions)
tf.shape(tensor)Get shape of tensor
tf.cast(tensor, dtype=tf.float32)Change data type

Tensor Operations

OperationFunctionDescription
Additiontf.add(x, y)Element-wise addition
Subtractiontf.subtract(x, y)Subtract tensors
Multiplicationtf.multiply(x, y)Element-wise multiply
Divisiontf.divide(x, y)Element-wise divide
Matrix Multiplytf.matmul(a, b)Matrix multiplication
Transposetf.transpose(a)Transpose tensor
Reshapetf.reshape(a, [r, c])Reshape tensor
Sumtf.reduce_sum(x)Sum of elements
Meantf.reduce_mean(x)Mean of elements
Max Indextf.argmax(x, axis=1)Index of max value
Equalitytf.equal(a, b)Element-wise equality

Random Tensors

CommandDescription
tf.random.normal([2,3], mean=0, stddev=1)Tensor from normal distribution
tf.random.uniform([2,3], minval=0, maxval=10)Tensor from uniform distribution
tf.random.shuffle(tensor)Shuffle tensor elements

Neural Network Layers

LayerCommandDescription
Densetf.keras.layers.Dense(64, activation='relu')Fully connected layer
Conv2Dtf.keras.layers.Conv2D(32, (3,3), activation='relu')Convolutional layer
MaxPoolingtf.keras.layers.MaxPooling2D((2,2))Pooling layer
Dropouttf.keras.layers.Dropout(0.5)Dropout layer
Flattentf.keras.layers.Flatten()Flatten input
Inputtf.keras.Input(shape=(784,))Define input layer

Model Creation & Training

StepCommandDescription
Create Sequential Modelmodel = tf.keras.models.Sequential([...])Create model structure
Compilemodel.compile(optimizer='adam', loss='mse', metrics=['accuracy'])Configure model
Trainmodel.fit(X_train, y_train, epochs=10, batch_size=32)Train model
Evaluatemodel.evaluate(X_test, y_test)Evaluate performance
Predictmodel.predict(X_new)Make predictions

Optimizers & Loss Functions

TypeCommandDescription
Optimizerstf.keras.optimizers.Adam(learning_rate=0.001)Adam optimizer
tf.keras.optimizers.SGD(learning_rate=0.01)Gradient Descent
tf.keras.optimizers.RMSprop()RMSprop optimizer
Loss Functionstf.keras.losses.MeanSquaredError()Regression loss
tf.keras.losses.BinaryCrossentropy()Binary classification loss
tf.keras.losses.CategoricalCrossentropy()Multi-class classification

Activation Functions

ActivationCommandDescription
ReLUtf.nn.relu(x)Rectified Linear Unit
Sigmoidtf.nn.sigmoid(x)Output between 0–1
Softmaxtf.nn.softmax(x)Probability distribution
Tanhtf.nn.tanh(x)Hyperbolic tangent

Gradient & Backpropagation

CommandDescription
with tf.GradientTape() as tape:Record operations for auto differentiation
grad = tape.gradient(loss, var)Compute gradient of loss w.r.t variable
optimizer.apply_gradients(zip(grads, vars))Apply computed gradients

Saving & Loading Models

CommandDescription
model.save('model.h5')Save entire model
tf.keras.models.load_model('model.h5')Load saved model
model.save_weights('weights.h5')Save weights only
model.load_weights('weights.h5')Load saved weights

TensorBoard & Visualization

CommandDescription
tensorboard --logdir=logs/Launch TensorBoard
tf.summary.scalar('loss', loss)Log scalar values
tf.summary.histogram('weights', w)Log histograms of weights

Devices & Performance

CommandDescription
tf.config.list_physical_devices('GPU')List available GPUs
tf.device('/GPU:0')Assign GPU device
tf.config.experimental.set_memory_growth(gpu, True)Prevent full GPU memory allocation

Datasets and Pipelines

CommandDescription
from tensorflow.keras.datasets import mnistImport dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()Load dataset
x_train = x_train / 255.0Normalize data
dataset = tf.data.Dataset.from_tensor_slices((x, y))Create dataset pipeline
dataset = dataset.shuffle(1000).batch(32)Shuffle and batch data

Callbacks & Metrics

CommandDescription
tf.keras.callbacks.EarlyStopping(monitor='val_loss')Stop when validation loss stops improving
tf.keras.callbacks.ModelCheckpoint(filepath='best_model.h5', save_best_only=True)Save best model
tf.keras.metrics.Accuracy()Accuracy metric
tf.keras.metrics.Precision()Precision metric
tf.keras.metrics.Recall()Recall metric

Miscellaneous Useful Commands

CommandDescription
tf.summary.image('input_image', images)Log images
tf.print(tensor)Print tensor values
tf.debugging.check_numerics(tensor, 'error')Check for NaN/Inf values
tf.experimental.numpyUse NumPy-like ops in TF