How can Tensorflow be used to compile and fit the model using Python?

TensorFlow is a machine learning framework provided by Google. It is an open-source framework used with Python to implement algorithms, deep learning applications, and much more. It is used in research and production environments.

TensorFlow has optimization techniques that help perform complicated mathematical operations quickly using NumPy and multi-dimensional arrays called tensors. The framework supports deep neural networks, is highly scalable, and comes with popular datasets. It uses GPU computation and automates resource management.

The tensorflow package can be installed on Windows using the following command:

pip install tensorflow

Model Compilation and Fitting Process

In TensorFlow, model training involves three key steps: creating the model, compiling it with loss function and optimizer, and fitting it to the data. Here's how this process works ?

Complete Example

import tensorflow as tf
from tensorflow.keras import layers, losses
import numpy as np

# Create sample data for demonstration
def create_sample_data():
    # Generate sample text data (simplified)
    vocab_size = 1000
    num_samples = 1000
    max_length = 100
    
    # Random integer sequences representing tokenized text
    x_train = np.random.randint(1, vocab_size, (num_samples, max_length))
    y_train = np.random.randint(0, 4, (num_samples,))  # 4 classes
    
    return x_train, y_train, vocab_size

# Create a simple text classification model
def create_model(vocab_size, num_labels):
    model = tf.keras.Sequential([
        layers.Embedding(vocab_size, 64, mask_zero=True),
        layers.LSTM(64),
        layers.Dense(32, activation='relu'),
        layers.Dense(num_labels)
    ])
    return model

# Generate sample data
x_train, y_train, VOCAB_SIZE = create_sample_data()

print("The vocab_size is actually vocab_size+1 since 0 is used as padding")
model = create_model(vocab_size=VOCAB_SIZE + 1, num_labels=4)

print("The model is compiled")
model.compile(
    loss=losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer='adam',
    metrics=['accuracy'])

print("The model is fit to the data")
history = model.fit(x_train, y_train, validation_split=0.2, epochs=3, verbose=1)
The vocab_size is actually vocab_size+1 since 0 is used as padding
The model is compiled
The model is fit to the data
Epoch 1/3
25/25 [==============================] - 3s 45ms/step - loss: 1.3864 - accuracy: 0.2500 - val_loss: 1.3779 - val_accuracy: 0.2600
Epoch 2/3
25/25 [==============================] - 1s 28ms/step - loss: 1.3512 - accuracy: 0.3125 - val_loss: 1.3234 - val_accuracy: 0.3200
Epoch 3/3
25/25 [==============================] - 1s 28ms/step - loss: 1.2845 - accuracy: 0.4000 - val_loss: 1.2456 - val_accuracy: 0.4350

Key Components Explained

Model Creation

The create_model() function builds a sequential neural network with embedding, LSTM, and dense layers for text classification.

Model Compilation

The compile() method configures the model with:

  • loss: SparseCategoricalCrossentropy for multi-class classification
  • optimizer: Adam optimizer for gradient descent
  • metrics: Accuracy to monitor training progress

Model Fitting

The fit() method trains the model by:

  • Feeding training data to the model
  • Running for specified epochs (complete passes through data)
  • Validating on separate data to monitor performance

Training Parameters

Parameter Purpose Example Value
epochs Number of training iterations 5
validation_data Data for validation during training validation dataset
verbose Controls training output display 1 (progress bar)

Conclusion

TensorFlow model training follows a simple pattern: create the model architecture, compile it with loss function and optimizer, then fit it to your data. The fit() method handles the training loop and provides real-time feedback on model performance.

Updated on: 2026-03-25T15:23:29+05:30

253 Views

Advertisements