From 48859c4fd478529e55764a455532a3b9d2d5f9a7 Mon Sep 17 00:00:00 2001 From: Shreyansh Pathak <21f1003357@student.onlinedegree.iitm.ac.in> Date: Tue, 27 Feb 2024 11:43:24 +0530 Subject: [PATCH] Update README.md --- README.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3624f94..12c5df5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ neural network built on a scalar engine rather than regular tensor engine ## scalar.py -This is a simple Python class named `Scalar` which represents scalar values and supports basic arithmetic operations and some elementary mathematical functions along with automatic differentiation for gradient computation. +This is a simple Python class named `Scalar` which represents scalar values and helps in implementing backpropagation on small neural networks representing arithmetic expressions. ### Features @@ -13,6 +13,66 @@ This is a simple Python class named `Scalar` which represents scalar values and - Automatic differentiation for gradient computation using backpropagation. - Support for building computational graphs and computing gradients for scalar-valued expressions. +### Comparison between pytorch and Scalar usage + +- Implementing \[ +w_1 \cdot x_1 + w_2 \cdot x_2 + b +\] +where \( w_1 \), \( w_2 \) are weights, \( x_1 \), \( x_2 \) are inputs, and \( b \) is the bias and a backward pass to evaluate gradients in pytorch + +```python +import torch + +x1 = torch.Tensor([2.0]).double() +x2 = torch.Tensor([0.0]).double() +w1 = torch.Tensor([-3.0]).double() +w2 = torch.Tensor([1.0]).double() +b = torch.Tensor([6.8813735870195432]).double() + +x1.requires_grad = True +x2.requires_grad = True +w1.requires_grad = True +w2.requires_grad = True +b.requires_grad = True + +n = x1*w1 + x2*w2 + b +o = torch.tanh(n) + +print('o: ',o.item()) + +o.backward() + +print('x2',x2.grad.item()) +print('x1',x1.grad.item()) +print('w1',w1.grad.item()) +print('w2',w2.grad.item()) +``` +Now implementing the same in Scalar : + +```python +x1 = Scalar(2.0,label='x1') +x2 = Scalar(0.0,label='x2') + +w1 = Scalar(-3.0,label='w1') +w2 = Scalar(1.0,label='w2') + +b = Scalar(6.8813735870195432,label='b') + +x1w1 = x1*w1 +x1w1.label='x1*w1' +x2w2 = x2*w2 +x2w2.label = 'x2*w2' + +x1w1x2w2 = x1w1 + x2w2 +x1w1x2w2.label = 'x1w1+x2w2' + +n = x1w1x2w2 + b +n.label = 'n' +o = n.tanh() +o.label = 'o' +o.backward() +``` + ### Usage You can use the `Scalar` class to perform arithmetic operations, compute mathematical functions, and automatically calculate gradients. Here's a brief overview of how to use it: