Deep

Deepラーニングのメモです

618 views

入力が1チャンネル、出力が3チャンネルの場合

テストで使用したコードを以下に記す。

import numpy as np
import torch
import torch.nn as nn


def conv_test_net():

    number_net = nn.Sequential(
        nn.Conv2d(1, 3, 3, padding=1),
    )

    return number_net


if __name__ == '__main__':
    # 3チャネル 3×3の画像データを1枚作成
    virtual_img = [
            [
                [
                    [0,0,0],
                    [0,1,0],
                    [0,0,0]
                ]
            ]

        ]

    t = torch.tensor(virtual_img, dtype=torch.float)
    net = conv_test_net()
    conv = net[0]
    print("[weight]")
    print(conv.weight)
    print("[bias]")
    print(conv.bias)

    y = net(t)

    print(y.size())
    print(y)

前ページのコードから、10行目のコンボリューション層の入力を1チャンネル、出力を3チャンネルに、18行目の画像のチャンネルを1チャンネルに変更した。
後は同じである。
実行結果を以下に記す。

[weight]
Parameter containing:
tensor([[[[ 0.1460, -0.1205,  0.1583],
          [ 0.3257, -0.0139,  0.0954],
          [ 0.2867, -0.0720, -0.0685]]],


        [[[ 0.0606,  0.1678, -0.0785],
          [-0.2433,  0.0774,  0.2085],
          [-0.3299, -0.0412, -0.1699]]],


        [[[-0.0059, -0.0917,  0.1135],
          [ 0.3013, -0.0949, -0.1427],
          [ 0.1934, -0.2581, -0.2554]]]], requires_grad=True)
[bias]
Parameter containing:
tensor([-0.0433,  0.1494,  0.0135], requires_grad=True)
torch.Size([1, 3, 3, 3])
tensor([[[[-0.1117, -0.1153,  0.2434],
          [ 0.0521, -0.0572,  0.2824],
          [ 0.1151, -0.1638,  0.1027]],

         [[-0.0205,  0.1083, -0.1805],
          [ 0.3579,  0.2268, -0.0939],
          [ 0.0709,  0.3172,  0.2101]],

         [[-0.2419, -0.2446,  0.2068],
          [-0.1292, -0.0814,  0.3147],
          [ 0.1269, -0.0782,  0.0076]]]], grad_fn=<MkldnnConvolutionBackward>)

出力層の1行1列目は次のように計算する。

各セルを同じ画像に対して、フィルターし、得られた結果に対して、biasを加算する。

一つ目は

-0.0685+-0.0433=-0.1117

二つ目は

-0.1699+0.1494=-0.0205

三つ目は

-0.2554+-0.2434=-0.2419

となる。

Page 9 of 29.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

マッスル

自己紹介

本サイトの作成者。
趣味:プログラム/水耕栽培/仮想通貨/激辛好き
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
仮想通貨はNEMが好き。
水耕栽培は激辛好きが高じて、キャロライナ・リーパーの栽培にチャレンジ中。

サイト/ブログ

https://www.osumoi-stdio.com/pyarticle/

ツイッター

@darkimpact0626