tf.layers.conv2d
和tf.layers.Conv2D
有什么区别?
为什么在此paper的示例代码中使用tf.layers.Conv2D
?
这是完整的代码段:
class AddCoords(base.Layer):
"""Add coords to a tensor"""
def __init__(self, x_dim=64, y_dim=64, with_r=False):
super(AddCoords, self).__init__()
self.x_dim = x_dim
self.y_dim = y_dim
self.with_r = with_r
def call(self, input_tensor):
"""
input_tensor: (batch, x_dim, y_dim, c)
"""
batch_size_tensor = tf.shape(input_tensor)[0]
xx_ones = tf.ones([batch_size_tensor, self.x_dim], dtype=tf.int32)
xx_ones = tf.expand_dims(xx_ones, -1)
xx_range = tf.tile(tf.expand_dims(tf.range(self.x_dim), 0), [batch_size_tensor, 1])
xx_range = tf.expand_dims(xx_range, 1)
xx_channel = tf.matmul(xx_ones, xx_range)
xx_channel = tf.expand_dims(xx_channel, -1)
yy_ones = tf.ones([batch_size_tensor, self.y_dim], dtype=tf.int32)
yy_ones = tf.expand_dims(yy_ones, 1)
yy_range = tf.tile(tf.expand_dims(tf.range(self.y_dim), 0), [batch_size_tensor, 1])
yy_range = tf.expand_dims(yy_range, -1)
yy_channel = tf.matmul(yy_range, yy_ones)
yy_channel = tf.expand_dims(yy_channel, -1)
xx_channel = tf.cast(xx_channel, 'float32') / (self.x_dim - 1)
yy_channel = tf.cast(yy_channel, 'float32') / (self.y_dim - 1)
xx_channel = xx_channel*2 - 1
yy_channel = yy_channel*2 - 1
ret = tf.concat([input_tensor, xx_channel, yy_channel], axis=-1)
if self.with_r:
rr = tf.sqrt(tf.square(xx_channel-0.5) + tf.square(yy_channel-0.5))
ret = tf.concat([ret, rr], axis=-1)
return ret
class CoordConv(base.Layer):
"""CoordConv layer as in the paper."""
def __init__(self, x_dim, y_dim, with_r, *args, **kwargs):
super(CoordConv, self).__init__()
self.addcoords = AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=with_r)
self.conv = tf.layers.Conv2D(*args, **kwargs)
def call(self, input_tensor):
ret = self.addcoords(input_tensor)
ret = self.conv(ret)
return ret
答案 0 :(得分:2)
const arr1 = [
{ name: "name1", id:"1" },
{ name: "name2", id:"2" },
{ name: "name3", id:"3" }
];
const arr2 = [
{ surname: "surname1" },
{ surname: "surname2" },
{ surname: "surname3" }
];
const newArr = arr1.map((item, index) => ({ ...item, surname: arr2[index].surname}));
console.log(newArr);
是一个简单的tf.layers.conv2d
/ function
,用于计算其输入的卷积,因此需要输入method
和feature maps
或kernel
运行此方法。一个用户只是调用此方法来计算卷积。
但是filter
是tf.layers.Conv2d
(一种OOP概念),您应该在使用某些Class
之前实例化它。实例化后,可以向其输入不同的输入并获取输出。使用该filter
来设计和编程新的Class
或一种新的操作类型,以供其他用户使用。
答案 1 :(得分:2)
通话
y = tf.layers.conv2d(inputs, **kwargs)
y = tf.layers.Conv2D(**kwargs).apply(inputs)
因此,只要您不打算重用由tf.layers.conv2d
创建的对象而不是调用其Conv2D
方法,就可以将apply
视为快捷方式。