首页 > 解决方案 > 未处理的拒绝(RangeError):使用 tf.models.modelFromJSON 时,Float32Array 的字节长度应为 4 的倍数

问题描述

我尝试训练情绪分析模型并将其加载到 tensorflow.js 但是,当我尝试这样做时,我收到了以下错误消息:

Unhandled Rejection (RangeError): byte length of Float32Array should be a multiple of 4 when using tf.models.modelFromJSON

以下是我开发过程中的一些重要部分:

Python:

model = tf.keras.models.Sequential([
  tf.keras.layers.Embedding(num_words, 1000, input_length=65-1),
  tf.keras.layers.Conv1D(filters=64, kernel_size=5,
                strides=1, padding="causal",
                activation="relu",
                input_shape=[None, 1]),
  tf.keras.layers.MaxPool1D(),
  tf.keras.layers.GlobalAveragePooling1D(),
  tf.keras.layers.Dense(24, activation='relu'),
  tf.keras.layers.Dense(5, activation="softmax"),
])
! tensorflowjs_converter --input_format keras \
                       /content/covid_td_1.h5 \
                      /content/covid_tc_02

然后,我将我的 model.json 文件转换为一个 javascript 文件并将其分配给一个const变量。

模型.json

export const modelJSON = {
    "format": "layers-model",
    "generatedBy": "keras v2.4.0",
    "convertedBy": "TensorFlow.js Converter v3.3.0",
    "modelTopology": {
        "keras_version": "2.4.0",
        "backend": "tensorflow",
        "model_config": {
            "class_name": "Sequential",
            "config": {
                "name": "sequential",
                "layers": [{
                    "class_name": "InputLayer",
                    "config": {
                        "batch_input_shape": [null, 64],
                        "dtype": "float32",
                        "sparse": false,
                        "ragged": false,
                        "name": "embedding_input"
                    }
                }, {
                    "class_name": "Embedding",
                    "config": {
                        "name": "embedding",
                        "trainable": true,
                        "batch_input_shape": [null, 64],
                        "dtype": "float32",
                        "input_dim": 10000,
                        "output_dim": 1000,
                        "embeddings_initializer": {
                            "class_name": "RandomUniform",
                            "config": {
                                "minval": -0.05,
                                "maxval": 0.05,
                                "seed": null
                            }
                        },
                        "embeddings_regularizer": null,
                        "activity_regularizer": null,
                        "embeddings_constraint": null,
                        "mask_zero": false,
                        "input_length": 64
                    }
                }, {
                    "class_name": "Conv1D",
                    "config": {
                        "name": "conv1d",
                        "trainable": true,
                        "batch_input_shape": [null, null, 1],
                        "dtype": "float32",
                        "filters": 64,
                        "kernel_size": [5],
                        "strides": [1],
                        "padding": "causal",
                        "data_format": "channels_last",
                        "dilation_rate": [1],
                        "groups": 1,
                        "activation": "relu",
                        "use_bias": true,
                        "kernel_initializer": {
                            "class_name": "GlorotUniform",
                            "config": {
                                "seed": null
                            }
                        },
                        "bias_initializer": {
                            "class_name": "Zeros",
                            "config": {}
                        },
                        "kernel_regularizer": null,
                        "bias_regularizer": null,
                        "activity_regularizer": null,
                        "kernel_constraint": null,
                        "bias_constraint": null
                    }
                }, {
                    "class_name": "MaxPooling1D",
                    "config": {
                        "name": "max_pooling1d",
                        "trainable": true,
                        "dtype": "float32",
                        "strides": [2],
                        "pool_size": [2],
                        "padding": "valid",
                        "data_format": "channels_last"
                    }
                }, {
                    "class_name": "GlobalAveragePooling1D",
                    "config": {
                        "name": "global_average_pooling1d",
                        "trainable": true,
                        "dtype": "float32",
                        "data_format": "channels_last"
                    }
                }, {
                    "class_name": "Dense",
                    "config": {
                        "name": "dense",
                        "trainable": true,
                        "dtype": "float32",
                        "units": 24,
                        "activation": "relu",
                        "use_bias": true,
                        "kernel_initializer": {
                            "class_name": "GlorotUniform",
                            "config": {
                                "seed": null
                            }
                        },
                        "bias_initializer": {
                            "class_name": "Zeros",
                            "config": {}
                        },
                        "kernel_regularizer": null,
                        "bias_regularizer": null,
                        "activity_regularizer": null,
                        "kernel_constraint": null,
                        "bias_constraint": null
                    }
                }, {
                    "class_name": "Dense",
                    "config": {
                        "name": "dense_1",
                        "trainable": true,
                        "dtype": "float32",
                        "units": 5,
                        "activation": "softmax",
                        "use_bias": true,
                        "kernel_initializer": {
                            "class_name": "GlorotUniform",
                            "config": {
                                "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": {
            "loss": "categorical_crossentropy",
            "metrics": [
                [{
                    "class_name": "MeanMetricWrapper",
                    "config": {
                        "name": "accuracy",
                        "dtype": "float32",
                        "fn": "categorical_accuracy"
                    }
                }]
            ],
            "weighted_metrics": null,
            "loss_weights": null,
            "optimizer_config": {
                "class_name": "Adam",
                "config": {
                    "name": "Adam",
                    "learning_rate": 0.0010000000474974513,
                    "decay": 0.0,
                    "beta_1": 0.8999999761581421,
                    "beta_2": 0.9990000128746033,
                    "epsilon": 1e-07,
                    "amsgrad": false
                }
            }
        }
    },
    "weightsManifest": [{
        "paths": ["group1-shard1of10.bin", "group1-shard2of10.bin", "group1-shard3of10.bin", "group1-shard4of10.bin", "group1-shard5of10.bin", "group1-shard6of10.bin", "group1-shard7of10.bin", "group1-shard8of10.bin", "group1-shard9of10.bin", "group1-shard10of10.bin"],
        "weights": [{
            "name": "conv1d/kernel",
            "shape": [5, 1000, 64],
            "dtype": "float32"
        }, {
            "name": "conv1d/bias",
            "shape": [64],
            "dtype": "float32"
        }, {
            "name": "dense/kernel",
            "shape": [64, 24],
            "dtype": "float32"
        }, {
            "name": "dense/bias",
            "shape": [24],
            "dtype": "float32"
        }, {
            "name": "dense_1/kernel",
            "shape": [24, 5],
            "dtype": "float32"
        }, {
            "name": "dense_1/bias",
            "shape": [5],
            "dtype": "float32"
        }, {
            "name": "embedding/embeddings",
            "shape": [10000, 1000],
            "dtype": "float32"
        }]
    }]
}

这是我初始化模型的地方。

import * as tf from '@tensorflow/tfjs';
import {modelJSON} from './file (1)/content/covid_tc_02/model';



const model = tf.models.modelFromJSON(modelJSON).then((result) => {
  console.log(result.summary)
});

如果有帮助,我正在使用带有反应的 tensorflow.js。

我尝试清除后端并重新启动运行时,但这不起作用。

到底是怎么回事?为什么我的模型不能正常工作?

如有需要,请留言以获取更多信息。谢谢!

标签: javascriptpythontensorflowtensorflow.jstensorflowjs-converter

解决方案


使用 tensorflow.js(至少对我而言)在本地加载模型确实是不可能的。相反,我将模型放入 github 存储库并使用该链接加载模型


推荐阅读