Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize softmax regression in Pytorch

Shulou Source: shulou.com Published: 2022-06-02 05:38:36 10月03日 Update

This article is to share with you about how to achieve softmax regression in Pytorch, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

If you build the network in a custom way:

Import torchimport torchvisionimport torchvision.transforms as transformsimport matplotlib.pyplot as pltimport timeimport sysimport numpy as npimport requests# set up training set and test set Download locally mnist_train = torchvision.datasets.FashionMNIST (root='~/Datasets/FashionMNIST', train=True, download=True, transform=transforms.ToTensor ()) mnist_test = torchvision.datasets.FashionMNIST (root='~/Datasets/FashionMNIST', train=False, download=True, transform=transforms.ToTensor ()) batch_size = 256if sys.platform.startswith ('win'): num_workers = 0 # 0 means no additional processes are needed to speed up reading data else: num_workers = 4train_iter = torch.utils.data.DataLoader (mnist_train Batch_size=batch_size, shuffle=True, num_workers=num_workers) test_iter = torch.utils.data.DataLoader (mnist_test, batch_size=batch_size, shuffle=False, num_workers=num_workers) num_inputs=784# picture is 1 "28" 28 Single channel, so the final category input as 28*28=784num_outputs=10# is 10 categories, so the output is 10 W=torch.tensor (np.random.normal (0jing0.01, (num_inputs,num_outputs), dtype=torch.float) b=torch.zeros (num_outputs,dtype=torch.float) # makes it possible for ww=torch.tensor b to backpropagate. Grad _ (requires_grad=True) b. Def cross_entropy (y_hat, y): return-torch.log (y_hat.gather (1, y.view (- 1,1)) def accuracy (y_hat) Y): return (y_hat.argmax (dim=1) = = y). Float (). Mean (). Item ()''here pay attention to the writing of the correct rate calculation For y_hat, a matrix of batch_size*10 is returned, and argemax (dim=1) is an index that selects the maximum number in a dimension; so y_hat.argmax (dim=1) returns a vector of batch_size*1 Compare the returned vector with y, and average the value. You can get the correct rate of the batch''def sgd (params, lr, batch_size): # this function has been saved in the d2lzh_pytorch package to facilitate future use of for param in params: param.data-= lr * param.grad / batch_size # Note the param.datadef evalute_accuracy (data_iter,net): acc_sum,n=0.0,0 for X used when changing param here Y in data_iter: acc_sum+= (net (X) .argmax (dim=1) = = y) .float () .sum () .item () n+=y.shape [0] return acc_sum/nnum_epochs,lr=30,0.1def softmax (X): X_exp = X.exp () partition = X_exp.sum (dim=1) Keepdim=True) return X_exp / partition # broadcast mechanism def net (X): return softmax (torch.mm (X.view ((- 1, num_inputs)), w) + b) def train (net,train_iter,test_iter,loss,num_epochs,batch_size,params=None,lr=None,optimizer=None): for epoch in range (num_epochs): train_l_sum,train_acc_sum,n=0.0,0.0,0 for X Y in train_iter: y_hat=net (X) l=loss (y_hat Y) .sum () if optimizer is not None: optimizer.zero_grad () elif params is not None and params [0] .grad is not None: for param in params: param.grad.data.zero_ () l.backward () if optimizer is None: sgd (params, lr Batch_size) else: optimizer.step () # the section "concise implementation of softmax regression" will use train_l_sum+=l.item () train_acc_sum+= (y_hat.argmax (dim=1) = = y). Sum (). Item () n+=y.shape [0] test_acc=evalute_accuracy (test_iter,net) Print ('epoch% d, loss% .4f, train acc% .3f, test acc% .3f'% (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc)) if _ name__ ='_ main__': print (mnist_train) X, y = [] []''for i in range (10): X.append (mnist_ [I] [0]) y.append (mnist_ [I] [1]) show_fashion_mnist (X, get_fashion_mnist_labels (y))' 'train (net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [w, b], lr)

If you use a typical layer-building function, you can have a more concise implementation version:

Import torchimport torchvisionimport torchvision.transforms as transformsfrom torchimport nnfrom collections import OrderedDictimport matplotlib.pyplot as pltimport timeimport sysimport numpy as npimport requests# set up training set and test set Download locally from torch.nn import initmnist_train = torchvision.datasets.FashionMNIST (root='~/Datasets/FashionMNIST', train=True, download=True, transform=transforms.ToTensor ()) mnist_test = torchvision.datasets.FashionMNIST (root='~/Datasets/FashionMNIST', train=False, download=True, transform=transforms.ToTensor ()) batch_size= 256train_iter = torch.utils.data.DataLoader (mnist_train, batch_size=batch_size, shuffle=True) test_iter = torch.utils.data.DataLoader (mnist_test, batch_size=batch_size) Shuffle=False) num_inputs=784# picture is 1 "28" 28 Single channel, so the final category input as 28*28=784num_outputs=10# is 10 categories, so the output is 10 Class LinearNet (nn.Module): def _ init__ (self,num_inputs,num_outputs): super (LinearNet,self). _ _ init__ () self.linear=nn.Linear (num_inputs,num_outputs) def forward (self,x): y=self.linear (x.view (x.shape [0],-1)) return ydef evalute_accuracy (data_iter,net): acc_sum,n=0.0,0 for X Y in data_iter: acc_sum+= (net (X) .argmax (dim=1) = = y). Float (). Sum () .item () n+=y.shape [0] return acc_sum/nclass FlattenLayer (nn.Module): def _ init__ (self): super (FlattenLayer, self). _ init__ () def forward (self,x): return x.view (x.shape [0]) -1) # convert 1" 28028 to 1*784net=LinearNet (num_inputs,num_outputs) # use orderdict to build the network structure # the first layer flatten converts 1" 28" 28 to 1" 78 "the second layer is the actual working layer net=nn.Sequential (OrderedDict ([('flatten',FlattenLayer (), (' linear',nn.Linear (num_inputs))" )) init.normal_ (net.linear.weight, mean=0, std=0.01) init.constant_ (net.linear.bias, val=0) loss=nn.CrossEntropyLoss () optimizer=torch.optim.SGD (net.parameters (), lr=0.1) num_epochs=10def train (net,train_iter,test_iter,loss,num_epochs,batch_size,params=None,lr=None,optimizer=None): for epoch in range (num_epochs): train_l_sum,train_acc_sum In train_iter: y_hat=net (X) l=loss (y_hat) Y) .sum () if optimizer is not None: optimizer.zero_grad () elif params is not None and params [0] .grad is not None: for param in params: param.grad.data.zero_ () l.backward () optimizer.step () # "simplicity of softmax regression The section "implementation" will use train_l_sum+=l.item () train_acc_sum+= (y_hat.argmax (dim=1) = = y). Sum (). Item () n+=y.shape [0] test_acc=evalute_accuracy (test_iter Net) Print ('epoch% d, loss% .4f, train acc% .3f, test acc% .3f'% (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc)) if _ name__ ='_ main__': print (mnist_train) X, y = [] []''for i in range (10): X.append (mnist_ [I] [0]) y.append (mnist_ [I] [1]) show_fashion_mnist (X, get_fashion_mnist_labels (y))' 'train (net, train_iter, test_iter, loss, num_epochs, batch_size,None,None,optimizer) above is how to achieve softmax regression in Pytorch The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Category simplicity function vector picture way more accuracy knowledge article network single channel work test training input output utility network structure no Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Shulou Tech Info MariaDB Apple