Language: Python
ML/AI
TensorFlow was developed by the Google Brain team and released in 2015. It was designed to provide a comprehensive ecosystem for developing, training, and deploying machine learning models. TensorFlow supports deep learning, neural networks, and large-scale numerical computations and has become one of the most widely used ML frameworks in both industry and academia.
TensorFlow is an end-to-end open-source platform for machine learning. It allows you to build and deploy machine learning models easily, from training to inference, across multiple platforms and devices.
pip install tensorflowconda install -c conda-forge tensorflowTensorFlow provides APIs for building neural networks, performing automatic differentiation, training models, and serving models for production. It supports both eager execution (imperative programming) and graph execution (declarative programming).
import tensorflow as tf
x = tf.constant([[1., 2.],[3., 4.]])
y = tf.constant([[5., 6.],[7., 8.]])
print(tf.matmul(x, y))Defines two constant tensors and performs matrix multiplication.
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(5,)),
keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy')Creates a simple feedforward neural network with one hidden layer using the Keras API integrated in TensorFlow.
import numpy as np
X_train = np.random.rand(100,5)
y_train = np.random.randint(0,2, size=(100,1))
model.fit(X_train, y_train, epochs=10, batch_size=8)Trains the previously defined model on synthetic data for 10 epochs with a batch size of 8.
model.save('my_model')
new_model = keras.models.load_model('my_model')Demonstrates saving a trained model and loading it later for inference or further training.
import tensorflow_datasets as tfds
dataset = tfds.load('mnist', split='train')
for example in dataset.take(1):
image, label = example['image'], example['label']
print(image.shape, label)Shows how to load standard datasets from TensorFlow Datasets for training or testing.
x = tf.random.normal((10,3))
y_true = tf.random.normal((10,1))
weights = tf.Variable(tf.random.normal((3,1)))
bias = tf.Variable(tf.zeros(1))
optimizer = tf.optimizers.SGD(0.01)
for i in range(100):
with tf.GradientTape() as tape:
y_pred = tf.matmul(x, weights) + bias
loss = tf.reduce_mean(tf.square(y_true - y_pred))
grads = tape.gradient(loss, [weights, bias])
optimizer.apply_gradients(zip(grads, [weights, bias]))Implements a custom training loop to optimize weights using TensorFlow’s GradientTape and optimizer API.
Use tf.data API for efficient data loading and preprocessing.
Use eager execution for debugging and graph execution for production performance.
Leverage TensorBoard for visualizing training metrics.
Use mixed precision and hardware acceleration (GPU/TPU) for faster training.
Organize models and code using the Keras API for simplicity and modularity.