Pytorch官方文件(八)翻譯版本

2021-10-02 14:09:17 字數 3940 閱讀 7854

import torch.nn as nn

import torch.nn.functional as

fclass

model

(nn.module)

: def __init__

(self)

:super

(model, self)

.__init__()

self.conv1 = nn.

conv2d(1

,20,5

) self.conv2 = nn.

conv2d(20

,20,5

) def forward

(self, x)

: x =f.

relu

(self.

conv1

(x))

returnf.

relu

(self.

conv2

(x))

例子:

# example of using sequential

model = nn.

sequential

( nn.

conv2d(1

,20,5

),nn.

relu()

, nn.

conv2d(20

,64,5

),nn.

relu()

)# example of using sequential with ordereddict

model = nn.

sequential

(ordereddict([

('conv1'

, nn.

conv2d(1

,20,5

)),(

'relu1'

, nn.

relu()

),('conv2'

, nn.

conv2d(20

,64,5

)),(

'relu2'

, nn.

relu()

)]))

引數:

in_channels,輸入影象的通道數。

out_channels,卷積後的通道數。

kernel_size,卷積核的大小。

stride(可選),卷積操作的步長,預設是1。

padding(可選),在輸入的所有邊填0,預設是0。

padding_mode(可選),零。

dilation(可選),卷積核元素之間的空隙,預設是1。

groups(可選),從輸入通道到輸出通道之間塊連線的數量。

bias(可選),如果值為true,新增乙個可學習的bias到輸出上,預設true。

官方例子:

>>> # with square kernels and equal stride

>>> m = nn.

conv2d(16

,33,3

, stride=2)

>>> # non-square kernels and unequal stride and with padding

>>> m = nn.

conv2d(16

,33,(

3,5)

, stride=(2

,1), padding=(4

,2))

>>> # non-square kernels and unequal stride and with padding and dilation

>>> m = nn.

conv2d(16

,33,(

3,5)

, stride=(2

,1), padding=(4

,2), dilation=(3

,1))

>>> input = torch.

randn(20

,16,50

,100

)>>> output =

m(input)

自己的例子:

(model.py)

import torch.nn as nn

import torch.nn.functional as

fclass

model

(nn.module)

: def __init__

(self)

:super

(model, self)

.__init__()

# conv2d 三個引數:1

)in_channels;2

)out_channels;3

)kernel_size;4

)stride=

1;5)padding=

0 # 6

)dilation=false;7

)groups=

1;8)bias=true

self.conv1 = nn.

conv2d(3

,20,3

) self.conv2 = nn.

conv2d(20

,20,3

) # batchnorm2d

() 引數:channels 作用:正則化防止引數過多

self.normal = nn.

batchnorm2d(20

) def forward

(self, x)

: x = self.

conv1

(x) x = self.

conv2

(x) x == self.

normal

(x) x =f.

relu

(x)return x

(train.py)

import model

import torch

# 初始化乙個model

model = model.

model()

# 初始化乙個4維tensor,1*3

*7*7。

test = torch.

rand(1

,3,7

,7)print

('size before: '

)print

(test.

size()

)# 將原tensor 經過model處理

x =model

(test)

print

('size after: '

)print

(x.size()

)

輸出:

size before: 

torch.

size([

1,3,

7,7]

)size after:

torch.

size([

1,20,

3,3]

)

Core Foundation 官方文件翻譯

core foundation框架中常用的隱含型別 使用這些隱含型別時需要自己初始化,自己去釋放記憶體。所以需要記住,在初始化的同時在相應位置釋放。以防出現記憶體問題。1.cfstringref 其他方法用的時候可以檢視文件 void testcfstringref 2 cfarrayref,還有很...

Docker 官方文件翻譯

docker compose 是利用docker來執行多個容器的工具。利用compose 在乙個檔案中定義多個容器,然後利用乙個單獨的命令,可以執行你所想做的任何事情。compose 能較好的作為開發環境的假設,伺服器腳手架以及ci方面的應用。我們不推薦使用在生產環境中。使用compose 需要一下...

Pytorch官方指南(二) 翻譯版本

torch.cuda用於設定和執行cuda操作。它跟蹤當前選定的gpu,預設情況下,您分配的所有cuda tensor都將在該裝置上建立。無論怎麼樣,一旦分配了乙個張量,就可以對它進行操作,而不必考慮所選的裝置,結果將始終與張量放在同乙個裝置上。預設情況下不允許跨gpu操作,除了copy u 和其他...