[go: up one dir, main page]

0% found this document useful (0 votes)
23 views6 pages

NLP Assignment (213-15-4243)

This document outlines an assignment comparing TensorFlow/Keras and PyTorch for implementing a simple text classification model using a small dataset. It details the model architecture, training procedures, and evaluation methods for both frameworks, highlighting key differences such as abstraction levels, model definition, and learning curves. The conclusion emphasizes that TensorFlow is suited for ease of use in production, while PyTorch offers more flexibility for research-oriented tasks.

Uploaded by

akash.alamin.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

NLP Assignment (213-15-4243)

This document outlines an assignment comparing TensorFlow/Keras and PyTorch for implementing a simple text classification model using a small dataset. It details the model architecture, training procedures, and evaluation methods for both frameworks, highlighting key differences such as abstraction levels, model definition, and learning curves. The conclusion emphasizes that TensorFlow is suited for ease of use in production, while PyTorch offers more flexibility for research-oriented tasks.

Uploaded by

akash.alamin.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Course Code: CSE445

Course Name: Computer Graphics Lab

Assignment

Course Teacher: Submitted By


Md. Abdullah Al Kafi ​ Md. Iftekhar Rahman Likhan
Lecturer​ ​ ​ ID: 213-15-4243
Section: 60_B
Department of CSE Department of CSE
Daffodil International Daffodil International
University University

Date of Submission: 17/04/2025


Objective:

The task is to compare the implementation of a simple text classification model using
TensorFlow/Keras and PyTorch, specifically focusing on the differences in code, model
structure, and training procedure. We will use a small dataset of text inputs and
corresponding labels for the classification task.

Dataset:

For this assignment, we will use a small sample dataset consisting of four text samples,
each labeled with a binary classification label (0 or 1).

Sample texts:

1.​ "I love programming" -> Label: 1​

2.​ "TensorFlow is great" -> Label: 0​

3.​ "PyTorch is powerful" -> Label: 0​

4.​ "I enjoy machine learning" -> Label: 1

Approach:

We will implement a simple Recurrent Neural Network (RNN) model using both
TensorFlow and PyTorch, specifically utilizing LSTM (Long Short-Term Memory) layers
for text classification.

We will compare the following aspects:

1.​ Model Definition: How to define the model architecture.​

2.​ Training Procedure: How the training loop is set up.​

3.​ Evaluation: How the accuracy is evaluated and predictions are made.​

1. TensorFlow/Keras Implementation
TensorFlow is widely used for its high-level API Keras, which simplifies the process of
building neural networks. Here is how we can implement the text classification task in
TensorFlow.
TensorFlow Code:

Explanation:

1.​ Tokenizer: We use Tokenizer to convert text into numerical sequences. It


creates a word index and transforms the text into a list of integers, which is
needed for input into neural networks.​

2.​ Model Architecture: We define a simple neural network with an Embedding layer
(for text representation), an LSTM layer (for sequence learning), and a Dense
layer (for binary classification).​

3.​ Model Training: We use the fit() method to train the model on the data,
specifying the number of epochs and batch size.
2. PyTorch Implementation
PyTorch provides more flexibility and control compared to TensorFlow, which can be
beneficial in more complex tasks. Below is the implementation of the same text
classification task in PyTorch.

PyTorch Code:
Explanation:

1.​ Vectorizer: In this implementation, we use CountVectorizer from scikit-learn


to convert text to a matrix of token counts.​

2.​ Model Architecture: We define a custom TextClassifier class that includes


an Embedding layer, an LSTM layer, and a fully connected (Linear) output
layer.​

3.​ Training Loop: Unlike TensorFlow, in PyTorch, we manually define the training
loop, specifying the forward pass, backward pass, and optimizer updates.​

4.​ Model Evaluation: We evaluate the model's performance on the test set and print
predictions along with true labels.

3. Comparison of TensorFlow vs PyTorch


Key Differences:

1.​ Abstraction Level:​

○​ TensorFlow: Offers higher-level abstractions like Keras, which simplify the


code and make it easier to use, especially for beginners.​

○​ PyTorch: Provides lower-level control, which is more flexible but requires


manual management of the training loop, making it more suitable for
research and experimentation.​

2.​ Model Definition:​

○​ TensorFlow: Uses the Sequential API or functional API, making model


definition simpler.​

○​ PyTorch: Requires subclassing nn.Module to define a custom model,


providing more control over the network structure.​

3.​ Training Procedure:​


○​ TensorFlow: Training is handled automatically by the fit() function.​

○​ PyTorch: The training loop is manually written, which offers more flexibility
but requires more boilerplate code.​

4.​ Learning Curve:​

○​ TensorFlow: Easier to learn for beginners due to its high-level abstractions


and better documentation.​

○​ PyTorch: Steeper learning curve but offers more flexibility and control,
which can be useful for advanced use cases.​

5.​ Performance:​

○​ Both frameworks are highly optimized for performance, but PyTorch is


known to offer better support for dynamic graphs, while TensorFlow is
preferred in production environments with static graphs.

Conclusion:
Both TensorFlow and PyTorch are excellent frameworks for deep learning, and the
choice depends on your needs:

●​ TensorFlow is better for those who prefer higher-level APIs and ease of use,
especially in production settings.​

●​ PyTorch is ideal for those who need more flexibility and control over their models
and prefer a more research-oriented approach.​

This assignment has provided a basic comparison of both frameworks in the context of
a simple text classification task. As you gain more experience, you can dive deeper into
each framework's advanced features.

You might also like