programing

tf.nn.conv2d 대 tf.layers.conv2d

nicescript 2021. 1. 15. 07:52
반응형

tf.nn.conv2d 대 tf.layers.conv2d


tf.nn.*over 를 사용하면 어떤 이점 tf.layers.*있습니까?

예를 들어 문서에있는 대부분의 예제는를 사용 tf.nn.conv2d하지만 그 이유는 명확하지 않습니다.


컨볼 루션의 경우 동일합니다. 더 정확하게는 tf.layers.conv2d(실제로 _Conv) tf.nn.convolution백엔드로 사용 합니다. 다음의 호출 체인을 따를 수 있습니다.tf.layers.conv2d>Conv2D>Conv2D.apply()>_Conv>_Conv.apply()>_Layer.apply()>_Layer.\__call__()>_Conv.call()>nn.convolution()...


GBY가 언급했듯이 동일한 구현을 사용합니다.

매개 변수에 약간의 차이가 있습니다.

tf.nn.conv2d의 경우 :

filter: A Tensor. Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels]

tf.layers.conv2d의 경우 :

filters: Integer, the dimensionality of the output space (i.e. the number of filters in the convolution).

사전 훈련 된 모델 (예제 코드 : https://github.com/ry/tensorflow-vgg16 )을 로드 할 때는 tf.nn.conv2d를 사용 하고 처음부터 훈련 된 모델에는 tf.layers.conv2d를 사용합니다.


다른 사람들이 언급했듯이 매개 변수는 특히 "필터"가 다릅니다. tf.nn.conv2d는 텐서를 필터로 사용합니다. 즉, cifar10 코드 에서 다음과 같이 가중치 감소 (또는 기타 속성)를 지정할 수 있습니다 . (전환 레이어에서 가중치 감소를 원하거나 필요한지 여부는 또 다른 질문입니다.)

kernel = _variable_with_weight_decay('weights',
                                     shape=[5, 5, 3, 64],
                                     stddev=5e-2,
                                     wd=0.0)
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')

정수만 필터로 사용하므로 tf.layers.conv2d에서 가중치 감소를 설정하는 방법을 잘 모르겠습니다. 어쩌면 사용 kernel_constraint?

반면에 tf.layers.conv2d는 tf.nn.conv2d를 사용하는 경우 추가 코드를 작성해야하는 동안 활성화 및 바이어스를 자동으로 처리합니다.


이 다른 모든 응답은 매개 변수가 어떻게 다른지에 대해 이야기하지만 실제로 tf.nn 및 tf.layers conv2d의 주요 차이점은 tf.nn의 경우 고유 한 필터 텐서를 생성하여 전달해야한다는 것입니다.이 필터 크기가 다음과 같아야합니다. [kernel_height, kernel_width, in_channels, num_filters]


여기를보세요 : tensorflow> tf.layers.conv2d

그리고 여기 : tensorflow> conv2d

보시다시피 레이어 버전에 대한 인수는 다음과 같습니다.

tf.layers.conv2d (inputs, filters, kernel_size, strides = (1, 1), padding = 'valid', data_format = 'channels_last', dilation_rate = (1, 1), activation = None, use_bias = True, kernel_initializer = 없음, bias_initializer = tf.zeros_initializer (), kernel_regularizer = None, bias_regularizer = None, activity_regularizer = None, trainable = True, name = None, 재사용 = None)

및 nn 버전 :

tf.nn.conv2d (입력, 필터, 스트라이드, 패딩, use_cudnn_on_gpu = None, data_format = None, name = None)

원하는 / 필요 / 좋아하는 옵션 중 하나를 선택할 수 있다고 생각합니다!


매개 변수의 차이점 :

코드에서 tf.layer * 사용 :

# Convolution Layer with 32 filters and a kernel size of 5
conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu) 
# Max Pooling (down-sampling) with strides of 2 and kernel size of 2
conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

코드에서 tf.nn * 사용 : (가중치와 편향 을 매개 변수로 추가전달해야합니다. )

strides = 1
# Weights matrix looks like: [kernel_size(=5), kernel_size(=5), input_channels (=3), filters (= 32)]
# Similarly bias = looks like [filters (=32)]
out = tf.nn.conv2d(input, weights, padding="SAME", strides = [1, strides, strides, 1])
out = tf.nn.bias_add(out, bias)
out = tf.nn.relu(out)

참조 URL : https://stackoverflow.com/questions/42785026/tf-nn-conv2d-vs-tf-layers-conv2d

반응형