Package: neograd
NeogradA Deep Learning framework created from scratch with Python and NumPyGet startedInstallationpip install neogradPyPIhttps://pypi.org/project/neograd/Documentationhttps://neograd.readthedocs.io/Explore on Colabhttps://colab.research.google.com/drive/1D4JgBwKgnNQ8Q5DpninB6rdFUidRbjwM?usp=sharinghttps://colab.research.google.com/drive/184916aB5alIyM_xCa0qWnZAL35fDa43L?usp=sharingMotivationI firmly believe that in order to understand something completely, you have to build it on your own from scratch. I used to do gradient calculation analytically, and thought that autograd was some kind of magic. So this was initially built to understand autograd but later on its scope was extended. You might be wondering, there are already many frameworks like TensorFlow and PyTorch that are very popular, and why did I have to create another one? The answer is that these have very complex codebases that are difficult to grasp. So I intend that this repository be used as an educational tool in order to understand how things work under the hood in these giant frameworks, with code that is intuitive and easily readable.FeaturesAutomatic Differentiationautogradoffers automatic differentiation, implemented for the most commonly required operations for vectors of any dimension, with broadcasting capabilitiesimportneogradasnga=ng.tensor(3,requires_grad=True)b=ng.tensor([1,2,3],requires_grad=True)c=a+bc.backward([1,1,1])print(a.grad)print(b.grad)Custom autograd operationsIf you wanted a custom operation to haveautogradcapabilities, those can be defined with very simple interface each having a forward method and a backward methodclassCustom(Operation):defforward(self):passdefbackward(self):passGradient CheckingDebug your models/functions with Gradient Checking, to ensure that the gradients are getting propagated correctlyHighly customizableCreate your own custom layers, optimizers, loss functions which provides more flexibility to create anything you
desirePyTorch like APIPyTorch's API is one of the best and one the most elegant API designs, so we've leveraged the sameNeural Network modulenncontains some of the most commonly used optimizers, activations and loss functions required to train a Neural NetworkSave and Load weights, modelTrained a model already? Then save the weights onto a file and load them whenever required or save the entire model, onto a fileCheckpointsLet's say you're training a model and your computer runs out of juice and if you'd waited until training was finished, to save the weights, then you'd lose all the weights. To prevent this, checkpoint your model with various sessions to save the weights during regular intervals with additional supporting dataExampleimportneogradasngfromneogradimportnnimportnumpyasnpfromneograd.nn.lossimportBCEfromneograd.nn.optimimportAdamfromneograd.autograd.utilsimportgrad_checkfromsklearn.datasetsimportmake_circlesfromsklearn.model_selectionimporttrain_test_splitfromsklearn.metricsimportclassification_report,accuracy_score# load dataset (binary classification problem)X,y=make_circles(n_samples=1000,noise=0.05,random_state=100)X_train,X_test,y_train,y_test=train_test_split(X,y)num_train=750# number of train examplesnum_test=250# number of test examplesnum_iter=50# number of training iterations# convert data into tensorsX_train,X_test=ng.tensor(X_train[:num_train,:]),ng.tensor(X_test[:num_test,:])y_train,y_test=ng.tensor(y_train[:num_train].reshape(num_train,1)),ng.tensor(y_test[:num_test].reshape(num_test,1))# define the structure of your neural netclassNN(nn.Model):def__init__(self):self.stack=nn.Sequential(nn.Linear(2,100),nn.ReLU(),nn.Linear(100,1),nn.Sigmoid())defforward(self,inputs):returnself.stack(inputs)model=NN()# initialize a modelloss_fn=BCE()# initialize a loss function (Binary Cross Entropy)optim=Adam(model.parameters(),0.05)# initialize an optimizer# training loopforiinrange(num_iter):optim.zero_grad()# zero out the gradients in the tensorsoutputs=model(X_train)# get the outputs by passing the training data to your modelloss=loss_fn(outputs,y_train)# calculate the lossloss.backward()# initiate the backward pass to calculate the gradientsoptim.step()# update the parametersprint(f"iter{i+1}/{num_iter}\nloss:{loss}\n")withmodel.eval():# put the model in evaluation modetest_outputs=model(X_test)# get the outputs of the model on test datapreds=np.where(test_outputs.data>=0.5,1,0)# make predictionsprint(classification_report(y_test.data.astype(int).flatten(),preds.flatten()))print(accuracy_score(y_test.data.astype(int).flatten(),preds.flatten()))grad_check(model,X_train,y_train,loss_fn)# perform gradient checking in your modelHow is this any different fromAndrej Karpathy'smicrogradNatively only supports scalar values for computation, whereas we support scalars, vectors, matrices all compatible with NumPy broadcastingGeorge Hotz'stinygradHas an obligation to be under 1000 lines of code leading to cramped up code, therefore our implementation is so much more readable and easily understandable. Also, no dealing with C/C++ code used in tinygrad for GPU accelerationpytorch,tensorflow, etcLarge messy codebases written mostly in C/C++ for efficiency making it impossible to find you're way around and understand stuff. We've a pure Python implementation making it easy to get started and understand what's going on under the hoodResourcesA big thank you to Andrej Karpathy for his CS231nlectureon Backpropagation which was instrumental in helping me gain a good grasp of the basic mechanisms of autogradThanks to Terance Parr and Jeremy Howard for theirpaperThe Matrix Calculus You Need For Deep Learning which helped me get rid of my fear for matrix calculus, that is beautifully written starting from the very fundamentals and slowly transitioning into advanced topics