gpt4 book ai didi

python - 使用 Lambda 创建自定义 Keras Layer 对象

转载 作者:行者123 更新时间:2023-11-30 22:00:13 25 4
gpt4 key购买 nike

我想构建一个自定义 Keras 层,保留 k 个最高激活值。我目前正在这样做(并且工作正常):

def max_topk_pool(x,k):
import tensorflow as tf
k_max = tf.nn.top_k(x,k=k,sorted=True,name=None)
return k_max

def KMax(k):
return Lambda(max_topk_pool,
arguments={'k':k},
output_shape=lambda x: (None, k))

你知道是否有一种方法可以按照Keras在https://keras.io/layers/writing-your-own-keras-layers/中所示的方式构建自定义图层类“KMax”

from keras import backend as K
from keras.layers import Layer

class MyLayer(Layer):

def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='uniform',
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this at the end

def call(self, x):
return K.dot(x, self.kernel)

def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)

我想要这样的东西:

from keras import backend as K
from keras.layers import Layer

class KMax(Layer):

def __init__(self, output_dim, **kwargs):
self.K = K
super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):
<... Lambda here ?>

def compute_output_shape(self, input_shape):
return (input_shape[0], self.K)

非常感谢!

最佳答案

这是您需要的(基于 https://github.com/keras-team/keras/issues/373 ):

from keras.engine import Layer, InputSpec
from keras.layers import Flatten
import tensorflow as tf


# https://github.com/keras-team/keras/issues/373
class KMaxPooling(Layer):
"""
K-max pooling layer that extracts the k-highest activations from a sequence (2nd dimension).
TensorFlow backend.
"""

def __init__(self, k=1, **kwargs):
super().__init__(**kwargs)
self.input_spec = InputSpec(ndim=3)
self.k = k

def compute_output_shape(self, input_shape):
return input_shape[0], (input_shape[2] * self.k)

def call(self, inputs):
# swap last two dimensions since top_k will be applied along the last dimension
shifted_input = tf.transpose(inputs, [0, 2, 1])

# extract top_k, returns two tensors [values, indices]
top_k = tf.nn.top_k(shifted_input, k=self.k, sorted=True, name=None)[0]

# return flattened output
return Flatten()(top_k)

def get_config(self):
config = {'k': self.k}
base_config = super().get_config()
return {**base_config, **config}

关于python - 使用 Lambda 创建自定义 Keras Layer 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54380375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com