淺談Keras的Sequential與PyTorch的Sequential的區(qū)別
深度學(xué)習(xí)庫Keras中的Sequential是多個網(wǎng)絡(luò)層的線性堆疊,在實現(xiàn)AlexNet與VGG等網(wǎng)絡(luò)方面比較容易,因為它們沒有ResNet那樣的shortcut連接。在Keras中要實現(xiàn)ResNet網(wǎng)絡(luò)則需要Model模型。
下面是Keras的Sequential具體示例:
可以通過向Sequential模型傳遞一個layer的list來構(gòu)造該模型:
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential([
Dense(32, input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
也可以通過.add()方法一個個的將layer加入模型中:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
Keras可以通過泛型模型(Model)實現(xiàn)復(fù)雜的網(wǎng)絡(luò),如ResNet,Inception等,具體示例如下:
from keras.layers import Input, Dense from keras.models import Model # this returns a tensor inputs = Input(shape=(784,)) # a layer instance is callable on a tensor, and returns a tensor x = Dense(64, activation='relu')(inputs) x = Dense(64, activation='relu')(x) predictions = Dense(10, activation='softmax')(x) # this creates a model that includes # the Input layer and three Dense layers model = Model(input=inputs, output=predictions) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(data, labels) # starts training
在目前的PyTorch版本中,可以僅通過Sequential實現(xiàn)線性模型和復(fù)雜的網(wǎng)絡(luò)模型。PyTorch中的Sequential具體示例如下:
model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), )
也可以通過.add_module()方法一個個的將layer加入模型中:
layer1 = nn.Sequential()
layer1.add_module('conv1', nn.Conv2d(3, 32, 3, 1, padding=1))
layer1.add_module('relu1', nn.ReLU(True))
layer1.add_module('pool1', nn.MaxPool2d(2, 2))
由上可以看出,PyTorch創(chuàng)建網(wǎng)絡(luò)的方法與Keras類似,PyTorch借鑒了Keras的一些優(yōu)點。
以上這篇淺談Keras的Sequential與PyTorch的Sequential的區(qū)別就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python shell根據(jù)ip獲取主機(jī)名代碼示例
這篇文章主要介紹了python shell根據(jù)ip獲取主機(jī)名代碼示例,涉及用socket模塊和shell中hostname命令獲取等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。2017-11-11
Python文本情感分類識別基于SVM算法Django框架實現(xiàn)
這篇文章主要為大家介紹了Python文本情感分類識別基于SVM算法Django框架實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Python循環(huán)緩沖區(qū)的應(yīng)用詳解
循環(huán)緩沖區(qū)是一個線性緩沖區(qū),邏輯上被視為一個循環(huán)的結(jié)構(gòu),本文主要為大家介紹了Python中循環(huán)緩沖區(qū)的相關(guān)應(yīng)用,有興趣的小伙伴可以了解一下2025-03-03
Python?Paramiko上傳下載sftp文件及遠(yuǎn)程執(zhí)行命令詳解
這篇文章主要為大家介紹了Python?Paramiko上傳下載sftp文件及遠(yuǎn)程執(zhí)行命令示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

