Updated 19 September 2023
In the new world of online shopping built on various tech stacks like Symfony, laravel eCommerce etc., the scope of AI and Natural Language Processing is continuously rising at high speed.
While dealing with NLP, we often encounter terms like Fine-Tuning a pre-trained model and embedding. Most of the time people get confused between them and don’t know which to use when.
Today in this blog, we will clear the difference between them and their use cases.
Embeddings: Embeddings are multi-dimensional vector representations of words. These vectors represent the meaning of the word in numerical language. They are the building blocks for Natural Language Processing. Hence, they will be used in every NLP task, directly or indirectly.
1 2 3 4 |
"cat": [0.2, 0.5, -0.1, 0.8, 0.3, -0.4, -0.2, 0.6, 0.7, -0.5, 0.2, 0.9, -0.3, -0.7, -0.6, 0.4, -0.8, 0.1, -0.9, 0.0] "dog": [0.1, 0.6, -0.2, 0.7, 0.4, -0.5, -0.3, 0.5, 0.8, -0.4, 0.1, 0.7, -0.2, -0.6, -0.5, 0.3, -0.7, 0.2, -0.8, 0.0] "fish": [0.3, 0.4, -0.3, 0.9, 0.2, -0.3, -0.1, 0.7, 0.6, -0.3, 0.3, 0.8, -0.1, -0.5, -0.4, 0.5, -0.9, 0.0, -0.7, 0.1] "bird": [0.4, 0.3, -0.4, 0.6, 0.1, -0.2, -0.4, 0.8, 0.5, -0.2, 0.4, 0.5, -0.4, -0.4, -0.3, 0.6, -0.6, 0.3, -0.6, 0.2] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from transformers import AutoModel, AutoTokenizer, pipeline tokenizer = AutoTokenizer.from_pretrained('xlnet-base-cased') model = AutoModel.from_pretrained("xlnet-base-cased") tokenizer.add_special_tokens({'pad_token':'[PAD]'}) model.resize_token_embeddings(len(tokenizer)) model_pipeline = pipeline('feature-extraction', model='xlnet-base-cased', tokenizer=tokenizer) data=model_pipeline("My name is Amit") data <output>: [[[0.7087899446487427, -7.2828168869018555, -0.6374418139457703, -2.0751280784606934, -0.45991745591163635, 1.2415050268173218, -2.2894093990325928, 1.744140863418579, ... -4.4170989990234375, 1.4603062868118286, -2.608621835708618, -0.1163523867726326, 1.0641734600067139]]] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings(openai_api_key="your openai api key") query="I love Football." query_embeddings=embeddings.embed_documents(query.split(" ")) import numpy as np print(query_embeddings) <output>: [[-0.013148742383117574, -0.03375915215991781, 0.007804009480188148, -0.014490166394041703, -0.020861926487810194, 0.019394745052703704, -0.014420300434022799, -0.021267148683390785, 0.012925171869850654, -0.005603236861867104, 0.01721493231552059, 0.017717965737540503, 0.015971320462358402, 0.005302814071976177, ... -0.012498991190116058, 0.0016217598017991166, -0.011206473723734208, -0.015929401631405153, -0.009536681190837422, 0.009382976637589406]] |
Fine Tuning a Pre-Trained LLM: Fine Tuning of Pre-Trained LLMs is done specifically for complex tasks like text generation. Preparing such a model from scratch requires a lot of data, computation resources, and a dedicated team of Data Scientists and ML Engineers to build.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import { ChatOpenAI } from "langchain/chat_models/openai"; import { ConversationalRetrievalQAChain } from "langchain/chains"; import { HNSWLib } from "langchain/vectorstores/hnswlib"; import { OpenAIEmbeddings } from "langchain/embeddings/openai"; import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; import { BufferMemory } from "langchain/memory"; import * as fs from "fs"; export const run = async () => { /* Initialize the LLM to use to answer the question */ const model = new ChatOpenAI({}); /* Load in the file we want to do question answering over */ const text = fs.readFileSync("state_of_the_union.txt", "utf8"); /* Split the text into chunks */ const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); const docs = await textSplitter.createDocuments([text]); /* Create the vectorstore */ const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()); /* Create the chain */ const chain = ConversationalRetrievalQAChain.fromLLM( model, vectorStore.asRetriever(), { memory: new BufferMemory({ memoryKey: "chat_history", // Must be set to "chat_history" }), } ); /* Ask it a question */ const question = "What did the president say about Justice Breyer?"; const res = await chain.call({ question }); console.log(res); /* Ask it a follow up question */ const followUpRes = await chain.call({ question: "Was that nice?", }); console.log(followUpRes); }; |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.