十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
如何進(jìn)行torchkeras的原理分析,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供天橋企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為天橋眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
torchkeras 是在pytorch上實(shí)現(xiàn)的仿keras的高層次Model接口。有了它,你可以像Keras那樣,對(duì)pytorch構(gòu)建的模型進(jìn)行summary,compile,fit,evaluate , predict五連擊。一切都像行云流水般自然。
聽(tīng)起來(lái),torchkeras的功能非常強(qiáng)大。但實(shí)際上,它的實(shí)現(xiàn)非常簡(jiǎn)單,全部源代碼不足300行。如果你想理解它實(shí)現(xiàn)原理的一些細(xì)節(jié),或者修改它的功能,不要猶豫閱讀和修改項(xiàng)目源碼。
安裝它僅需要運(yùn)行:
pip install torchkeras
公眾號(hào)后臺(tái)回復(fù)關(guān)鍵詞:torchkeras。獲取項(xiàng)目git源代碼和本文全部源碼!
下面是一個(gè)使用torchkeras來(lái)訓(xùn)練模型的完整范例。我們?cè)O(shè)計(jì)了一個(gè)3層的神經(jīng)網(wǎng)絡(luò)來(lái)解決一個(gè)正負(fù)樣本按照同心圓分布的分類(lèi)問(wèn)題。
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader,TensorDataset
from torchkeras import Model,summary #Attention this line!構(gòu)造按照同心圓分布的正負(fù)樣本數(shù)據(jù)。
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
#number of samples
n_positive,n_negative = 2000,2000
#positive samples
r_p = 5.0 + torch.normal(0.0,1.0,size = [n_positive,1])
theta_p = 2*np.pi*torch.rand([n_positive,1])
Xp = torch.cat([r_p*torch.cos(theta_p),r_p*torch.sin(theta_p)],axis = 1)
Yp = torch.ones_like(r_p)
#negative samples
r_n = 8.0 + torch.normal(0.0,1.0,size = [n_negative,1])
theta_n = 2*np.pi*torch.rand([n_negative,1])
Xn = torch.cat([r_n*torch.cos(theta_n),r_n*torch.sin(theta_n)],axis = 1)
Yn = torch.zeros_like(r_n)
#concat positive and negative samples
X = torch.cat([Xp,Xn],axis = 0)
Y = torch.cat([Yp,Yn],axis = 0)
#visual samples
plt.figure(figsize = (6,6))
plt.scatter(Xp[:,0],Xp[:,1],c = "r")
plt.scatter(Xn[:,0],Xn[:,1],c = "g")
plt.legend(["positive","negative"]);

# split samples into train and valid data.
ds = TensorDataset(X,Y)
ds_train,ds_valid = torch.utils.data.random_split(ds,[int(len(ds)*0.7),len(ds)-int(len(ds)*0.7)])
dl_train = DataLoader(ds_train,batch_size = 100,shuffle=True,num_workers=2)
dl_valid = DataLoader(ds_valid,batch_size = 100,num_workers=2) 我們通過(guò)對(duì)torchkeras.Model進(jìn)行子類(lèi)化來(lái)構(gòu)建模型,而不是對(duì)torch.nn.Module的子類(lèi)化來(lái)構(gòu)建模型。實(shí)際上 torchkeras.Model是torch.nn.Moduled的子類(lèi)。
class DNNModel(Model): ### Attention here
def __init__(self):
super(DNNModel, self).__init__()
self.fc1 = nn.Linear(2,4)
self.fc2 = nn.Linear(4,8)
self.fc3 = nn.Linear(8,1)
def forward(self,x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
y = nn.Sigmoid()(self.fc3(x))
return y
model = DNNModel()
model.summary(input_shape =(2,))

我們需要先用compile將損失函數(shù),優(yōu)化器以及評(píng)估指標(biāo)和模型綁定。然后就可以用fit方法進(jìn)行模型訓(xùn)練了。
關(guān)于如何進(jìn)行torchkeras的原理分析問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。