Package: xshinnosuke
XShinnosuke : Deep Learning FrameworkDescriptionsXShinnosuke(short asXS) is a high-level neural network framework which supports for bothDynamic GraphandStatic Graph, and has almost the same API toKerasandPytorchwithslightly differences.It was written by Python only, and dedicated to realize experimentations quickly.Here are some features of XS:Based onCupy(GPU version)/Numpyandnativeto Python.Withoutany other3rd-partydeep learning library.Keras and Pytorch style API, easy to start up.Supports commonly used layers such as:Dense, Conv2D, MaxPooling2D, LSTM, SimpleRNN, etc, and commonly used function:conv2d, max_pool2d, relu, etc.Sequentialin Pytorch and Keras,Modelin Keras andModulein Pytorch,all of them are supportedby XS.Training and inference supports for bothdynamic graphandstatic graph.Autogradis supported .XS is compatible with:Python 3.x (3.7 is recommended)==> C++ version1. API docs2. NotebookGetting startedCompared with Pytorch and KerasResNet18(5 Epochs, 32 Batch_size)XS_static_graph(cpu)XS_dynamic_graph(cpu)Pytorch(cpu)Keras(cpu)Speed(Ratio - seconds)1x-65.050.98x- 66.332.67x- 24.391.8x- 35.97Memory(Ratio - GB)1x-0.470.47x- 0.220.55x- 0.260.96x- 0.45ResNet18(5 Epochs, 32 Batch_size)XS_static_graph(gpu)XS_dynamic_graph(gpu)Pytorch(gpu)Keras(gpu)Speed(Ratio - seconds)1x-9.641.02x- 9.453.47x- 2.781.07x- 9.04Memory(Ratio - GB)1x-0.481.02x- 0.494.4x- 2.114.21x- 2.02XS holds the best memory usage!1. Static GraphThe core networks of XS is a model, which provide a way to combine layers. There are two model types:Sequential(a linear stack of layers) andFunctional(build  a graph for layers).ForSequentialmodel:fromxs.nn.modelsimportSequentialmodel=Sequential()Using.add()to connect layers:fromxs.layersimportDensemodel.add(Dense(out_features=500,activation='relu',input_shape=(784,)))# must be specify input_shape if current layer is the first layer of modelmodel.add(Dense(out_features=10))Once you have constructed your model, you should configure it with.compile()before training or inference:model.compile(loss='cross_entropy',optimizer='sgd')If your labels areone-hotencoded vectors/matrix, you shall specify loss assparse_crossentropy, otherwise usecrossentropyinstead.Useprint(model)to see details of model:***************************************************************************Layer(type)OutputShapeParamConnectedto###########################################################################dense0(Dense)(None,500)392500---------------------------------------------------------------------------dense1(Dense)(None,10)5010dense0---------------------------------------------------------------------------***************************************************************************Totalparams:397510Trainableparams:397510Non-trainableparams:0Start training your network byfit():# trainX and trainy are ndarrayshistory=model.fit(trainX,trainy,batch_size=128,epochs=5)Once completing training your model, you can save or load your model bysave()/load(), respectively.model.save(save_path)model.load(model_path)Evaluate your model performance byevaluate():# testX and testy are Cupy/Numpy ndarrayaccuracy,loss=model.evaluate(testX,testy,batch_size=128)Inference throughpredict():predict=model.predict(testX)ForFunctionalmodel:fromxs.nn.modelsimportModelfromxs.layersimportInput,Conv2D,MaxPooling2D,Flatten,DenseX_input=Input(input_shape=(1,28,28))# (channels, height, width)X=Conv2D(8,(2,2),activation='relu')(X_input)X=MaxPooling2D((2,2))(X)X=Flatten()(X)X=Dense(10)(X)model=Model(inputs=X_input,outputs=X)model.compile(optimizer='sgd',loss='cross_entropy')model.fit(trainX,trainy,batch_size=256,epochs=80)Pass inputs and outputs layer toModel(), thencompileandfitmodel asSequentialmodel.2. Dynamic GraphFirst design your own network, make sure your network is inherited fromModuleandoverridethe__init__()andforward()function:fromxs.nn.modelsimportModulefromxs.layersimportConv2D,ReLU,Flatten,Denseimportxs.nn.functionalasFclassMyNet(Module):def__init__(self):super().__init__()self.conv1=Conv2D(out_channels=8,kernel_size=3)# don't need to specify in_channels, which is simple than Pytorchself.relu=ReLU(inplace=True)self.flat=Flatten()self.fc=Dense(10)defforward(self,x,*args):x=self.conv1(x)x=self.relu(x)x=F.max_pool2d(x,kernel_size=2)x=self.flat(x)x=self.fc(x)returnxThen manually set the training/ testing flow:fromxs.nn.optimizersimportSGDfromxs.utils.dataimportDataSet,DataLoaderimportxs.nnasnnimportnumpyasnp# random generate dataX=np.random.randn(100,3,12,12)Y=np.random.randint(0,10,(100,))# generate training dataloadertrain_dataset=DataSet(X,Y)train_loader=DataLoader(dataset=train_dataset,batch_size=10,shuffle=True)# initialize netnet=MyNet()# specify optimizer and critetionoptimizer=SGD(net.parameters(),lr=0.1)critetion=nn.CrossEntropyLoss()# start trainingEPOCH=5forepochinrange(EPOCH):forx,yintrain_loader:optimizer.zero_grad()out=net(x)loss=critetion(out,y)loss.backward()optimizer.step()train_acc=critetion.calc_acc(out,y)print(f'epoch ->{epoch}, train_acc:{train_acc}, train_loss:{loss.item()}')Building an image classification model, a question answering system or any other model is just as convenient and fast~AutogradXS autograd supports for basic operators such as:+, -, *, \, **, etcand some common functions:matmul(), mean(), sum(), log(), view(), etc.fromxs.nnimportTensora=Tensor(5,requires_grad=True)b=Tensor(10,requires_grad=True)c=Tensor(3,requires_grad=True)x=(a+b)*cy=x**2print('x: ',x)# x:  Variable(45.0, requires_grad=True, grad_fn=<MultiplyBackward>)print('y: ',y)# y:  Variable(2025.0, requires_grad=True, grad_fn=<PowBackward>)x.retain_grad()y.backward()print('x grad:',x.grad)# x grad: 90.0print('c grad:',c.grad)# c grad: 1350.0print('b grad:',b.grad)# b grad: 270.0print('a grad:',a.grad)# a grad: 270.0Here are examples ofautograd.InstallationBefore installing XS, please install the followingdependencies:NumpyCupy (Optional)Then you can install XS by using pip:$ pip install xshinnosukeSupportsfunctionaladmmmmreluflattenconv2dmax_pool2davg_pool2dreshapesigmoidtanhsoftmaxdropout2dbatch_normgroupnorm2dlayernormpad_2dembeddingTwo basic class:- Layer:DenseFlattenConv2DMaxPooling2DAvgPooling2DChannelMaxPoolingChannelAvgPoolingActivationInputDropoutBatchNormalizationLayerNormalizationGroupNormalizationTimeDistributedSimpleRNNLSTMEmbeddingZeroPadding2DAddMultiplyMatmulLogNegativeExpSumAbsMeanPow- Tenosr:ParameterOptimizersSGDMomentumRMSpropAdaGradAdaDeltaAdamWaiting for implemented moreObjectivesMSELossMAELossBCELossSparseCrossEntropyCrossEntropyLossActivationsReLUSigmoidTanhSoftmaxInitializationsZerosOnesUniformLecunUniformGlorotUniformHeUniformNormalLecunNormalGlorotNormalHeNormalOrthogonalRegularizeswaiting for implement.Preprocessto_categorical (convert inputs to one-hot vector/matrix)pad_sequences (pad sequences to the same length)ContactEmail:eleven_1111@outlook.com