avatarNaina Chaturvedi

Summary

The webpage content outlines a comprehensive guide on transfer learning and text classification using TensorFlow, emphasizing the use of pre-trained NLP models from TensorFlow Hub for fine-tuning models, and provides resources for further learning in data science and machine learning.

Abstract

The provided web content delves into the application of transfer learning within the realm of natural language processing (NLP) and text classification. It introduces TensorFlow, an open-source platform for machine learning, and discusses its utility in handling large datasets through tensors and data flow graphs. The article highlights the importance of transfer learning in leveraging pre-trained models to improve machine learning tasks, particularly in NLP. It also includes a practical example of using TensorFlow Hub to access pre-trained text embedding models for fine-tuning classification models. Additionally, the content offers a plethora of educational resources and project series for readers interested in deepening their knowledge in various areas of data science, machine learning, and related technologies. The author encourages readers to subscribe to a tech newsletter and a newly launched YouTube channel for more insights and project tutorials.

Opinions

  • The author believes that transfer learning is a pivotal concept in machine learning, especially for tasks involving natural language processing.
  • TensorFlow is presented as a powerful tool for machine learning and deep learning, capable of processing large datasets efficiently.
  • Pre-trained models from TensorFlow Hub are considered valuable resources for practitioners looking to fine-tune models without starting from scratch.
  • The article suggests that the vastness of transfer learning makes it the next frontier of machine learning.
  • By providing a range of project videos, the author implies that hands-on practice is crucial for understanding and applying data science and machine learning techniques.
  • The encouragement to join the Tech Brew newsletter indicates the author's commitment to sharing knowledge and fostering a community of learners in tech.
  • The author emphasizes the importance of practical implementation by showcasing various projects and their code implementations, suggesting a learn-by-doing approach.
  • The inclusion of a quote by Grace Hopper at the end of the article reflects the author's philosophy that true progress and learning come from venturing beyond one's comfort zone.

Day 60: 60 days of Data Science and Machine Learning Series

Transfer learning and Text Classification…

Pic credits : ResearchGate

Tensorflow is an open source platform for machine learning and deep learning developed by Google Brain Team and written in C++, Python, and CUDA created for large numerical computations and deep learning. It ingests the data in the form of tensors which are nothing but multi-dimensional arrays of higher dimensions to handle large amounts of data. It works on the data flow graphs that have nodes and edges and supports both CPUs and GPUs. It works by preprocessing the data, building the model, training and estimating the model.

Pic credits : Tensorflow org

A good reference to Tensorflow ( used in this project as well ) —

Natural Language Processing is a branch of linguistics, AI and CS for manipulation, translation of natural language which gives the machines an ability to read, understand and derive meaning from human language.

Some of the other best Series —

30 Days of Natural Language Processing ( NLP) Series

30 days of Data Engineering with projects Series

60 days of Data Science and ML Series with projects

100 days : Your Data Science and Machine Learning Degree Series with projects

23 Data Science Techniques You Should Know

Tech Interview Series — Curated List of coding questions

Complete System Design with most popular Questions Series

Complete Data Visualization and Pre-processing Series with projects

Complete Python Series with Projects

Complete Advanced Python Series with Projects

Kaggle Best Notebooks that will teach you the most

Complete Developers Guide to Git

All the Data Science and Machine Learning Resources

210 Machine Learning Projects

30 days of Machine Learning Ops

Projects Videos —

All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).

Subscribe today!

Tech Newsletter —

If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :

According to Pan and Yang (2010) — Transfer learning involves the concepts of a domain and a task. A domain DD consists of a feature space XX and a marginal probability distribution P(X)P(X) over the feature space, where X=x1,⋯,xn∈XX=x1,⋯,xn∈X. For document classification with a bag-of-words representation, XX is the space of all document representations, xixi is the ii-th term vector corresponding to some document and XX is the sample of documents used for training.

Pic credits : ResearchGate

A good reference point to understand the vastness of Transfer learning and why’s it’s considered to be the next frontier of ML —

