{
  "id": "tensorflow/keras-model-save-custom-layer",
  "signature": "ValueError: Unable to serialize the layer 'my_custom_layer'. The layer has a non-serializable argument in its __init__ method.",
  "signature_zh": "值错误：无法序列化层 'my_custom_layer'。该层的 __init__ 方法包含不可序列化的参数。",
  "regex": "Unable to serialize the layer.*non-serializable argument",
  "domain": "tensorflow",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A custom Keras layer has a constructor parameter that is not a TensorFlow-compatible type (e.g., a lambda function, a file handle, or a non-serializable object), which prevents model saving.",
  "root_cause_type": "generic",
  "root_cause_zh": "自定义 Keras 层的构造函数包含非 TensorFlow 兼容类型的参数（例如 lambda 函数、文件句柄或不可序列化的对象），导致模型无法保存。",
  "versions": [
    {
      "version": "TensorFlow 2.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Keras 2.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Save_weights only saves the weights, not the architecture or custom layer config, so loading requires redefining the model manually.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "An empty config omits necessary parameters, leading to errors when loading the model due to missing arguments.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Global variables break encapsulation and are not serialized, causing the same issue when the model is loaded in a different context.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Override the get_config() method in the custom layer to return a dictionary of all serializable parameters. Ensure each parameter is a basic Python type (int, float, str, list, dict) or a TensorFlow object. For complex objects, convert them to a serializable form (e.g., store a configuration dictionary).",
      "success_rate": 0.9,
      "how": "Override the get_config() method in the custom layer to return a dictionary of all serializable parameters. Ensure each parameter is a basic Python type (int, float, str, list, dict) or a TensorFlow object. For complex objects, convert them to a serializable form (e.g., store a configuration dictionary).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use the `from_config` class method to reconstruct the layer from the config dictionary. This ensures that when loading the model, the layer is correctly instantiated.",
      "success_rate": 0.85,
      "how": "Use the `from_config` class method to reconstruct the layer from the config dictionary. This ensures that when loading the model, the layer is correctly instantiated.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the parameter is truly non-serializable (e.g., a lambda), refactor it to use a serializable alternative like a string identifier and a mapping dictionary.",
      "success_rate": 0.8,
      "how": "If the parameter is truly non-serializable (e.g., a lambda), refactor it to use a serializable alternative like a string identifier and a mapping dictionary.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在自定义层中重写 get_config() 方法，返回所有可序列化参数的字典。确保每个参数是基本 Python 类型或 TensorFlow 对象。对于复杂对象，转换为可序列化形式（例如存储配置字典）。",
    "使用 from_config 类方法从配置字典重建层。这确保加载模型时正确实例化层。",
    "如果参数确实不可序列化（例如 lambda），将其重构为可序列化的替代方案，如字符串标识符和映射字典。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/guide/keras/save_and_serialize#custom_objects",
  "official_doc_section": null,
  "error_code": "EMSS",
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-06-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}