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

Keras调用层传入name参数报错TypeError的原因与解决方法

Fixing "TypeError: call() got an unexpected keyword argument 'name'" in Keras

Let's break down why this error pops up and how to fix it—since you need named layers for weight assignment later, we'll make sure the solution aligns with that goal:

Why the Error Happens

You're passing the name argument to the layer's call() method (when you run g_layer(bncv11, name='GB')), but Keras layer call() methods don't accept this parameter. Layer names are meant to be assigned when you create the layer instance, not when you invoke it to process input.

This is the standard Keras workflow, and it directly supports your need to reference layers by name for weight operations:

# Move the 'name' parameter to the DepthwiseConv2D initialization
g_layer = DepthwiseConv2D(kernel_size, use_bias=False, padding='same', name='GB')
# Call the layer without the name argument
decoded_noise = g_layer(bncv11)

Solution 2: Rename an Existing Layer Instance

If you've already created the layer and need to adjust its name afterward, you can directly modify its name attribute:

# Original layer creation (with temporary name)
g_layer = DepthwiseConv2D(kernel_size, use_bias=False, padding='same', name='glay')
# Rename the layer before or after calling it
g_layer.name = 'GB'
decoded_noise = g_layer(bncv11)

Accessing the Layer for Weight Assignment

Once your layer has the correct name, you can easily retrieve it from your model later to set weights:

# Assuming your model is stored in a variable called `model`
target_layer = model.get_layer(name='GB')
# Use set_weights() to assign your desired weights
# target_layer.set_weights(your_prepared_weight_array)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:26:06