Functional Magic Behind an AI Model.

Is it possible to explain the magic behind the functionality of an AI model simplistically???

Yes, It is just a mathematical model (neural network) that "figures out" the function on its own, during training of the model, based on the data that we feed it upon. Whenever, then, you are given an input and the model will give you back an output by performing "that" function.? Don't you believe in it ?

Let me create a very simple "neural network" with only one layer that accepts 10 numbers as input and a scalar value as an output.??

I train this "neural network model" with input data of random numbers but ensuring that the output should be "5" as it is my lucky number.?

I chose SGD (Stochastic Gradient Descent) optimizer for back-propagation which is the backbone for the model to come up with a relevant function. I ran the training 200 (epochs) times.

That is, it. Run the following code and observe the results.

import torch 
import torch.nn as nn 
import torch.optim as optim 
import torchmetrics  
	  
model = nn.Linear(10,1) 
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.1)
criterion = nn.MSELoss()
    
target = torch.tensor([[5.0]])

for epoch in  range(200):
    optimizer.zero_grad()
    x = torch.rand(size=(1,10), requires_grad=True)
    output = model(x)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
	    
torch.save(model,"layer.pth")

 #Load the saved model back
loaded_model = torch.load("layer.pth")
    
# Regression metrics
mse_metric = torchmetrics.MeanSquaredError()
mae_metric = torchmetrics.MeanAbsoluteError()

def  test_model():
    input_vector = torch.rand(size=(1,10), requires_grad=True)
    prediction = loaded_model(input_vector)
    target_result = target
    mse = mse_metric(prediction, target_result)
    mae = mae_metric(prediction, target_result)
    print(f"Prediction: {prediction[0][0]}, Target: {target_result[0][0]}")
    print(f"MSE: {mse}, MAE: {mae}")
    print(f"="*40)

for i in  range(10):
    test_model()        

NOTE:?

Of course, you will not get the output exactly as 5 but values closer to 5 in most of the cases. In order to improve the accuracy of the output, the model requires to be trained with a considerable number of inputs. In reality, the neural networks will have very many layers and the size of the dataset will be huge. But, I hope that you would have got an idea about the "magic" behind the AI models.

Great insights, Ganesh! Simplification of complex AI concepts is truly commendable and makes the subject much more approachable.

要查看或添加评论,请登录

Ganesan Krishnamurthy的更多文章

  • Top-down or Bottom-up Approach To Learn AI?

    Top-down or Bottom-up Approach To Learn AI?

    What is the better way to learn AI technologies?,,, Bottom-up or Top-down learning approach?,,, By Bottom-up learning…

  • Accuracy is not that accurate, and Precision is not that precise,,,

    Accuracy is not that accurate, and Precision is not that precise,,,

    Evaluating the solution of the AI model for regression is straight-forward,,, There are measures such as "Mean Square…

  • AI and Admission Problem,,,

    AI and Admission Problem,,,

    Last week, We (My kid, My wife and I) were in a modest sized conf room of a school, sitting across three sombre looking…

  • Back To Square One,,,

    Back To Square One,,,

    Should we be the savants of Maths to understand LLMs and their basic architecture such as transformer model? No, They…

其他会员也浏览了