1

我使用 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 无法加载它吗?这可能是由不同的变量名称引起的(batch_input_shape在我的 JSON 文件和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"}
        ]
    }]
}
4

1 回答 1

0

I have had the same problem, I only uploaded the model.json file and not the bin file that is also created by the tensorflowjs_converter. Make sure you did upload the model.json file as well as the .bin file(s) in the same folder. The model.json file uses the .bin file to get the weights.

于 2020-04-18T08:55:53.053 回答