You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在ResNet模块中集成自定义Sin激活构建VGG16时遇类型错误

解决融合ResNet残差模块与Sin激活函数的VGG16模型构建类型错误

错误原因分析

  1. 模型构建方式冲突:你的residual_module函数采用Functional API风格(通过张量传递连接层),返回的是KerasTensor对象,但Sequential.add()方法要求传入Layer实例,而非张量,这直接触发了类型错误。
  2. 张量赋值错误:原代码中first = model.add(layers.MaxPooling2D(...))的写法无效,model.add()返回None,而非层的输出张量,导致后续传入残差模块的参数异常。

修复方案

改用Functional API完整构建模型,通过张量链式调用连接所有层和残差模块。以下是修正后的核心代码:

# ... 数据加载、预处理部分保持不变 ...

# 残差模块函数无需修改(本身是Functional风格)
def residual_module(layer_in, n_filters, kernel_size, padding, initializer, activation, regularizer, triple=False):
    activation2 = 'linear'
    filters2 = layer_in.shape[-1]
    size2 = 1
    conv1 = layers.Conv2D(n_filters, kernel_size, padding=padding, kernel_initializer=initializer, kernel_regularizer=regularizer)(layer_in)
    conv1 = layers.Activation(activation)(conv1)
    batch1 = layers.BatchNormalization()(conv1)
    conv2 = layers.Conv2D(filters2, size2, padding='same', kernel_regularizer=regularizer)(batch1)
    conv2 = layers.Activation(activation2)(conv2)
    batch2 = layers.BatchNormalization()(conv2)
    if triple == True:
        activation2 = activation
        filters2 = n_filters
        size2 = kernel_size
        conv3 = layers.Conv2D(layer_in.shape[-1], 1, padding='same', activation='linear', kernel_regularizer=regularizer)(batch2)
        batch3 = layers.BatchNormalization()(conv3)
        layer_out = layers.add([batch3, layer_in])
        layer_out = layers.Activation(activation)(layer_out)
    else:
        layer_out = layers.add([batch2, layer_in])
        layer_out = layers.Activation(activation)(layer_out)
    return layer_out

# 使用Functional API构建完整模型
weight_decay = 0.0005
input_layer = layers.Input(shape=(32,32,3))
x = residual_module(input_layer, n_filters=64, kernel_size=(3,3), padding='same', initializer="he_uniform", activation=tf.math.sin, regularizer=keras.regularizers.l2(weight_decay))
x = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
x = residual_module(x, 128, (3,3), 'same', None, tf.math.sin, keras.regularizers.l2(weight_decay))
x = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
x = residual_module(x, 256, (3,3), 'same', None, tf.math.sin, keras.regularizers.l2(weight_decay), triple=True)
x = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
x = residual_module(x, 512, (3,3), 'same', None, tf.math.sin, keras.regularizers.l2(weight_decay), triple=True)
x = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(x)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(4096, activation="relu")(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(4096, activation="relu")(x)
x = layers.Dropout(0.5)(x)
output_layer = layers.Dense(10)(x)

model = keras.Model(inputs=input_layer, outputs=output_layer)

# ... 模型编译、训练、保存部分保持不变 ...

可选优化:自定义Sin激活层

如果希望更规范地使用自定义激活,可以定义专属Layer类:

class SinActivation(layers.Layer):
    def __init__(self):
        super().__init__()
    
    def call(self, inputs):
        return tf.math.sin(inputs)

之后在残差模块中替换为conv1 = SinActivation()(conv1)即可。

内容的提问来源于stack exchange,提问作者James Newton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 22:20:41