我目前正在考虑在 keras 中实施 Self-Attention GAN。我想实现的方式如下:
def Attention(X, channels):
def hw_flatten(x):
return np.reshape(x, (x.shape[0], -1, x.shape[-1]))
f = Conv2D(channels//8, kernel_size=1, strides=1, padding='same')(X) # [bs, h, w, c']
g = Conv2D(channels//8, kernel_size=1, strides=1, padding='same')(X) # [bs, h, w, c']
h = Conv2D(channels, kernel_size=1, strides=1, padding='same')(X) # [bs, h, w, c]
# N = h * w
flatten_g = hw_flatten(g)
flatten_f = hw_flatten(f)
s = np.matmul(flatten_g, flatten_f.reshape((flatten_f.shape[0], flatten_f.shape[-1], -1))) # [bs, N, N]
beta = softmax(s, axis=-1) # attention map
flatten_h = hw_flatten(h) …Run Code Online (Sandbox Code Playgroud) conv-neural-network keras tensorflow attention-model generative-adversarial-network
我正在为我的神经网络设计一个自定义层,但我的代码出现错误。
我想做一个注意力层,如论文中所述:SAGAN。和原来的tf代码
class AttentionLayer(Layer):
def __init__(self, **kwargs):
super(AttentionLayer, self).__init__(**kwargs)
def build(self, input_shape):
input_dim = input_shape[-1]
filters_f_g = input_dim // 8
filters_h = input_dim
kernel_shape_f_g = (1, 1) + (input_dim, filters_f_g)
kernel_shape_h = (1, 1) + (input_dim, filters_h)
# Create a trainable weight variable for this layer:
self.gamma = self.add_weight(name='gamma', shape=[1], initializer='zeros', trainable=True)
self.kernel_f = self.add_weight(shape=kernel_shape_f_g,
initializer='glorot_uniform',
name='kernel')
self.kernel_g = self.add_weight(shape=kernel_shape_f_g,
initializer='glorot_uniform',
name='kernel')
self.kernel_h = self.add_weight(shape=kernel_shape_h,
initializer='glorot_uniform',
name='kernel')
self.bias_f = self.add_weight(shape=(filters_f_g,),
initializer='zeros',
name='bias')
self.bias_g = self.add_weight(shape=(filters_f_g,),
initializer='zeros',
name='bias') …Run Code Online (Sandbox Code Playgroud)