TensorflowJS无法加载模型

时间:2020-04-10 11:42:18

标签: json tensorflow.js tensorflowjs-converter

我使用Keras构建和训练了一个模型,并使用tensorflowjs转换器(tfjs.converters.save_keras_model()函数)将其保存了。

后来,当尝试在tensorflowjs中加载它时,出现以下错误: Error: The first layer in a Sequential model must get an 'inputShape' or 'batchInputShape' argument.

但是在检入包含模型结构的JSON文件之后,指定了输入形状。 知道为什么tfjs无法加载吗? 可能是由于变量名不同(我的JSON文件中的batch_input_shape和错误消息中的batchInputShape)引起的。

这是我建立和训练模型的方式:

    model.add(LSTM(128, dropout=0.2, input_shape=(time_steps, input_dim) ))
    model.add(Dense(output_dim, activation='sigmoid'))

    model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
    model.fit_generator(generator=train_generator,
                          steps_per_epoch=steps_per_epoch,
                          epochs=epochs,
                          validation_data=valid_generator,
                          validation_steps=valid_steps
                        )

这是JSON文件。我不知道第三个null变量是从哪里来的,但是如果我更改它,则会收到错误消息,指出维数错误。

    "format": "layers-model", 
    "generatedBy": "keras v2.3.1", 
    "convertedBy": "TensorFlow.js Converter v1.4.0", 
    "modelTopology": {
        "keras_version": "2.3.1", 
        "backend": "tensorflow", 
        "model_config": {
            "class_name": "Sequential", 
            "config": {
                "name": "sequential_1", 
                "layers": [
                    {
                        "class_name": "LSTM", 
                        "config": {
                            "name": "lstm_1", 
                            "trainable": true, 
                            "batch_input_shape": [null, 10, 100], 
                            "dtype": "float32", 
                            "return_sequences": false, 
                            "return_state": false, 
                            "go_backwards": false, 
                            "stateful": false, 
                            "unroll": false, 
                            "units": 128, 
                            "activation": "tanh", 
                            "recurrent_activation": "sigmoid", 
                            "use_bias": true, 
                            "kernel_initializer": {
                                "class_name": "VarianceScaling", 
                                "config": {
                                    "scale": 1.0, 
                                    "mode": "fan_avg", 
                                    "distribution": "uniform", 
                                    "seed": null
                                }
                            }, 
                            "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, 
                            "bias_initializer": {"class_name": "Zeros", "config": {}}, 
                            "unit_forget_bias": true, 
                            "kernel_regularizer": null, 
                            "recurrent_regularizer": null, 
                            "bias_regularizer": null, 
                            "activity_regularizer": null, 
                            "kernel_constraint": null, 
                            "recurrent_constraint": null, 
                            "bias_constraint": null, 
                            "dropout": 0.2, 
                            "recurrent_dropout": 0.0, 
                            "implementation": 2
                        }
                    }, 
                    {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 29, "activation": "sigmoid", "use_bias": true, "kernel_initializer": 
                    {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}
                ]
            }
        }, 
        "training_config": {
            "optimizer_config": {
                "class_name": "Adam", 
                "config": {
                    "learning_rate": 0.0010000000474974513, 
                    "beta_1": 0.8999999761581421, 
                    "beta_2": 0.9990000128746033, 
                    "decay": 0.0, 
                    "epsilon": 1e-07, 
                    "amsgrad": false
                }
            }, 
            "loss": "mse", 
            "metrics": ["accuracy"], 
            "weighted_metrics": null, 
            "sample_weight_mode": null, 
            "loss_weights": null
        }
    }, 
    "weightsManifest": [{
        "paths": ["group1-shard1of1.bin"], 
        "weights": [
            {"name": "dense_1/kernel", "shape": [128, 29], "dtype": "float32"}, 
            {"name": "dense_1/bias", "shape": [29], "dtype": "float32"}, 
            {"name": "lstm_1/kernel", "shape": [100, 512], "dtype": "float32"}, 
            {"name": "lstm_1/recurrent_kernel", "shape": [128, 512], "dtype": "float32"}, 
            {"name": "lstm_1/bias", "shape": [512], "dtype": "float32"}
        ]
    }]
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我只上传了model.json文件,而不上传了由tensorflowjs_converter创建的bin文件。确保您确实在同一文件夹中上载了model.json文件和.bin文件。 model.json文件使用.bin文件获取权重。