Logistic regression is used to classify iris
- The code uses root mean square error
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch as t
from torch.autograd import Variable as var
file_path = "pyTorchTry/DNN/iris.csv"
df_iris = pd.read_csv(file_path, sep=",", header="infer")
np_iris = df_iris.values
np.random.shuffle(np_iris)
def normalize(temp) :
temp = 2*(temp - np.mean(temp,axis = 0))/(np.max(temp,axis = 0)-np.min(temp,axis = 0))
return(temp)
def convert2onehot(data) :
# covert data to onehot representation
return pd.get_dummies(data)
xs = normalize(np_iris[:,1:5]).astype(np.double)
ys = convert2onehot(np_iris[:,-1]).values
xs = var(t.Tensor(xs))
ys = var(t.Tensor(ys))
class softmax_model(t.nn.Module) :
def __init__(self) :
super(softmax_model,self).__init__()
self.linear1 = t.nn.Linear(4.64)
self.relu = t.nn.ReLU()
self.linear2 = t.nn.Linear(64.16)
self.linear3 = t.nn.Linear(16.3)
self.softmax = t.nn.Softmax()
self.criterion = t.nn.MSELoss()
self.opt = t.optim.SGD(self.parameters(),lr=0.6)
def forward(self, input) :
y = self.linear1(input)
y = self.relu(y)
y = self.linear2(y)
y = self.relu(y)
y = self.linear3(y)
y = self.softmax(y)
return y
model = softmax_model()
for e in range(6001):
y_pre = model(xs[:90,:])
loss = model.criterion(y_pre,ys[:90,:])
if(e%200= =0) :print(e,loss.data)
# Zero gradients
model.opt.zero_grad()
# perform backward pass
loss.backward()
# update weights
model.opt.step()
result = (np.argmax(model(xs[90:,:]).data.numpy(),axis=1) == np.argmax(ys[90:,:].data.numpy(),axis=1))
print(np.sum(result)/60)
Copy the code
- The results of the log
Tensor 0 tensor (0.2260) 600 (0.0052) 1200 tensor tensor (0.0019) 1800 (0.0009) 2400 tensor tensor (0.0005) 3000 (0.0004) in 3600 Tensor (0.0003) 4200 tensor(0.0002) 4800 tensor(0.0002) 5400 tensor(0.0001) 6000 tensor(0.0001) 0.966666666667Copy the code
- Using cross entropy
class softmax_model(t.nn.Module) :
def __init__(self) :
super(softmax_model,self).__init__()
self.linear1 = t.nn.Linear(4.64)
self.relu = t.nn.ReLU()
self.linear2 = t.nn.Linear(64.16)
self.linear3 = t.nn.Linear(16.3)
self.softmax = t.nn.functional.log_softmax
self.criterion = t.nn.MSELoss()
self.opt = t.optim.SGD(self.parameters(),lr=0.8)
def forward(self, input) :
y = self.linear1(input)
y = self.relu(y)
y = self.linear2(y)
y = self.relu(y)
y = self.linear3(y)
y = self.softmax(y,dim=1)
return y
Copy the code