Python library centre
Comprehensive Guide to Training Neural Networks with TensorFlow in Python
TensorFlow is an open-source library for machine learning and deep learning. It is created by Google and supports building and training neural networks.…
- CoursePython library centre
- Video19 min
- FormatJupyter notebook · 21 code cells
What you'll learn
- Core Concepts of TensorFlow
- Beginner Example: Arithmetic with Tensors
- Beginner Example: Variables
- Beginner Example: Basic Operations
- Intermediate Example: Creating a Simple Neural Network Model
- Intermediate Example: Loading Data with TensorFlow
- Intermediate Example: Saving and Loading Models
- Advanced Example: Custom Layers
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbIntroduction to TensorFlow#
- TensorFlow is an open-source library for machine learning and deep learning.
- It is created by Google and supports building and training neural networks.
- TensorFlow is used in real-world projects like image recognition, natural language processing, and recommendation systems.
- You can use TensorFlow to create models that learn from data and make predictions.
import warnings; warnings.filterwarnings("ignore", category=UserWarning)
import tensorflow as tf # Import TensorFlow
Core Concepts of TensorFlow#
- TensorFlow works with tensors, which are multi-dimensional arrays.
- Models in TensorFlow are built using layers and operations.
- Training involves feeding data and adjusting model weights.
- TensorFlow can run on CPUs and GPUs.
# Let us create our first constant tensor
a = tf.constant(5)
print(a)
# Tensor with multiple values
b = tf.constant([1, 2, 3, 4])
print(b)
# Checking the shape and type of a tensor
print(b.shape)
print(b.dtype)
Beginner Example: Arithmetic with Tensors#
- You can do math like addition and multiplication with TensorFlow tensors.
x = tf.constant([10, 20, 30])
y = tf.constant([1, 2, 3])
z = x + y
print(z)
m = tf.constant([2, 3, 4])
n = tf.constant([5, 6, 7])
mul = m * n
print(mul)
# Convert a tensor to a NumPy array
arr = b.numpy()
print(arr)
Beginner Example: Variables#
- tf.Variable is used when you want to change the value of a tensor.
v = tf.Variable([10, 20, 30])
print(v)
v.assign([40, 50, 60])
print(v)
Beginner Example: Basic Operations#
- You can use TensorFlow functions for math operations, such as tf.add() or tf.multiply().
result = tf.add(5, 3)
print('5 + 3 =', result.numpy())
result2 = tf.multiply(6, 7)
print('6 * 7 =', result2.numpy())
Intermediate Example: Creating a Simple Neural Network Model#
- Keras (inside TensorFlow) helps make models easily.
- We will build a model with one input and one output.
from tensorflow import keras
from tensorflow.keras import layers
# Build a model with one dense layer
model = keras.Sequential([
layers.Dense(units=1, input_shape=[1])
])
print(model.summary())
# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')
# Prepare example training data
xs = tf.constant([1, 2, 3, 4, 5, 6], dtype=tf.float32)
ys = tf.constant([2, 4, 6, 8, 10, 12], dtype=tf.float32)
# Train the model
model.fit(xs, ys, epochs=400, verbose=0)
# Use the model to make a prediction
import numpy as np
print("If X is 7, Y is", model.predict(np.array([[7.0]])))
Intermediate Example: Loading Data with TensorFlow#
- Use tf.data.Dataset to manage data batches.
- We will make a dataset from tensors.
dataset = tf.data.Dataset.from_tensor_slices(([1, 2, 3], [4, 5, 6]))
for features, labels in dataset:
print('Feature:', features.numpy(), 'Label:', labels.numpy())
Intermediate Example: Saving and Loading Models#
- You can save your trained model to a file.
- You can also load it later to reuse without retraining.
model.save('my_model.keras')
new_model = keras.models.load_model('my_model.keras')
print('Model loaded successfully!')
Advanced Example: Custom Layers#
- You can create your own custom layers by subclassing tf.keras.layers.Layer.
class MyLayer(layers.Layer):
def __init__(self):
super(MyLayer, self).__init__()
def call(self, inputs):
return inputs * 3
custom_model = keras.Sequential([MyLayer()])
output = custom_model(tf.constant([[2.0]]))
print('Output:', output.numpy())
Advanced Example: Training With Callbacks#
- Callbacks let you stop training early or save models automatically.
callback = keras.callbacks.EarlyStopping(monitor='loss', patience=2)
model.fit(xs, ys, epochs=50, callbacks=[callback], verbose=0)
print('Training with EarlyStopping callback finished.')
Error Handling and Debugging in TensorFlow#
- Errors may happen if shapes do not match or types are incorrect.
- Use try and except to catch errors.
try:
invalid = tf.constant([1, 2]) + tf.constant([[3], [4]])
except Exception as e:
print('Error:', e)
Best Practices and Common Patterns#
- Always use tf.constant for data you do not want to change.
- Use tf.Variable for values you will update.
- Use clear names for layers and variables.
- Save your models after training them.
- Use callbacks for more control during training.
Mini-project: Build and Use a Model#
- We will build a model to learn y = 3 * x + 1
- We will train it and make predictions.
proj_model = keras.Sequential([layers.Dense(units=1, input_shape=[1])])
proj_model.compile(optimizer='sgd', loss='mean_squared_error')
proj_xs = tf.constant([-1, 0, 1, 2, 3, 4], dtype=tf.float32)
proj_ys = tf.constant([-2, 1, 4, 7, 10, 13], dtype=tf.float32)
proj_model.fit(proj_xs, proj_ys, epochs=500, verbose=0)
prediction = proj_model.predict(np.array([[10.0]]))
print('For x=10, predicted y is', prediction)
user_x = float(input('Type a number to predict its y: '))
user_pred = proj_model.predict(np.array([[user_x]])).item()
print(f'For x={user_x}, model predicts y={user_pred:.2f}')
Thank You and Next Steps#
- You now know the basics of TensorFlow.
- Keep trying new examples for practice.
- Subscribe for more tutorials!
- Leave a comment if you want to see more.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



