0
例:テンソルフローでは次のようにすることができます。mxnetでカーネルを制御するにはどうすればいいですか(重み=重み*マスク)。私たちがmxnetで必要としているようにカーネルを制御する方法
if mask_type is not None:
#C
mask[:center_h, :, :, :] = 1
if mask_type == 'A':
mask[center_h, :center_w, :, :] = 1
if mask_type == 'B':
mask[center_h, :center_w+1, :, :] = 1
else:
mask[:, :, :, :] = 1
weights_shape = [kernel_h, kernel_w, in_channel, num_outputs]
weights = tf.get_variable("weights", weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
weights = weights * mask
biases = tf.get_variable("biases", [num_outputs],
tf.float32, tf.constant_initializer(0.0))
outputs = tf.nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding="SAME")
outputs = tf.nn.bias_add(outputs, biases)
# #########これはtensorflow
def conv2d(inputs, num_outputs, kernel_shape, strides=[1, 1], mask_type=None, scope="conv2d"):
with tf.variable_scope(scope) as scope:
kernel_h, kernel_w = kernel_shape
stride_h, stride_w = strides
batch_size, height, width, in_channel = inputs.get_shape().as_list()
center_h = kernel_h // 2
center_w = kernel_w // 2
assert kernel_h % 2 == 1 and kernel_w % 2 == 1, "kernel height and width must be odd number"
mask = np.zeros((kernel_h, kernel_w, in_channel, num_outputs), dtype=np.float32)
if mask_type is not None:
#C
mask[:center_h, :, :, :] = 1
if mask_type == 'A':
mask[center_h, :center_w, :, :] = 1
if mask_type == 'B':
mask[center_h, :center_w+1, :, :] = 1
else:
mask[:, :, :, :] = 1
weights_shape = [kernel_h, kernel_w, in_channel, num_outputs]
weights = tf.get_variable("weights", weights_shape,
tf.float32, tf.truncated_normal_initializer(stddev=0.1))
weights = weights * mask
biases = tf.get_variable("biases", [num_outputs],
tf.float32, tf.constant_initializer(0.0))
outputs = tf.nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding="SAME")
outputs = tf.nn.bias_add(outputs, biases)
return outputs
重みを更新する方法は? –