检查目标时出错:预期 dense_Dense2 具有形状 x,但得到形状为 y 的数组
Error when checking target: expected dense_Dense2 to have shape x, but got array with shape y
这是我在 tensorflow 中的第一步。
想法
有一些数字模式(数字数组:Pattern = number[]
)。以及这个模式对应的类别(0到2的数字:Category = 0 | 1 | 2
)。我遵循结构数据:xs = Pattern[]
,ys = Category[]
。
例如:
xs = [[1, 2, 3, 4], [5, 6, 7, 8], ..., [9, 10, 11, 12]];
ys = [1, 0, ..., 2];
我希望神经网络找到 xs[0]
和 xy[0]
之间的匹配项,依此类推。我想传递像 [1, 2, 3, 4]
这样的神经网络数据并得到接近 1
.
的结果
model.predict(tf.tensor([1, 2, 3, 4])) // ≈1
我的代码
import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');
const xs = tf.tensor2d([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape, activation: 'relu' }));
^ - Pattern length, it is constant
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fit(xs, ys, { epochs: 500 });
我收到以下错误:
Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 3,4
我不明白如何解释我的神经网络数据结构。
模型 inputShape 是 [3,4]
。要使用此模型进行拟合或预测,它需要具有 [b, 3, 4]
形式的数据,其中 b 是批量形状。尝试使用 xs
.
拟合模型时缺少批量形状
模型 inputShape 应该是 [4] 以便 xs 可以用于预测。可以使用 xs.shape.slice(-1)
.
而不是 xs.shape
const xs = tf.tensor2d([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape.slice(1), activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fit(xs, ys);
model.predict(xs).print()
此外,如果模型的目标是预测使用 softmax
和 categoricalCrossentropy
指示的类别,则标签应 one-hot 编码。
相似答案:
我找到了适合我任务的解决方案。
只需要使用 dataset
https://js.tensorflow.org/api/latest/#tf.Sequential.fitDataset
import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');
const xArray = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
const yArray = [0, 1, 2];
const { length } = yArray;
const xs = tf.data.array(xArray);
const ys = tf.data.array(yArray);
const xyDataset = tf.data.zip({ xs: xDataset, ys: yDataset }).batch(length).shuffle(length);
const model = tf.sequential();
model.add(tf.layers.dense({ units: length, inputShape: [length], activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fitDataset(xyDataset, { epochs: 500 });
这是我在 tensorflow 中的第一步。
想法
有一些数字模式(数字数组:Pattern = number[]
)。以及这个模式对应的类别(0到2的数字:Category = 0 | 1 | 2
)。我遵循结构数据:xs = Pattern[]
,ys = Category[]
。
例如:
xs = [[1, 2, 3, 4], [5, 6, 7, 8], ..., [9, 10, 11, 12]];
ys = [1, 0, ..., 2];
我希望神经网络找到 xs[0]
和 xy[0]
之间的匹配项,依此类推。我想传递像 [1, 2, 3, 4]
这样的神经网络数据并得到接近 1
.
model.predict(tf.tensor([1, 2, 3, 4])) // ≈1
我的代码
import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');
const xs = tf.tensor2d([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape, activation: 'relu' }));
^ - Pattern length, it is constant
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fit(xs, ys, { epochs: 500 });
我收到以下错误:
Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 3,4
我不明白如何解释我的神经网络数据结构。
模型 inputShape 是 [3,4]
。要使用此模型进行拟合或预测,它需要具有 [b, 3, 4]
形式的数据,其中 b 是批量形状。尝试使用 xs
.
模型 inputShape 应该是 [4] 以便 xs 可以用于预测。可以使用 xs.shape.slice(-1)
.
xs.shape
const xs = tf.tensor2d([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape.slice(1), activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fit(xs, ys);
model.predict(xs).print()
此外,如果模型的目标是预测使用 softmax
和 categoricalCrossentropy
指示的类别,则标签应 one-hot 编码。
相似答案:
我找到了适合我任务的解决方案。
只需要使用 dataset
https://js.tensorflow.org/api/latest/#tf.Sequential.fitDataset
import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');
const xArray = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
const yArray = [0, 1, 2];
const { length } = yArray;
const xs = tf.data.array(xArray);
const ys = tf.data.array(yArray);
const xyDataset = tf.data.zip({ xs: xDataset, ys: yDataset }).batch(length).shuffle(length);
const model = tf.sequential();
model.add(tf.layers.dense({ units: length, inputShape: [length], activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fitDataset(xyDataset, { epochs: 500 });