In this project we are going to learn how to use transfer learning to fine-tune models, use pre-trained NLP text embedding models from TensorFlow Hub.

Let’s dive in!

Import necessary libraries

import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (12, 8)
from  IPython import display
import pathlib
import shutil
import tempfile
!pip install -q git+https://github.com/tensorflow/docs
import tensorflow_docs as tfdocs
import tensorflow_docs.modeling
import tensorflow_docs.plots
print("Version: ", tf.__version__)
print("Hub version: ", hub.__version__)
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
logdir = pathlib.Path(tempfile.mkdtemp())/"tensorboard_logs"
shutil.rmtree(logdir, ignore_errors=True)
from sklearn.model_selection import train_test_split

Output —

Version:  2.7.0
Hub version:  0.12.0
GPU is available

Download the Dataset and split the data to train and test

df = pd.read_csv('Path to data file/data.csv.zip',
                 compression='zip',low_memory=False)
df['target'].plot(kind='hist',title='Target distribution')

train_df, r = train_test_split(df,random_state=42,train_size=0.01,stratify=df.target.values)
valid_df, _ = train_test_split(r,random_state=42,train_size=0.001,stratify=r.target.values)

Output —

Build and Compile Models

def train_and_evaluate_model(module_url, embed_size, name, trainable=False):
  hub_layer = hub.KerasLayer(module_url,input_shape=[],output_shape=[embed_size],dtye=tf.string,trainable=trainable)
  m = tf.keras,models.Sequential(
      [
         hub_layer, 
        tf.keras.layers.Dense(256,activation='relu'),
        tf.keras.layers.Dense(64,activation='relu'),
        tf.keras.layers.Dense(1,activation ='sigmoid')
]
  )
  
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001),
              loss = tf.losses.BinaryCrossentropy(),
              metrics = [tf.metrics.BinaryAccuracy(name='accuracy')]
              
              )
history = model.fit(train_df['question_text'], train_df['target'],epochs=100,batch_size=32,
validation_data = (valid_df['question_text'],valid_df['target'])
                           verbose=0                    
                    
                    )
return history

Train Text Classification Models

hs= []
module_url = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1" #@param ["https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1", "https://tfhub.dev/google/tf2-preview/nnlm-en-dim50/1", "https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1", "https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5"] {allow-input: true}
hs['nnln-en-dim50'] = train_and_evaluate_model(module_url,embed_size=20,name='nnln-en-dim50')

Accuracy vs Loss

plt.rcParams['figure.figsize'] = (12, 8)
plotter = tfdocs.plots.HistoryPlotter(metric = 'accuracy')
plotter.plot(histories)
plt.xlabel("Epochs")
plt.legend(bbox_to_anchor=(1.0, 1.0), loc='upper left')
plt.title("Accuracy Curves for Models")
plotter = tfdocs.plots.HistoryPlotter(metric = 'loss')
plotter.plot(histories)
plt.xlabel("Epochs")
plt.legend(bbox_to_anchor=(1.0, 1.0), loc='upper left')
plt.title("Loss Curves for Models")

Learnings —

How to perform transfer learning to fine-tune models, use pre-trained NLP text embedding models from TensorFlow Hub.

The End!

Follow and Stay tuned. Keep coding :)

For other projects, tune to —

Build Machine Learning Pipelines( With Code)

Recurrent Neural Network with Keras

Clustering Geolocation Data in Python using DBSCAN and K-Means

Facial Expression Recognition using Keras

Hyperparameter Tuning with Keras Tuner

Custom Layers in Keras

That’s it fellas. Peace out and keep coding :)

Happy new year 2022!

Stay Tuned and of-course let me end this post with a quote by Grace Hopper ( a female pioneer of computer programming, helped built UNIVAC I, the first commercial electronic computer, and naval applications for COBOL)

“A ship in harbor is safe — but that is not what ships are built for.”

Machine Learning
Artificial Intelligence
Data Science
Tech
Programming
Recommended from ReadMedium