Package: beaverpy
beaverpy :beaver:Descriptionv0.1.0beaverpyis an implementation of PyTorch operators using only NumPy.Implemented operators (their PyTorch equivalents) include the following:LayersConv2D(torch.nn.Conv2d)MaxPool2D(torch.nn.MaxPool2d)Linear(torch.nn.Linear)Loss/Distance FunctionsMSELoss(torch.nn.MSELoss)CosineSimilarity(torch.nn.CosineSimilarity)ActivationsReLU(torch.nn.ReLU)Sigmoid(torch.nn.Sigmoid)Softmax(torch.nn.Softmax)Note 1:[n, c, h, w]format is usedNote 2:Test code that checks for correctness of the implementation is included in respective notebooks and is also available as standalonepytestscriptsOptional parameters supportedConv2D—stride,padding,dilation,groupsMaxPool2D—stride,padding,dilation,return_indicesLinear—biasMSELoss—reductionCosineSimilarity—dim,epsSoftmax—dimHow to install?pip3 install beaverpyHow to use?ImportbeaverpyandnumpyimportbeaverpyasbpimportnumpyasnpFollowing is an example to useConv2D:Define input parametersin_channels=6# input channelsout_channels=4# output channelskernel_size=(2,2)# kernel size_stride=(2,1)# stride (optional)_padding=(1,3)# padding (optional)_dilation=(2,3)# dilation factor (optional)_groups=2# groups (optional)in_batches=2# input batchesin_h=4# input heightin_w=4# input weightCreate a random input using the input parameters_input=np.random.rand(in_batches,in_channels,in_h,in_w)Call an instance ofConv2Dwith the input parametersconv2d=bp.Conv2D(in_channels,out_channels,kernel_size,stride=_stride,padding=_padding,dilation=_dilation,groups=_groups)Perform convolution_output=conv2d.forward(_input)# perform convolutionIn case you wish to provide your own kernel, then define the same and pass it as an argument toforward():kernels=[]forkinrange(out_channels):kernel=np.random.rand(int(in_channels/_groups),kernel_size[0],kernel_size[1])# define a random kernel based on the kernel parameterskernels.append(kernel)_output=conv2d.forward(_input,kernels)# perform convolutionFollowing is an example to useMaxPool2D:Define input parametersin_channels=3# input channelskernel_size=(6,6)# kernel size_stride=(1,5)# stride (optional)_padding=(1,2)# padding (optional)_dilation=(2,1)# dilation factor (optional)_return_indices=True# return max indices (optional)in_batches=3# input batchesin_h=11# input heightin_w=8# input weightCreate a random input using the input parameters_input=np.random.rand(in_batches,in_channels,in_h,in_w)Call an instance ofMaxPool2Dwith the input parametersmaxpool2d=bp.MaxPool2D(kernel_size,stride=_stride,padding=_padding,dilation=_dilation,return_indices=_return_indices)Perform maxpooling_output=maxpool2d.forward(_input)Following is an example to useLinear:Define input parametersin_samples=128# input samplesin_features=20# input featuresout_features=30# output featuresCreate a random input using the input parameters_input=np.random.rand(in_samples,in_features)Call an instance ofLinearwith the input parameterslinear=bp.Linear(in_features,out_features)Apply a linear transformation_output=linear.forward(_input)In case you wish to provide your own weights and bias, then define the same and pass them as arguments toforward():_weights=np.random.rand(out_features,in_features)# define random weights_bias=np.random.rand(out_features)# define random bias_output=linear.forward(_input,weights=_weights,bias_weights=_bias)# apply linear transformationFollowing is an example to useMSELoss:Create a random input and targetdimension=np.random.randint(500)# dimension of the input and target_input=np.random.rand(dimension)# define a random input of the above dimension_target=np.random.rand(dimension)# define a random target of the above dimensionCall an instance ofMSELosswith the input parametersmseloss=bp.MSELoss()Compue MSE loss_output=mseloss.forward(_input,_target)Following is an example to useCosineSimilarity:Create random inputnum_dim=np.random.randint(6)+1# number of input dimensionsshape=tuple(np.random.randint(5)+1for_inrange(num_dim))# shape of input_input1=np.random.rand(*shape)# generate an input based on the dimensions and shape_input2=np.random.rand(*shape)# generate another input based on the dimensions and shape_dim=np.random.randint(num_dim)# dimension along which CosineSimilarity is to be computed (optional)_eps=np.random.uniform(low=1e-10,high=1e-6)# (optional)Call an instance ofCosineSimilaritywith the input parameterscosinesimilarity=bp.CosineSimilarity(dim=_dim,eps=_eps)Compue CosineSimilarity_output=cosinesimilarity.forward(_input1,_input2)Following is an example to useReLU:Create a random input_input=np.random.rand(10,20,3)Call an instance ofReLUwith the input parametersrelu=bp.ReLU()Apply ReLU activation_output=relu.forward(_input)Following is an example to useSigmoid:Create a random input_input=np.random.rand(10,20,3)Call an instance ofSigmoidwith the input parameterssigmoid=bp.Sigmoid()Apply Sigmoid activation_output=sigmoid.forward(_input)Following is an example to useSoftmax:Create a random input and dimension_input=np.random.rand(1,2,1,3,4)_dim=np.random.randint(len(_input))# (optional)Call an instance ofSoftmaxwith the input parameterssoftmax=bp.Softmax(dim=_dim)Apply Softmax activation_output=softmax.forward(_input)Future workReplacetorch.round()withnp.allclose()for testsImplement other operatorsOptimize codeFor newer builds of this package that are under development and not yet available on PyPI, please visit theGitHub repositoryAcknowledgementsThis work is being done during my summer internship atDeGirum Corp., Santa Clara.Using this code in your projectsIf you are using this code in your projects, please make sure to cite this repository and the authorIf you find bugs, create a pull request with a description of the bug and the proposed changesDo have a look at theauthor's webpagefor other interesting works!READMElast updated on 06/08/2023