TensorFlow
Deep Learning
Keras API
TensorFlow Basics
TensorFlow is a popular open‑source framework for building and training deep learning models, with Keras as its high‑level API.
Tensors & Computation
At the core of TensorFlow are tensors: multi‑dimensional arrays similar to NumPy arrays but with automatic differentiation support.
Creating basic tensors
import tensorflow as tf
x = tf.constant([[1., 2.], [3., 4.]])
y = tf.ones_like(x)
z = x + y
print(z.numpy())
Building Models with Keras Sequential
The Sequential API is the easiest way to stack layers and create feed‑forward neural networks.
Simple classification model
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(16, activation="relu", input_shape=(num_features,)),
Dense(8, activation="relu"),
Dense(1, activation="sigmoid")
])
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
history = model.fit(X_train, y_train, epochs=20, batch_size=32,
validation_data=(X_val, y_val))
Practical Tips
- Normalize or standardize inputs for faster convergence.
- Use callbacks like
EarlyStoppingto prevent overfitting. - Start with small models and increase complexity only if needed.
Next Steps with TensorFlow
- Explore convolutional neural networks (CNNs) for image data and recurrent / transformer models for sequence data.
- Use
tf.datapipelines for efficient input pipelines on large datasets. - Try saving and loading models with
model.save()andtf.keras.models.load_model()for deployment.