質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Chainer

Chainerは、国産の深層学習フレームワークです。あらゆるニューラルネットワークをPythonで柔軟に書くことができ、学習させることが可能。GPUをサポートしており、複数のGPUを用いた学習も直感的に記述できます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

466閲覧

chainerの畳み込み層で生成される特徴マップの数はどうやって決まるのですか?

deb

総合スコア17

Chainer

Chainerは、国産の深層学習フレームワークです。あらゆるニューラルネットワークをPythonで柔軟に書くことができ、学習させることが可能。GPUをサポートしており、複数のGPUを用いた学習も直感的に記述できます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/05/10 02:03

前提・実現したいこと

chainerを使ってセマンティックセグメンテーションを行っています。chainerモジュールの中身にアクセスして、畳み込みのカーネルの部分に細工をしたいと思っているのですが、カーネルや特徴マップの数を決める文章を見つけられません。
下記が該当と思われるchainerモジュールの中の chainer.links.convolution_2d.py ですが、変数となる、
(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, *, dilate=1, groups=1)
の中に、カーネルの数はありますか?

python

1from chainer.functions.connection import convolution_2d 2from chainer import initializers 3from chainer import link 4from chainer.utils import argument 5from chainer import variable 6 7 8class Convolution2D(link.Link): 9 10 """__init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, \ 11nobias=False, initialW=None, initial_bias=None, *, dilate=1, groups=1) 12 13 Two-dimensional convolutional layer. 14 15 This link wraps the :func:`~chainer.functions.convolution_2d` function and 16 holds the filter weight and bias vector as parameters. 17 18 The output of this function can be non-deterministic when it uses cuDNN. 19 If ``chainer.configuration.config.deterministic`` is ``True`` and 20 cuDNN version is >= v3, it forces cuDNN to use a deterministic algorithm. 21 22 Convolution links can use a feature of cuDNN called autotuning, which 23 selects the most efficient CNN algorithm for images of fixed-size, 24 can provide a significant performance boost for fixed neural nets. 25 To enable, set `chainer.using_config('autotune', True)` 26 27 .. warning:: 28 29 ``deterministic`` argument is not supported anymore since v2. 30 Instead, use ``chainer.using_config('cudnn_deterministic', value`` 31 (value is either ``True`` or ``False``). 32 See :func:`chainer.using_config`. 33 34 Args: 35 in_channels (int or None): Number of channels of input arrays. 36 If ``None``, parameter initialization will be deferred until the 37 first forward data pass at which time the size will be determined. 38 out_channels (int): Number of channels of output arrays. 39 ksize (int or pair of ints): Size of filters (a.k.a. kernels). 40 ``ksize=k`` and ``ksize=(k, k)`` are equivalent. 41 stride (int or pair of ints): Stride of filter applications. 42 ``stride=s`` and ``stride=(s, s)`` are equivalent. 43 pad (int or pair of ints): Spatial padding width for input arrays. 44 ``pad=p`` and ``pad=(p, p)`` are equivalent. 45 nobias (bool): If ``True``, then this link does not use the bias term. 46 initialW (:ref:`initializer <initializer>`): Initializer to 47 initialize the weight. When it is :class:`numpy.ndarray`, 48 its ``ndim`` should be 4. 49 initial_bias (:ref:`initializer <initializer>`): Initializer to 50 initialize the bias. If ``None``, the bias will be initialized to 51 zero. When it is :class:`numpy.ndarray`, its ``ndim`` should be 1. 52 dilate (int or pair of ints): 53 Dilation factor of filter applications. 54 ``dilate=d`` and ``dilate=(d, d)`` are equivalent. 55 groups (:class:`int`): Number of groups of channels. If the number 56 is greater than 1, input tensor :math:`W` is divided into some 57 blocks by this value channel-wise. For each tensor blocks, 58 convolution operation will be executed independently. Input channel 59 size ``in_channels`` and output channel size ``out_channels`` must 60 be exactly divisible by this value. 61 62 .. seealso:: 63 See :func:`chainer.functions.convolution_2d` for the definition of 64 two-dimensional convolution. 65 66 Attributes: 67 W (~chainer.Variable): Weight parameter. 68 b (~chainer.Variable): Bias parameter. 69 70 .. admonition:: Example 71 72 There are several ways to make a Convolution2D link. 73 74 Let an input vector ``x`` be: 75 76 >>> x = np.arange(1 * 3 * 10 * 10, dtype=np.float32).reshape( 77 ... 1, 3, 10, 10) 78 79 1. Give the first three arguments explicitly: 80 81 >>> l = L.Convolution2D(3, 7, 5) 82 >>> y = l(x) 83 >>> y.shape 84 (1, 7, 6, 6) 85 86 2. Omit ``in_channels`` or fill it with ``None``: 87 88 The below two cases are the same. 89 90 >>> l = L.Convolution2D(7, 5) 91 >>> y = l(x) 92 >>> y.shape 93 (1, 7, 6, 6) 94 95 >>> l = L.Convolution2D(None, 7, 5) 96 >>> y = l(x) 97 >>> y.shape 98 (1, 7, 6, 6) 99 100 When you omit the first argument, you need to specify the other 101 subsequent arguments from ``stride`` as keyword auguments. So the 102 below two cases are the same. 103 104 >>> l = L.Convolution2D(7, 5, stride=1, pad=0) 105 >>> y = l(x) 106 >>> y.shape 107 (1, 7, 6, 6) 108 109 >>> l = L.Convolution2D(None, 7, 5, 1, 0) 110 >>> y = l(x) 111 >>> y.shape 112 (1, 7, 6, 6) 113 114 """ 115 116 def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, 117 nobias=False, initialW=None, initial_bias=None, **kwargs): 118 super(Convolution2D, self).__init__() 119 120 dilate, groups = argument.parse_kwargs( 121 kwargs, ('dilate', 1), ('groups', 1), 122 deterministic="deterministic argument is not supported anymore. " 123 "Use chainer.using_config('cudnn_deterministic', value) " 124 "context where value is either `True` or `False`.") 125 126 if ksize is None: 127 out_channels, ksize, in_channels = in_channels, out_channels, None 128 129 self.ksize = ksize 130 self.stride = _pair(stride) 131 self.pad = _pair(pad) 132 self.dilate = _pair(dilate) 133 self.out_channels = out_channels 134 self.groups = int(groups) 135 136 with self.init_scope(): 137 W_initializer = initializers._get_initializer(initialW) 138 self.W = variable.Parameter(W_initializer) 139 if in_channels is not None: 140 self._initialize_params(in_channels) 141 142 if nobias: 143 self.b = None 144 else: 145 if initial_bias is None: 146 initial_bias = 0 147 bias_initializer = initializers._get_initializer(initial_bias) 148 self.b = variable.Parameter(bias_initializer, out_channels) 149 150 def _initialize_params(self, in_channels): 151 kh, kw = _pair(self.ksize) 152 if self.out_channels % self.groups != 0: 153 raise ValueError('the number of output channels must be' 154 ' divisible by the number of groups') 155 if in_channels % self.groups != 0: 156 raise ValueError('the number of input channels must be' 157 ' divisible by the number of groups') 158 W_shape = (self.out_channels, int(in_channels / self.groups), kh, kw) 159 self.W.initialize(W_shape) 160 161 def forward(self, x): 162 """Applies the convolution layer. 163 164 Args: 165 x (~chainer.Variable): Input image. 166 167 Returns: 168 ~chainer.Variable: Output of the convolution. 169 170 """ 171 if self.W.array is None: 172 self._initialize_params(x.shape[1]) 173 return convolution_2d.convolution_2d( 174 x, self.W, self.b, self.stride, self.pad, dilate=self.dilate, 175 groups=self.groups) 176 177 178def _pair(x): 179 if hasattr(x, '__getitem__'): 180 return x 181 return x, x 182

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

out_channelsが生成されるカーネルの数になります。

in_channels * ksize * ksizeのカーネルがout_channels個つくられます。

投稿2019/06/07 01:30

physics303

総合スコア89

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

deb

2019/06/10 00:57

回答ありがとうございます。 なるほど、よく読むとコード中に書いてありました、お恥ずかしい。 ちなみにカーネルの重みとなるWやbの更新の仕方が書いてあるプログラムコードはどこにあるかご存知ですか?
physics303

2019/06/11 01:02

Wやbを更新するのはoptimizerの役割です。optimizerあたりを探してみるといいと思います。
deb

2019/06/27 05:38

返事が遅くなってすいません。 Optimizer.pyの中に更新規則が書かれているのを見つけました。 本当に助かりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問