Mathew K Analytics

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.…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb

Introduction 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)
tf.Tensor(5, shape=(), dtype=int32)
# Tensor with multiple values
b = tf.constant([1, 2, 3, 4])
print(b)
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
# Checking the shape and type of a tensor
print(b.shape)
print(b.dtype)
(4,)
<dtype: 'int32'>

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)
tf.Tensor([11 22 33], shape=(3,), dtype=int32)
m = tf.constant([2, 3, 4])
n = tf.constant([5, 6, 7])
mul = m * n
print(mul)
tf.Tensor([10 18 28], shape=(3,), dtype=int32)
# Convert a tensor to a NumPy array
arr = b.numpy()
print(arr)
[1 2 3 4]

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)
<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([10, 20, 30], dtype=int32)>
<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([40, 50, 60], dtype=int32)>

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())
5 + 3 = 8
6 * 7 = 42

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())
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type)                    ┃ Output Shape           ┃       Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ dense (Dense)                   │ (None, 1)              │             2 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
 Total params: 2 (8.00 B)
 Trainable params: 2 (8.00 B)
 Non-trainable params: 0 (0.00 B)
None
# 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)
<keras.src.callbacks.history.History at 0x218acef5b50>
# Use the model to make a prediction
import numpy as np
print("If X is 7, Y is", model.predict(np.array([[7.0]])))
 
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 68ms/step
If X is 7, Y is [[13.975991]]

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())
Feature: 1 Label: 4
Feature: 2 Label: 5
Feature: 3 Label: 6

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!')
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())
WARNING:tensorflow:From c:\Users\makmw\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\tensorflow\core.py:232: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

Output: [[6.]]

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.')
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)
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
For x=10, predicted y is [[31.006624]]
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}')
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step
For x=5.0, model predicts y=16.00

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.