Tensorflow.js inputShape 与模型输入不匹配 [英] Tensorflow.js inputShape doesn't match model input

查看:71
本文介绍了Tensorflow.js inputShape 与模型输入不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很基本,但我无法弄清楚.

This seems to be pretty basic but I can't figure it out.

所以我的样本/数据/输入是一个由 10 个整数组成的数组,而输出/标签只是一个整数数组.

So I have sample/data/inputs that is an array of arrays of 10 ints and the output/label it's just an array of integers.

让我解释一下,因为我的数据可能结构不正确.基于 10 个整数的输入,我告诉模型结果是标签/输出中的 1 个整数.

Let me explain as it might be that my data is not properly structured. Based on the input of 10 integers I tell the model the result is the 1 integer in the label/output.

最重要的是,我无法批量处理数据,因为它们是连续的.这意味着输入向右移动一位,因此 sample[i+1] 中的前九个整数是 sample[i] 的最后 9 个加上一个新的整数.

On top of that I cannot batch the data because they are sequential. Meaning the inputs shift by one to the right so the first nine integers in sample[i+1] are the last 9 of sample[i] plus a new one.

这是我如何编码的.

let labels = [1,0,0...]

let samples = [[0,1,1,0,1,0,1,1,1,0], ...]

基本上是一个由 10 个数组组成的数组.

basically an array of arrays of 10.

const model = tf.sequential();
let input = tf.tensor2d(samples);
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });
model.fit(labels, input, { batchSize: 1, shuffle: false, verbose: 1 });

当我尝试这个或任何其他输入组合时,我得到以下内容

When I try this or any other input combination i get the following

UnhandledPromiseRejectionWarning: Error: Error when checking model input: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see 1 Tensor(s), but instead got the following list of Tensor(s): 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0

预先感谢您的帮助.

推荐答案

inputShape 与模型输入不匹配

inputShape doesn't match model input

该错误表示模型的输入与数据之间存在形状不匹配.

The error indicates that there is a shape mismatch between the input of the model and the data.

如何解决这个问题?

  • 特征应该具有与 batchInputShape 相同的形状,或者应该比 InputShape 高一维.batchInputShape 的第一维通常为 null,以允许在训练期间使用不同的 batchInputShape.但是通过在此处指定恰好为 1(与 null 不同),训练的特征应该恰好具有等于 1 的第一个维度.如果它为 null,则可能具有形状 [b, ...InputShape]

  • The features should have the same shape as the batchInputShape or should be one dimension higher than the InputShape. The first dimension of the batchInputShape is generally null to allow for different batchInputShape during training. But by specifying here to be exactly 1 (different of null), the features of the training should exactly have the first dimension to be equal to 1. Had it been null, it could be possible to have features of shape [b, ...InputShape]

标签应该是 [b, LastLayerUnit] 形状.同样,通过指定批次的硬编码值(与 null 不同),标签的第一维应该正好是该长度.

the labels should of the shape [b, LastLayerUnit]. Again, by specifying a hard coded value of the batch (different of null) the labels first dimension should exactly be of that length.

什么是批次维度?

这里是一个有趣的答案,可以理解它.简单地说给定一个模型.以下允许训练模型:

here is an interesting answer to understand it. Simply put given a model. The following allows to train the model:

model.fit(features, label)

features 是一个特征数组,而特征是我们想要进行预测的一个元素.因此批量大小是该数组的长度.模型的第一层可以有参数 inputShape 或 batchInputShape 或两者都有,因为知道 batchInputShape 将优先于 inputShape.当只提供 inputShape 时,batchInputShape = [null, ...InputShape],这表明我们可以用不同长度的特征元素拟合模型,前提是标签具有相同长度的标签,这对标签需要为每个功能提供.

features is an array of feature whereas feature is one element from which we want to make a prediction. The batch size is therefore the length of that array. The first layer of a model can either have the parameters inputShape or batchInputShape or both, knowing that the batchInputShape will take precedence over the inputShape. When only inputShape is supplied, batchInputShape = [null, ...InputShape], thus indicating that we can fit the model with a various length of feature elements provided that the labels has the same length of label which makes sense for a label needs to be supplied for each feature.

因此

      inputShape = batchInputShape[1:] // python notation 
      inputShape = batchInputShape.slice(1) // js notation

无论如何,特征应该具有与 inputShape 相同的形状.

At any rate a feature should have the same shape as inputShape.

const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, batchInputShape: [1, 10], activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "softmax" }));
model.summary();
model.compile({ loss: "meanSquaredError", optimizer: "sgd", metrics: ["accuracy"] });


const arr = Array.from({length: 10}, (_, k) => k+1 )
const features =  tf.tensor(arr, [1, 10])
const labels = tf.tensor([1], [1, 1])

logs = await model.fit(features, labels, { batchSize: 1, shuffle: false, verbose: 1 });
console.log(logs)

这篇关于Tensorflow.js inputShape 与模型输入不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