如何(有效地)在 TensorFlow 中应用通道级全连接层
How to (efficiently) apply a channel-wise fully connected layer in TensorFlow
我又来找你了,我正在为我可以开始工作的事情挠头,但真的很慢。希望大家帮我优化一下
我正在尝试在 TensorFlow 中实现一个卷积自动编码器,在编码器和解码器之间有一个很大的潜在 space。通常,人们会使用全连接层将编码器连接到解码器,但由于这个潜在的 space 具有高维度,这样做会产生太多的特征,使其在计算上不可行。
我在 this paper 中找到了解决此问题的好方法。他们称之为 'channel-wise fully connected layer'。它基本上是每个通道的全连接层。
我正在努力实施并且我让它工作了,但是图表的生成需要很长时间。到目前为止,这是我的代码:
def _network(self, dataset, isTraining):
encoded = self._encoder(dataset, isTraining)
with tf.variable_scope("fully_connected_channel_wise"):
shape = encoded.get_shape().as_list()
print(shape)
channel_wise = tf.TensorArray(dtype=tf.float32, size=(shape[-1]))
for i in range(shape[-1]): # last index in shape should be the output channels of the last conv
channel_wise = channel_wise.write(i, self._linearLayer(encoded[:,:,i], shape[1], shape[1]*4,
name='Channel-wise' + str(i), isTraining=isTraining))
channel_wise = channel_wise.concat()
reshape = tf.reshape(channel_wise, [shape[0], shape[1]*4, shape[-1]])
reconstructed = self._decoder(reshape, isTraining)
return reconstructed
那么,关于为什么要花这么长时间,有什么想法吗?这实际上是一个范围(2048),但所有线性层都非常小(4x16)。我是不是用错了方法?
谢谢!
您可以查看他们在 Tensorflow 中对该论文的实现。
这是他们对 'channel-wise fully connected layer'.
的实现
def channel_wise_fc_layer(self, input, name): # bottom: (7x7x512)
_, width, height, n_feat_map = input.get_shape().as_list()
input_reshape = tf.reshape( input, [-1, width*height, n_feat_map] )
input_transpose = tf.transpose( input_reshape, [2,0,1] )
with tf.variable_scope(name):
W = tf.get_variable(
"W",
shape=[n_feat_map,width*height, width*height], # (512,49,49)
initializer=tf.random_normal_initializer(0., 0.005))
output = tf.batch_matmul(input_transpose, W)
output_transpose = tf.transpose(output, [1,2,0])
output_reshape = tf.reshape( output_transpose, [-1, height, width, n_feat_map] )
return output_reshape
主要思想是使用tf.batch_matmul函数。
但是,tf.batch_matmul在最新版本的Tensorflow中被删除了,您可以使用tf.matmul来代替它。
我又来找你了,我正在为我可以开始工作的事情挠头,但真的很慢。希望大家帮我优化一下
我正在尝试在 TensorFlow 中实现一个卷积自动编码器,在编码器和解码器之间有一个很大的潜在 space。通常,人们会使用全连接层将编码器连接到解码器,但由于这个潜在的 space 具有高维度,这样做会产生太多的特征,使其在计算上不可行。
我在 this paper 中找到了解决此问题的好方法。他们称之为 'channel-wise fully connected layer'。它基本上是每个通道的全连接层。
我正在努力实施并且我让它工作了,但是图表的生成需要很长时间。到目前为止,这是我的代码:
def _network(self, dataset, isTraining):
encoded = self._encoder(dataset, isTraining)
with tf.variable_scope("fully_connected_channel_wise"):
shape = encoded.get_shape().as_list()
print(shape)
channel_wise = tf.TensorArray(dtype=tf.float32, size=(shape[-1]))
for i in range(shape[-1]): # last index in shape should be the output channels of the last conv
channel_wise = channel_wise.write(i, self._linearLayer(encoded[:,:,i], shape[1], shape[1]*4,
name='Channel-wise' + str(i), isTraining=isTraining))
channel_wise = channel_wise.concat()
reshape = tf.reshape(channel_wise, [shape[0], shape[1]*4, shape[-1]])
reconstructed = self._decoder(reshape, isTraining)
return reconstructed
那么,关于为什么要花这么长时间,有什么想法吗?这实际上是一个范围(2048),但所有线性层都非常小(4x16)。我是不是用错了方法?
谢谢!
您可以查看他们在 Tensorflow 中对该论文的实现。 这是他们对 'channel-wise fully connected layer'.
的实现def channel_wise_fc_layer(self, input, name): # bottom: (7x7x512)
_, width, height, n_feat_map = input.get_shape().as_list()
input_reshape = tf.reshape( input, [-1, width*height, n_feat_map] )
input_transpose = tf.transpose( input_reshape, [2,0,1] )
with tf.variable_scope(name):
W = tf.get_variable(
"W",
shape=[n_feat_map,width*height, width*height], # (512,49,49)
initializer=tf.random_normal_initializer(0., 0.005))
output = tf.batch_matmul(input_transpose, W)
output_transpose = tf.transpose(output, [1,2,0])
output_reshape = tf.reshape( output_transpose, [-1, height, width, n_feat_map] )
return output_reshape
主要思想是使用tf.batch_matmul函数。
但是,tf.batch_matmul在最新版本的Tensorflow中被删除了,您可以使用tf.matmul来代替它。