Here’s a concise cheat sheet for getting started with TensorFlow

1. Import TensorFlow

pythonCopy

import tensorflow as tf

2. Tensors

  • Create constant tensor:

pythonCopy

tensor = tf.constant([[1, 2], [3, 4]])
  • Create variable tensor:

pythonCopy

var = tf.Variable([[1, 2], [3, 4]], dtype=tf.float32)

pythonCopy

import numpy as np
array = np.array([1, 2, 3])
tensor = tf.convert_to_tensor(array)

3. Tensor Operations

  • Element-wise addition:

pythonCopy

result = tf.add(tensor1, tensor2)
  • Element-wise multiplication:

pythonCopy

result = tf.multiply(tensor1, tensor2)
  • Matrix multiplication:

pythonCopy

result = tf.matmul(tensor1, tensor2)
  • Reshape tensor:

pythonCopy

result = tf.reshape(tensor, new_shape)

4. Eager Execution

  • Check if eager execution is enabled:

pythonCopy

print(tf.executing_eagerly())

5. Gradient Tape

  • Compute gradients:

pythonCopy

with tf.GradientTape() as tape:
    loss = compute_loss()
gradients = tape.gradient(loss, variables)

6. Keras

  • Create a simple sequential model:

pythonCopy

from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(input_shape,)),
    layers.Dense(64, activation='relu'),
    layers.Dense(num_classes, activation='softmax')
])
  • Compile the model:

pythonCopy

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=['accuracy'])
  • Train the model:

pythonCopy

history = model.fit(x_train, y_train, epochs=num_epochs, batch_size=batch_size, validation_split=0.2)
  • Evaluate the model:

pythonCopy

loss, accuracy = model.evaluate(x_test, y_test)
  • Save and load the model:

pythonCopy

model.save('my_model.h5')
loaded_model = tf.keras.models.load_model('my_model.h5')

7. Custom Layers and Models

  • Create a custom layer:

pythonCopy

class CustomDense(layers.Layer):
    def __init__(self, units):
        super(CustomDense, self).__init__()
        self.units = units

    def build(self, input_shape):
        self.w = self.add_weight(shape=(input_shape[-1], self.units),
                                 initializer='random_normal',
                                 trainable=True)
        self.b = self.add_weight(shape=(self.units,),
                                 initializer='zeros',
                                 trainable=True)

    def call(self, inputs):
        return tf.matmul(inputs, self.w) + self.b
  • Create a custom model:

pythonCopy

class CustomModel(tf.keras.Model):
    def __init__(self, num_classes):
        super(CustomModel, self).__init__()
        self.dense1 = CustomDense(64)
        self.dense2 = CustomDense(64)
        self.dense3 = CustomDense(num_classes)

    def call(self, inputs):
        x = tf.nn.relu(self.dense1(inputs))
        x = tf.nn.relu(self.dense2(x))
        return self.dense3(x)

This cheat sheet covers the basic elements of TensorFlow. For more advanced topics and detailed explanations, refer to the official TensorFlow documentation.