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

如何将Keras 1.1.0的model.json适配至TensorFlow 2.3.1?

Fixing Keras 1.x Model JSON Compatibility with TensorFlow 2.3.1

The error you're hitting stems from outdated parameter names in your Keras 1.1.0-saved model JSON—these conventions changed significantly in modern TensorFlow/Keras (TF2.x). Here's exactly how to update your model.json to load successfully:

Critical Parameter Replacements

Keras 1.x used different naming for core layer arguments that TF2.x no longer recognizes. For every Dense layer in your JSON:

  • Replace output_dim with units (this is the direct cause of your missing 'units' argument error)
  • Swap init for kernel_initializer
  • Rename W_regularizer to kernel_regularizer
  • Change b_regularizer to bias_regularizer
  • Update W_constraint to kernel_constraint
  • Replace b_constraint with bias_constraint
  • Switch bias to use_bias (another TF2.x naming update)

For Dropout layers:

  • Replace p (dropout rate) with rate to align with TF2.x syntax

Updated Model JSON

Here's the fully revised version of your model.json that will load correctly in TF2.3.1:

{
  "class_name":"Sequential",
  "keras_version":"2.3.1",
  "config":[
    {
      "class_name":"Dense",
      "config":{
        "kernel_constraint":null,
        "bias_constraint":null,
        "name":"dense_1",
        "units":512,
        "activity_regularizer":null,
        "trainable":true,
        "kernel_initializer":"glorot_normal",
        "use_bias":true,
        "input_dtype":"float32",
        "input_dim":4096,
        "bias_regularizer":null,
        "kernel_regularizer":{
          "l2":0.0010000000474974513,
          "name":"WeightRegularizer",
          "l1":0.0
        },
        "activation":"relu",
        "batch_input_shape":[null,4096]
      }
    },
    {
      "class_name":"Dropout",
      "config":{
        "rate":0.6,
        "trainable":true,
        "name":"dropout_1"
      }
    },
    {
      "class_name":"Dense",
      "config":{
        "kernel_constraint":null,
        "bias_constraint":null,
        "name":"dense_2",
        "activity_regularizer":null,
        "trainable":true,
        "kernel_initializer":"glorot_normal",
        "use_bias":true,
        "input_dim":null,
        "bias_regularizer":null,
        "kernel_regularizer":{
          "l2":0.0010000000474974513,
          "name":"WeightRegularizer",
          "l1":0.0
        },
        "activation":"linear",
        "units":32
      }
    },
    {
      "class_name":"Dropout",
      "config":{
        "rate":0.6,
        "trainable":true,
        "name":"dropout_2"
      }
    },
    {
      "class_name":"Dense",
      "config":{
        "kernel_constraint":null,
        "bias_constraint":null,
        "name":"dense_3",
        "activity_regularizer":null,
        "trainable":true,
        "kernel_initializer":"glorot_normal",
        "use_bias":true,
        "input_dim":null,
        "bias_regularizer":null,
        "kernel_regularizer":{
          "l2":0.0010000000474974513,
          "name":"WeightRegularizer",
          "l1":0.0
        },
        "activation":"sigmoid",
        "units":1
      }
    }
  ]
}

Quick Extra Notes

  1. I updated the keras_version field to match your TF2.3.1 environment (Keras bundled with TF2.3.1 is version 2.3.1)
  2. All original regularization values and layer configurations are preserved—only the parameter names are adjusted

Save this revised JSON, and you should be able to load it using tensorflow.keras.models.model_from_json() without any type errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:13:11