340🌻) How does Chat GPT work? (e/u)


Behind ChatGPT and other similar applications (such as GeminiDeepSec, and Grok, etc.) is actually deep learning, a branch of artificial intelligence

Deep Neural Network

Deep learning as a whole uses neural networks as a weapon, now a neural network is actually a network of neurons where each neuron is an artificial neuron (which is apparently likened to a natural neuron).

Neuron

Now each neuron has some inputs, which are the same for every neuron. Each neuron takes a unit of data x and multiplies it by a random weight w (initially), and then adds a random bias b to it. 

x' = wx + b

Since this is a linear equation, which makes the model very simple, we have to make it non-linear, for which we use the activation function σ, which changes its nature and makes it non-linear. This helps in understanding the model as a complex relationship. 

a = σ(x') or

a = σ(wx + b) 

Layers

This is a single neuron mechanism, so a collection of all these neurons is called a layer, now this network of layers is called a deep neural network. 

Now each layer sends the calculations of its neurons to the next stage. Which are collected by the neurons of the next layer. We consider them as a vector which is the sum of all the neurons in a layer which is used as input in the next layer. 

X = (a1, a2, a3, ..., an) 

Now we multiply it by the transpose vector of the next initial weights and then add a bias b to it. And then we apply the activation function to it and this sequence is transferred to the next layer. 

When we reach the last layer, which is called the output layer, we stop there and compare our answer with the original answer, i.e.

 error rate or loss 

Then we do the reverse process here, which we call back propagation, which updates the previous random weights, and this is a complete cycle.

 epoch 

Which is a training cycle of a complete data set. 

Then these cycles continue until the model's answer becomes closer to our original answer or no further improvement is possible. 

This was the basic and simple analysis of deep learning. Now let's come to the actual work of GPT. 

Transformer

Behind GPT is an advanced deep learning algorithm that we call the Transformer Model.

Now what is a transformer? A transformer is an advanced sequence-to-sequence model, which takes a sequence of data and inversely generates a new sequence, e.g.

 textual data 

Here each word is connected to a string that depends on the text of the previous sentence.

Audio, video, and time series data are also examples of sequence data. 

Now these transformer models are of three types: 

encoder-only, decoder-only ، encoder-decoder 

(Combination of both) 

Encoder Only

These models understand input sequences, classify them, analyze features, fill in missing words between sentences, and correct misspelled words. 

Decoder Only

These models are a kind of text generator, which takes an input sequence and then, based on their training data, finds a list of new possible words in the sequence and then selects the words that are most likely to occur. And so on. 

Similarly, models like Chat GPT

 decoder-only 

Transformers are a modern form of the model that generates new words based on text. 

Encoder-decoder

It is a combination of both that analyze the input and output sequences, such as the encoder taking the input sequence while the decoder taking the output sequence.

Now both of them take the input and output sequences, the encoder analyzes the input (i.e. the source language) and then sends them to the decoder and the decoder analyzes the output (i.e. the target language) and converts it into new words (of the target language) based on the encoder sequence.

 prediction

Do.

Features of a transformer

Now coming to its features, let us understand the encoder-decoder architecture in it. 

First of all we take input and output sequence data like input data is English sentences and output data is Urdu sentences. 

Tokenization and positional encoding

First we tokenize them (i.e. break them into words) and then we

 embedding

This refers to a vector representation that shows how similar two words are to each other. 

Then we do their positional encoding, in which we calculate the position of the word order, and then we pass this data to the encoder and decoder. 

Now the decoder and encoder process this data further. 

Now these are the main pillars of the data transformer.

 multi head self attention

 Passes. 

Multi-head self-attention

Here multi-head means more than one process head that processes in parallel, we initially take 8 (as explained in the original paper). 

Now, to explain self-attention in simple terms, it actually understands the relationship between its own sequence data and updates the weights based on the relationship. 

Similarly, in the decoder, we use multi-mask self-attention, in which we mask some words so that they cannot see the next words during training. Which we call

 casual

 It's called masking. 

Then we collect all the heads and then normalize them (i.e. normalize

distribution 

It is normalized under 

 Encoder

Then we train it by sending it to a feedforward network (as we understood in the deep neural network above) during encoding and then we send its weights to the decoder. And then they are once again collected and normalized and here the encoding work is finished. 

Decoder

While we are in the decoder 

cross head attention

 , which detects the relationship between the data from both the encoder and decoder and then uses it to decode the data from the decoder.

 feed forward network 

We send it to the I. And then it is again collected and normalized in the above-mentioned way and here the decoding work is finished. 

Output

Then in the final stage of the model

 output

 To 

Linear Layer 

is passed through, which makes this output the model's 

vocabulary on map

 That is, it assigns a value to each possible word.

Then these prices

 Softmax Function 

is poured into, which they

 probability

 It changes the word to find out what the next word is most likely to be. Then the most likely word is selected as the final output.

This was a comprehensive and complete process through which he received training on data. 

And then the same process runs during inference, that is, generation, which generates the next word.

چیٹ جی پی ٹی کیسے کام کرتا ہے؟ 

چیٹ جی پی ٹی اور دیگر اس طرح کی ایپلیکشن (جیسے جیمنائی، ڈیپ سیک اور گروک وغیرہ) کے پیچھے دراصل مصنوعی ذہانت کی ایک شاخ ڈیپ لرننگ کار فرما ہوتی ہے۔ 

ڈیپ نیورل نیٹ ورک

ڈیپ لرننگ مجموعی طور پر نیورل نیٹ ورکس کو بطور ہتھیار استعمال کرتا ہے، اب نیورل نیٹ ورک دراصل نیورنز کا جال ہے جہاں ہر ایک نیورون ایک مصنوعی نیورون ہے (جسے بظاہر نیچرل نیورون سے تشبیہ دی جاتی ہے)

نیورون

اب ہر ایک نیورون کے کچھ خدودخال ہے، جو کہ ہر نیورون میں یکساں ہوتے ہیں۔ ہر نیورون ڈیٹا کی ایک اکائی x کو لیتا ہے اور اسے ایک رینڈم ویٹ w (ابتدائی طور پر) سے ضرب دیتا ہے, اور پھر اس میں ایک رینڈم بائیس بی میں جمع کرتا ہے۔ 

x' = wx + b

چونکہ یہ ایک لینئیر ایکویشن ہے، جو کہ ماڈل کو بہت سادہ بنا دیتا ہے تو اسے ہمیں نان لینئیر بنانا ہوتا ہے جس کیلئے ہم ایکٹیویشن فنکشن σ استعمال کرتے ہیں جو کہ اس کی نیچر کو تبدیل کرکے نان لینیر بنادیتا ہے۔ جو کہ ماڈل کو کمپلیکس ریلیشن شپ سمجھنے میں مدد دیتا ہے 

a = σ(x') or

a = σ(wx + b) 

لیئیرز

یہ ایک نیورون کا میکنیزم ہے اس طرح یہ سارے نیورونز کا ایک مجموعہ ایک لیئیر کہلاتی ہے، اب یہ لئیرز کا جال ہی ڈیپ نیورل نیٹ ورک کہلاتا ہے۔ 

اب ہر لیئیر اپنے نیورونز کی ان کیلکولیشن کو اگلے مرحلے میں بھیجتا ہے۔ جو کہ اگلی لیئیر کے نیورونز میں جمع ہوکر جاتی ہیں۔ جنہیں سارے نیورونز اے کیلکولیٹ کرتے ہیں انہیں ہم ایک ویکٹر سمجھ لیتے ہیں جو ایک لیئیر میں سارے نیورنز کا مجموعہ ہے جو کہ اگلے لیئیر میں بطور ان پٹ استعمال ہوتا ہے۔ 

X = (a1, a2, a3, ..., an) 

اب اسے ہم اگلے ابتدائی ویٹس کے ٹرانسپوز ویکٹر سے ضرب دے کر پھر اس میں بائیس b کا اضافہ کرتے ہیں۔ اور پھر اس پر ایکٹیوشن فنکشن کا اطلاق کرتے ہیں اور یہی سلسلہ اگلی لیئیر میں منتقل ہوجاتا ہے۔ 

جب آخری لیئیر پر پہنچتے ہیں جو کہ آوٹ پٹ لیئیر کہلاتی ہے، تو پھر ہم وہاں رک کر اسے اپنے جواب کو اصل جواب سے موازنہ کرتے ہیں یعنی

 error rate or loss 

معلوم کرتے ہیں۔ پھر ہم یہاں اسے ریورس پروسیس کرتے ہیں جسے ہم بیک پروپیگیشن کہتے ہیں جو کہ پچھلے رینڈم ویٹس کو اپڈیٹ کرتے ہیں اور یہ ایک مکمل سائیکل ایک

 epoch 

کہلاتی ہے۔ جو کہ ایک مکمل ڈیٹا سیٹ کا ٹریننگ سائیکل ہے۔ 

پھر یہ سائیکل جب تک چلتی ہیں جب تک کہ ماڈل کا جواب ہمارے اصل جواب سے قریب تر نہ ہوجائے یا اس میں مزید بہتری ممکن نہیں۔ 

یہ تھا ڈیپ لرننگ کا بنیادی اور سادہ تجزیہ اب آتے ہیں جی پی ٹی کی اصل کام کی طرف۔ 

ٹرانسفارمر

جی پی ٹی کے پیچھے ایک ایڈوانس ڈیپ لرننگ الگورتھم موجود ہوتا ہے جسے ہم ٹرانسفارمر ماڈل کہتے ہیں۔

اب ٹرانسفارمر کیا ہے؟ ٹرانسفارمر ایک جدید سیکوینس ٹو سیکوینس ماڈل ہے، جو کہ ڈیٹا کا سیکونس لیتا ہے اور اس کے برعکس نئی سیکونس جنریٹ کرتا ہے جیسے

 textual data 

یہاں ہر ایک لفظ ایک لڑی سے جڑا ہے جو پچھلے جملے کے متن پر منحصر ہے۔

آڈیو، ویڈیو، اور ٹائم سریز ڈیٹا بھی سیکونس ڈیٹا کی ہی مثال ہے۔ 

اب یہ ٹرانسفارمر ماڈل تین طرح کے ہوتے ہیں: 

encoder-only, decoder-only ، encoder-decoder 

(دونوں کا مجموعہ) 

انکوڈر اونلی

یہ ماڈل ان پٹ سیکونس  کو سمجھتے ہیں، ان کی درجہ بندی کرتے ہیں، فیچر کا تجزیہ کرتے ہیں, جملے کے درمیان گم شدہ الفاظ کو پر کرتے ہیں اور غلط الفاظ کو درست کرتے ہیں۔ 

 ڈیکوڈر اونلی

یہ ماڈل ایک طرح کا ٹیکسٹ جنریٹر ہیں، جو کہ ان پٹ سیکونس لیتے ہیں اور پھر اپنے تربیت شدہ ڈیٹا کی بنیاد پر سیکوینس  میں نئے امکانات شدہ لفظ کی فہرست تلاش کرتے ہیں اور پھر جن لفظ کا امکان زیادہ ہوتا انہیں چن لیا جاتا ہے۔ اور پھر یہ سلسلہ چلتا رہتا ہے۔ 

اسی طرح چیٹ جی پی ٹی جیسے ماڈل

 decoder-only 

ٹرانسفارمر ماڈل کی ایک جدید شکل ہے۔ جو متن کی بنیاد پر نئے الفاظ جنریٹ کرتے ہیں۔ 

انکوڈر-ڈیکوڈر

یہ دونوں کا مجموعہ ہے جو کہ ان پٹ اور آوٹ پٹ سیکونس کا تجزیہ کرتے ہیں، جیسے انکوڈر ان پٹ سیکونس جبکہ ڈیکوڈر آوٹ پٹ سیکونس کو لیتے ہیں۔

اب یہ دونوں ان پٹ اور آوٹ پٹ کے سیکونس کو لیتے ہیں، ان کوڈر ان پٹ (یعنی سورس لینگویج) کا تجزیہ کرتے ہیں اور پھر انہیں ڈیکوڈر کو بھیج دیتے ہیں اور ڈیکوڈر آوٹ پٹ (یعنی ٹارگٹ لینگویج) کا تجزیہ کرتے ہیں اور اسے انکوڈر سیکونس کی بنیاد پر (ٹارگٹ لینگویج کے) نئے الفاظ کی

 prediction

کرتے ہیں۔

ٹرانسفارمر کے خدوخال

اب آتے ہیں اس کے خدوخال کی طرف،  چلیں ہم اس میں انکوڈر-ڈیکوڈر آرکیٹیکچر کو سمجھتے ہیں۔ 

سب سے پہلے ہم ان پٹ اور آوٹ پٹ سیکونس ڈیٹا لے لیتے ہیں جیسے ان پٹ ڈیٹا انگریزی جملے اور آوٹ پٹ ڈیٹا اردو جملے ہیں 

ٹوکنائزیشن اور پوزیش انکوڈنگ

پہلے ہم انہیں ٹوکنائز کرتے ہیں (یعنی لفظوں میں توڑ دیتے ہیں) اور پھر ہم انکی

 embedding

بناتے ہیں اس سے مراد ایک ویکٹر ریپریزینٹیشن ہے جو کہ یہ بتاتا ہے کہ دو الفاظ آپس میں کتنے مطابقت رکھتے ہیں۔ 

اسکے بعد ہم ان کی پوزیشنل انکوڈنگ کرتے ہیں جس میں ہم الفاظوں کے ترتیب کی  پوزیشن کو کیلکولیٹ کرتے ہیں، اور پھر ہم یہ ڈیٹا انکوڈر اور ڈیکوڈر کو دے دیتے ہیں۔ 

اب ڈیکوڈر اور انکوڈر اس ڈیٹا کو مزید پروسیس کرتے ہیں۔ 

اب یہ ڈیٹا ٹرانسفارمر کے اہم ستون

 multi head self attention

 کو پاس ہوجاتا ہے۔ 

ملٹی ہیڈ سیلف اٹینشن

یہاں ملٹی ہیڈ سے مراد ایک سے زیادہ پروسیس ہیڈ ہیں جو کہ پیرالل میں پروسیس ہوتے ہیں، ہم ابتدائی طور پر ۸ لیتے ہیں (جیسا کہ اصل پیپر میں واضح ہے) 

اب سیلف اٹینشن کو آسان الفاظ میں سمجھایا جائے تو یہ دراصل خود کے سیکونس ڈیٹا کے درمیان تعلق کو سمجھتا ہے اور تعلق کی بنیاد پر ویٹس کو اپڈیٹ کرتا ہے۔ 

اسی طرح ڈیکوڈر میں ہم ملٹی ماسک سیلف اٹینشن کا استعمال کرتے ہیں جس میں ہم کچھ الفاظ کو ماسک یعنی چھپا دیتے ہیں تاکہ وہ ٹریننگ کے دوران اگلے الفاظ نہ دیکھ سکے۔ جسے ہم

 casual

 ماسکنگ کہتے ہیں۔ 

پھر ہم سارے ہیڈز کو جمع کرتے ہیں اور پھر انہیں نارملائز کرتے ہیں (یعنی نارمل

distribution 

کے تحت اسے نارملائز کیا جاتا ہے) 

 انکوڈر

پھر ہم اسے انکوڈنگ کے دوران فیڈ فاروڈ نیٹ ورک (جیسا کہ ہم نے اوپر ڈیپ نیورل نیٹ ورک میں سمجھا تھا) میں بھیج کر ٹرین کرتے ہیں اور پھر اس کی ویٹس کو ہم ڈیکوڈر میں بھیج دیتے ہیں۔ اور پھر انہیں ایک بار پھر جمع اور نارملائز کیا جاتا ہے  اور یہاں انکوڈنگ کا کام ختم ہو جاتا ہے۔ 

ڈیکوڈر

جبکہ ہم ڈی کوڈر میں 

cross head attention

 کا استعمال کرتے ہیں، جو کہ دونوں انکوڈر اور ڈیکوڈر ڈیٹا کے درمیان تعلق کو معلوم کرتا ہے اور اسکے بعد اسے ڈیکوڈر کے

 feed forward network 

میں بھیج دیتے ہیں۔ اور پھر اسے دوبارہ مزکورہ طریقے سے جمع اور نارملائز کیا جاتا ہے اور یہاں ڈیکوڈنگ کا کام ختم ہو جاتا ہے۔ 

آوٹ پٹ

اس کے بعد ماڈل کے آخری مرحلے میں

 output

 کو 

Linear Layer 

سے گزارا جاتا ہے، جو اس آوٹ پٹ کو ماڈل کی 

vocabulary on map

 کرتا ہے۔ یعنی ہر ممکنہ لفظ کیلئے ایک قیمت طے کرتا ہے۔

پھر ان قیمت کو

 Softmax Function 

میں ڈالا جاتا ہے، جو ان کو

 probability

 میں تبدیل کر دیتا ہے تاکہ یہ معلوم ہو کہ اگلے لفظ کے ہونے کا سب سے زیادہ امکان کیا ہے۔  پھر سب سے زیادہ امکان والا لفظ اخر آوٹ پٹ کے طور پر منتخب کر لیا جاتا ہے۔

یہ تھا ایک جامع اور مکمل پروسیس جس سے گزر کر ڈیٹا پر یہ تربیت حاصل کرتا ہے۔ 

اور پھر یہی پروسیس انفرینس یعنی جنریشن کے وقت چلتا ہے جس سے اگلا لفظ جنریٹ ہوتا ہے

Comments