【问题标题】:Error when checking target: expected dense_Dense2 to have shape x, but got array with shape y检查目标时出错:预期dense_Dense2 的形状为x,但得到的数组的形状为y
【发布时间】:2021-01-06 14:11:39
【问题描述】:

这是我在 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 });

我收到跟随错误:

检查输入时出错:预期 dense_Dense1_input 具有 3 个维度。但得到了形状为 3,4 的数组

我不明白如何解释我的神经网络数据结构。

【问题讨论】:

    标签: tensorflow tensorflow2.0 tensorflow.js


    【解决方案1】:

    模型 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()
    

    此外,如果模型的目标是预测使用 softmaxcategoricalCrossentropy 指示的类别,那么标签应该是 one-hot 编码的。

    类似的答案:

    【讨论】:

    • 不幸的是它不起作用。是的,它修复了错误,但是在发生新错误“检查目标时出错:预期dense_Dense2 具有形状[,3],但得到形状为[3,1] 的数组”之后。我知道需要准备数据或需要更改 inputShape,但我不知道该怎么做。
    • 我运行你的代码,只要你改变它就可以工作:inputShape: xs.shape.slice(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.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, { epochs: 500 });
    • 非常感谢您的帮助!
    • 我不明白这已经改变了,但现在它正在工作!
    【解决方案2】:

    我找到了适合我的任务的解决方案。 只需要使用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 });
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2018-08-29
      • 2020-08-29
      • 2019-08-02
      • 2019-07-07
      • 2020-09-03
      • 2020-03-13
      • 2020-05-19
      • 2019-01-16
      相关资源
      最近更新 更多