首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorflowJS无法加载模型

TensorflowJS无法加载模型
EN

Stack Overflow用户
提问于 2020-04-10 19:42:19
回答 1查看 375关注 0票数 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不能加载它吗?会不会是不同的变量名(我的JSON文件中的batch_input_shape和错误消息中的batchInputShape )导致的?

下面是我如何构建和训练模型:

代码语言:javascript
复制
    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变量是从哪里来的,但是如果我修改它,我会得到一个错误,指出维度的数量是错误的。

代码语言:javascript
复制
    "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"}
        ]
    }]
}
EN

回答 1

Stack Overflow用户

发布于 2020-04-18 16:55:53

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61139552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档