当前位置:网站首页>7. Processing the input of multidimensional features

7. Processing the input of multidimensional features

2022-07-05 05:41:00 A big pigeon

Multidimensional input logistic regression

                                          Multi layer network

Complete code :

import torch
import  torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt

# 1. Prepare the data , from csv File read 
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:,[-1]])

# 2. The design model ( class )  Inherit nn.Module  In order to use its method 
class Model(torch.nn.Module):
    #  initialization 
    def __init__(self):
        super(Model, self).__init__()
        #3  Layer neural networks 
        self.linear1 = torch.nn.Linear(8, 6)  # Linear Is a linear unit 
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 1)
        self.sigmoid = torch.nn.Sigmoid()

    #  Feedforward method 
    def forward(self, x):
        # The output of each layer is the input of the lower layer 
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x


model = Model()

# 3 loss  and  optimizer( Optimizer )
criterion = torch.nn.BCELoss(size_average=True)
#  Optimizer . model.parameters() Get the parameters that need to be optimized in the model ,lr(learning rate, Learning rate )
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# 4  Training process 
for epoch in range(100):
    #  feedforward 
    y_pred = model(x_data)
    #  Calculate the loss 
    loss = criterion(y_pred, y_data)
    print("epoch={},loss={}".format(epoch, loss))

    optimizer.zero_grad()  #  Zeroing 
    #  Back propagation 
    loss.backward()
    #  to update 、 Optimization parameters 
    optimizer.step()












原网站

版权声明
本文为[A big pigeon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140620422760.html