TF.js 中的张量大小不正确

作者: codingMASTER398创建于 2023年3月20日更新于 2023年3月20日

我正在尝试在 Tensorflow.JS 中训练这个模型,如下载该模型:

javascript
// 加载 Fashion MNIST 数据集
const urll = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/';
const filenames = {
  images: 'train-images-idx3-ubyte.gz',
  labels: 'train-labels-idx1-ubyte.gz'
};

const loadFile = async (filename) => {
  const url = `${urll}${filename}`;
  const response = await fetch(url);
  const buffer = await response.arrayBuffer();
  return new Uint8Array(buffer);
};

const loadDataset = async () => {
  const [imageData, labelData] = await Promise.all([
    loadFile(filenames.images),
    loadFile(filenames.labels)
  ]);

  const imageTensor = tf.tensor(imageData.slice(16), [60000, 28, 28, 1]);
  console.log(`imageTensor.shape: ${imageTensor.shape}`);
  console.log(`imageData.byteLength: ${imageData.byteLength}`);

  const labelTensor = tf.tensor(labelData.slice(8), [60000], 'int32');
  console.log(`labelTensor.shape: ${labelTensor.shape}`);
  console.log(`labelData.byteLength: ${labelData.byteLength}`);

  return {
    images: imageTensor,
    labels: labelTensor
  };
};
根据提供的形状 [60000,28,28,1], 该张量应该包含 47040000 值,但实际值为 26421864

内容来源: zalandoresearch/fashion-mnist