我正在尝试实现一个用 tensorflow 编写到 pytorch 的图像去噪 Gan,但我无法理解 pytorch 中 tf.variable_scope
和 tf.Variable
的相似之处。请帮忙。
def conv_layer(input_image, ksize, in_channels, out_channels, stride, scope_name, activation_function=lrelu, reuse=False):
with tf.variable_scope(scope_name):
filter = tf.Variable(tf.random_normal([ksize, ksize, in_channels, out_channels], stddev=0.03))
output = tf.nn.conv2d(input_image, filter, strides=[1, stride, stride, 1], padding='SAME')
output = slim.batch_norm(output)
if activation_function:
output = activation_function(output)
return output, filter
def residual_layer(input_image, ksize, in_channels, out_channels, stride, scope_name):
with tf.variable_scope(scope_name):
output, filter = conv_layer(input_image, ksize, in_channels, out_channels, stride, scope_name+"_conv1")
output, filter = conv_layer(output, ksize, out_channels, out_channels, stride, scope_name+"_conv2")
output = tf.add(output, tf.identity(input_image))
return output, filter
def transpose_deconvolution_layer(input_tensor, used_weights, new_shape, stride, scope_name):
with tf.varaible_scope(scope_name):
output = tf.nn.conv2d_transpose(input_tensor, used_weights, output_shape=new_shape, strides=[1, stride, stride, 1], padding='SAME')
output = tf.nn.relu(output)
return output
def resize_deconvolution_layer(input_tensor, new_shape, scope_name):
with tf.variable_scope(scope_name):
output = tf.image.resize_images(input_tensor, (new_shape[1], new_shape[2]), method=1)
output, unused_weights = conv_layer(output, 3, new_shape[3]*2, new_shape[3], 1, scope_name+"_deconv")
return output
答案 0 :(得分:1)
您可以将 tf.Variable
替换为 torch.tensor
,torch.tensor
可以保持相同的渐变。
在 torch 中,您也不会创建图形,然后通过某个范围按名称访问其中的内容。您只需创建张量,然后就可以直接访问它。那里的 output
变量可以让您随心所欲地使用它,并可以随心所欲地重复使用。
事实上,如果你的代码没有直接使用这个变量作用域,那么你可以忽略它。如果您要检查图形,变量作用域通常只是为了给事物提供方便的名称。