How can Tensorflow be used to compare the linear model and the Convolutional 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.

The tensorflow package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow that helps connect edges in a flow diagram called the Data Flow Graph. Tensors are multidimensional arrays or lists identified by three main attributes −

  • Rank − The dimensionality of the tensor (number of dimensions)

  • Type − The data type of tensor elements

  • Shape − The number of rows and columns

Keras is a high-level deep learning API written in Python that runs on top of TensorFlow. It provides essential abstractions and building blocks for developing machine learning solutions quickly.

Creating Linear and Convolutional Models

Let's create both models to understand their differences −

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Create a linear model for binary vectorized data
binary_model = keras.Sequential([
    layers.Dense(4, input_shape=(10000,), activation='relu')
])

# Create a ConvNet model for integer vectorized data  
int_model = keras.Sequential([
    layers.Embedding(10000, 64),
    layers.Conv1D(64, 5, activation='relu'),
    layers.GlobalMaxPooling1D(),
    layers.Dense(4, activation='relu')
])

print("The two models are compared")
print("Linear model on binary vectorized data:")
print(binary_model.summary())
print("ConvNet model on int vectorized data:")
print(int_model.summary())
The two models are compared
Linear model on binary vectorized data:
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 4)                 40004     
=================================================================
Total params: 40,004
Trainable params: 40,004
Non-trainable params: 0
_________________________________________________________________
None
ConvNet model on int vectorized data:
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, None, 64)          640000    
_________________________________________________________________
conv1d (Conv1D)              (None, None, 64)          20544     
_________________________________________________________________
global_max_pooling1d (Global (None, 64)                0         
_________________________________________________________________
dense (Dense)                (None, 4)                 260       
=================================================================
Total params: 660,804
Trainable params: 660,804
Non-trainable params: 0
_________________________________________________________________
None

Key Differences

Model Type Input Data Parameters Best For
Linear Model Binary vectors 40,004 Simple classification tasks
ConvNet Model Integer sequences 660,804 Sequential data, text processing

How It Works

  • The linear model uses a single Dense layer that processes binary vectorized input directly

  • The ConvNet model uses an Embedding layer to convert integers to dense vectors, followed by Conv1D for feature extraction

  • The summary() method displays the architecture, output shapes, and parameter counts for comparison

Conclusion

Linear models are simpler with fewer parameters, suitable for basic tasks. ConvNet models are more complex but better for sequential data processing like text classification.

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

240 Views

Advertisements