Tensorflow CNN:从要在 Lambda 层中使用的卷积层访问数据
Tensorflow CNN: Accessing the data from the Convolution layer to be used in the Lambda layer
我正在使用 Tensorflow 2.x 在卷积层之后创建一个 Lambda 层。
我有一个名为 "custom_layer" 的函数,它接收前一个卷积层的张量输出。
我需要从这个张量中提取卷积层的每个特征图并执行数学运算。
最后,必须将输出组合成一个张量并返回以供下一层使用。
#Lamba layer
def custom_layer(tensor):
# perform operation on individual feature maps
# return the combined tensor output
return tensor
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters= 64, kernel_size= (3,3), input_shape = (28,28,1), activation = 'relu', name = 'conv2D_1'),
tf.keras.layers.Lambda(custom_layer, name="lambda_layer"),
tf.keras.layers.Conv2D(filters= 64, kernel_size= (3,3), activation = 'relu', name = 'conv2D_2'),
tf.keras.layers.MaxPooling2D(pool_size = (2,2), name = 'MaxPool2D_1'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation= 'softmax')])
使用 tf.print(tensor)
我能够查看张量输出(特征图)。但是我无法找到一种方法来访问这些单独的特征图。
问题已解决。我在 custom_layer(tensor) 函数中使用了 tf.py_function()。
我正在使用 Tensorflow 2.x 在卷积层之后创建一个 Lambda 层。 我有一个名为 "custom_layer" 的函数,它接收前一个卷积层的张量输出。 我需要从这个张量中提取卷积层的每个特征图并执行数学运算。 最后,必须将输出组合成一个张量并返回以供下一层使用。
#Lamba layer
def custom_layer(tensor):
# perform operation on individual feature maps
# return the combined tensor output
return tensor
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters= 64, kernel_size= (3,3), input_shape = (28,28,1), activation = 'relu', name = 'conv2D_1'),
tf.keras.layers.Lambda(custom_layer, name="lambda_layer"),
tf.keras.layers.Conv2D(filters= 64, kernel_size= (3,3), activation = 'relu', name = 'conv2D_2'),
tf.keras.layers.MaxPooling2D(pool_size = (2,2), name = 'MaxPool2D_1'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation= 'softmax')])
使用 tf.print(tensor)
我能够查看张量输出(特征图)。但是我无法找到一种方法来访问这些单独的特征图。
问题已解决。我在 custom_layer(tensor) 函数中使用了 tf.py_function()。