Package: import-ai
import aiAI library in python using numpy, with end-to-end reverse auto-differentiable dynamic Computational Graph. Implements general Deep Learning library components with the end API similar to that of my favourite, Pytorch.AboutBegineers in Deep Learning will find this repo useful. The purpose of this library is to serve as an educational tool, a reference guide to better understand the mechanics of deep concepts of AI by implementing everything from scratch. I want to expose the functions of Deep Learning APIs as clearly as possible. I originally built this for myself to understand Deep Learning critically, whose importance is pointed by one of favourite AI researchersAndrej Karpath, inthisvideo.So, as you have guessed, the best way to utilise this library is by implementing your own from scratch. Refer to this library when you don't understand how a Deep Learning component is built, tweak it and have fun :)FeaturesThis library implements:aParameterobject - that holds the weights and gradients(wrt to a scalar quantity)aComputational Graph- to store operations during forward pass, as a bfs walk in Directed Graph and execute them in reverse order during backprop. This has all the necessary functions to help realise many layers to do deep learning(activations, regularizers etc.)Layers/models - the fundamentalLinearlayer,LSTM and RNN,Conv2d and ConvTranspose2d,BatchNormand a genericModeltemplate for util functionsLoss- Mean Square, Cross Entropy, BCELoss and few other loss functions.Optimizers- basic SGD, Adam, Adagrad and Adadelta optimizer functions with choice of momentum.Vsisualization tool to draw the computational graph of any program you write.some example implementations using this libraryI will keep updating the library with more explanations, documentation and a similar library in my favourite language, c++ soon!InstallationThis library requires Python>=3.6 and numpy. Install the library as below(Installation takes care of the dependencies):pipinstallimport-aior you could just clone this repo and work locally as below:gitclonehttps://github.com/srirambandi/ai.git
pipinstall-rrequirements.txtUsageYou can directly work with Parameter objects, ComputationGraph and have fun!(The graph engine takes care of the reverse-mode auto-differentiation - the backpropagation algorithm. It is of highest importance that you actually understand how these internal mechanics work together, that's the foremost intended purpose of this library.)import and initiate>>>importai>>>x=ai.Parameter((3,1),eval_grad=False)>>>W=ai.Parameter((3,3))>>>b=ai.Parameter((3,1),init_zeros=True)>>>print(W)Parameter(shape=(3,3),eval_grad=True)containing:Data:[[-0.010924950.00542457-0.00562512][0.00911396-0.00143499-0.0160998][-0.016010840.011469770.00797995]]do operations>>>y=(W@x)+b# supports basic arithmetic>>>print(y)Parameter(shape=(3,1),eval_grad=True)containing:Data:[[-0.00011536][0.00012833][-0.00023106]]backward>>>y.grad[1,0]=1.0>>>y.backward()>>>print(W.grad)array([[0.,0.,0.],[0.00873683,-0.00623124,-0.00246939],[0.,0.,0.]])see the Computational Graph for the above program>>>ai.draw_graph(filename='linear')Parameters(single circles) interact with functions(double circles) and output Parameters. The values in the circles of parameters are the node ids indexed with the bfs-walk of graph during forward pass, goes from lowest node id circle to highest node id circle. The backward pass is the same graph with edges reversed, goes from highest node id circle to lowest node id circle. The circles withNonedoesn't have any backward operations attached to them. The circes with red line doesn't need gradients(inputs, outputs, constants). Also, checkout some other nice renderings in the assets folder.Or a more schematic code to use in Deep Learning projects as below.importaiai.manual_seed(2357)defdata_generator(file):yielddata_batchclassNet(ai.Module):def__init__(self):self.conv=ai.Conv2d(3,16,kernel=3,stride=1,padding=0)self.fc=ai.Linear(x,10)defforward(self,x):o1=ai.G.relu(self.conv(x))o2=ai.G.dropout(ai.G.maxpool(o1),p=0.5)o3=self.fc(o2)returnai.G.softmax(o3)model=Net()print(model)L=ai.Loss('CrossEntropyLoss')optim=ai.Optimizer(model.parameters(),optim_fn='Adam',lr=1e-3)# inferencedefevaluate():# testing and inferenceai.G.grad_mode=Falsepredicttion=model.forward(test_input)ai.G.grad_mode=True# some control parameters# training loopwhilenotconverged:# get scores and compute gradientsscores=model.forward(train_input)loss=L.loss(scores,outputs)loss.backward()# logging info# update weightsoptim.step()optim.zero_grad()model.save()ImplementationsExamples directory contains some basic popular Deep Learning implementations, and I will add challenging ones soon.Other examples using this library, resting in their stand-alone repos are:GAN/Wasserstein-GAN ImplementationsNeural Turing Machines Implementation"Deep Learning for Symbolic Mathematics" - paper implementation- Work In ProgressGoalsTo implement other learning paradigms besides Supervised such as, Unsupervised and Reinforcement Learning algorithms into the library.I want to implement every model in the below Deep Learning Toolkit picturesource tweetLicenseMITAuthorSri Ram Bandi /@_srirambandi_