As one can see from the following example, it is very easy to apply deep learning structure in Pytorch to perform multi-class classificatoin task. The valuation performance of this iris dataset is almost perfect.
import torch.nn as nn import torch #import matplotlib.pyplot as plt import pandas as pd
from sklearn.datasets import load_iris import numpy as np
torch.manual_seed(1121)
<torch._C.Generator at 0x19734b24a70>
load iris dataset for training and valuation
iris = load_iris() X = iris.data Y = iris.target X=pd.DataFrame(X) Y=pd.DataFrame(Y)
display(X) display(Y)
0
1
2
3
0
5.1
3.5
1.4
0.2
1
4.9
3.0
1.4
0.2
2
4.7
3.2
1.3
0.2
3
4.6
3.1
1.5
0.2
4
5.0
3.6
1.4
0.2
...
...
...
...
...
145
6.7
3.0
5.2
2.3
146
6.3
2.5
5.0
1.9
147
6.5
3.0
5.2
2.0
148
6.2
3.4
5.4
2.3
149
5.9
3.0
5.1
1.8
150 rows × 4 columns
0
0
0
1
0
2
0
3
0
4
0
...
...
145
2
146
2
147
2
148
2
149
2
150 rows × 1 columns
train and valuation dataset split
X=X.values Y=Y.values
from sklearn.model_selection import train_test_split x, x_val, y, y_val = train_test_split(X, Y, test_size=0.33, random_state=42)
x.shape, y.shape, x_val.shape, y_val.shape
((100, 4), (100, 1), (50, 4), (50, 1))
notice that: y needs to be one dimension indicating the class type, not encoded vectors
# define batch sizes here batch_size = 16 trainloader=DataLoader(dataset=data_set,batch_size=batch_size)
build softmax classifier
# D_in, dimension of input layer # H , dimension of hidden layer # D_out, dimension of output layer classNet(nn.Module): def__init__(self,D_in,H,D_out): super(Net,self).__init__() self.linear1=nn.Linear(D_in,H) self.linear2=nn.Linear(H,D_out)
defforward(self,x): x=torch.sigmoid(self.linear1(x)) x=self.linear2(x) return x
input_dim=4# how many features are in the dataset or how many input nodes in the input layer hidden_dim = 20# hidden layer size output_dim=3# number of classes print(input_dim,hidden_dim,output_dim)
# Instantiate model model=Net(input_dim,hidden_dim,output_dim)
#n_epochs for epoch inrange(n_epochs): if epoch %100==0: print(epoch) for x, y in trainloader: #clear gradient optimizer.zero_grad() #make a prediction z=model(x) #print(z) # calculate loss, da Cross Entropy benutzt wird muss ich in den loss Klassen vorhersagen, # also Wahrscheinlichkeit pro Klasse. Das mach torch.max(y,1)[1]) loss=criterion(z,y) # calculate gradients of parameters loss.backward() # update parameters optimizer.step() loss_list.append(loss.data) #print('epoch {}, loss {}'.format(epoch, loss.item()))
0
100
200
300
400
500
600
700
800
900
check performance using the valuation data
# predict the class and give probablity for each class label x_val = torch.from_numpy(x_val) z=model(x_val) display(z)
Reprint policy:
All articles in this blog are used except for special statements
CC BY 4.0
reprint policy. If reproduced, please indicate source
robot learner
!