pytorch白話入門筆記1 6 快速搭建神經網路

2021-10-04 12:43:55 字數 1765 閱讀 4528

目錄

1.快速搭建神經網路

(1)**

(2)執行結果

import torch

from torch.autograd import variable

import torch.nn.functional as f

import matplotlib.pyplot as plt

n_data= torch.ones(100,2)

x0 = torch.normal(2*n_data,1)

y0 = torch.zeros(100)

x1 = torch.normal(-2*n_data,1)

y1 = torch.ones(100)

x = torch.cat((x0,x1),0).type(torch.floattensor)

y = torch.cat((y0,y1),0).type(torch.longtensor)

x,y = variable(x),variable(y)

# plt.scatter(x.data.numpy()[:,0],x.data.numpy()[:,1],c= y.data.numpy(),s=100,lw =0,cmap ='rdylgn')

# plt.show()

# net __init__()

# methord 1

class net(torch.nn.module):#繼承module

def __init__(self,n_features,n_hidden,n_output):

super(net,self).__init__()#官方步驟,繼承

self.hidden = torch.nn.linear(n_features,n_hidden)

self.predict = torch.nn.linear(n_hidden,n_output)#**

def forward(self,x):

# 前向傳遞過程,搭建神經網路

x = f.relu(self.hidden(x))#乙個function

x = self.predict(x)

return x

net = net(2,10,2) #輸入、隱藏層、輸出分別為1,10,1

#哪個位置為1就是其對應分類

#methord 2【新的快速搭建方法】

net2 = torch.nn.sequential(

#一層一層磊神經層

torch.nn.linear(2,10),

torch.nn.relu(),#層的類

torch.nn.linear(10,2),

)print(net)

print(net2)

net(

(hidden): linear(in_features=2, out_features=10, bias=true)

(predict): linear(in_features=10, out_features=2, bias=true)

)sequential(

(0): linear(in_features=2, out_features=10, bias=true)

(1): relu()

(2): linear(in_features=10, out_features=2, bias=true)

)process finished with exit code 0

pytorch白話入門筆記1 8 批資料訓練

目錄 1.批資料訓練 1 2 執行結果 import torch import torch.utils.data as data torch.manual seed 1 reproducible batch size 8 批訓練的資料個數 x torch.linspace 1,10,10 x dat...

PyTorch入門筆記

原教程 資料集csv 此處使用numpy來匯入,除此之外還可以使用csv和pandas匯入 資料集鏈結 import csv import numpy as np wine path data chapter3 winequality white.csv 路徑 wineq numpy np.load...

pytorch入門 函式用法筆記

class torchvision.transforms.compose transforms 將多個transform組合起來使用。可以將以上 中的 torchvision.transforms.compose 類看作一種容器 它能夠同時對多種資料變換進行組合 傳入的引數是乙個列表,列表中的元素就...