张量流js中的错误``由数据集生成的要素数据缺少所需的输入键''是什么意思?

时间:2020-02-13 18:46:47

标签: javascript csv tensorflow.js

错误未捕获(保证)错误是什么?错误:由数据集生成的要素数据缺少必需的输入键'dense_Dense1_input'。是什么意思?我尝试了不同的方法来解决此问题,例如不同的输入形状和不同的批处理大小,但似乎没有任何效果。我有一个具有484个要素和30行的数据输入,以及一个具有1列和30行的标签集。

确切的错误是:

Uncaught (in promise) Error: The feature data generated by the dataset lacks the required input key 'dense_Dense1_input'.
    at new e (errors.ts:48)
    at Wd (training_dataset.ts:277)
    at Pd (training_dataset.ts:222)
    at training_dataset.ts:421
    at common.ts:14
    at Object.next (common.ts:14)
    at o (common.ts:14)

我的代码

<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.5.2/dist/tf.min.js"></script>
<title>test</title>             
</head>
<body>
<script>

const csvUrlData = '/image_data.csv';
const csvUrlLabel = '/number_data.csv';
const headers_image = Array.from(Array(484).keys());
const headers_image_string = headers_image.map(String);

async function run() {
  const csvDataset = tf.data.csv(
    csvUrlData,{
        hasHeader: false,
        columnNames: headers_image_string
    });
  const csvLabelset = tf.data.csv(
    csvUrlLabel, {
        columnConfigs: {
            label_numbers: {
                isLabel: true
            }
        }
    }
  );

  const flattenedDataset = tf.data.zip({xs: csvDataset, ys: csvLabelset}).batch(5);

  const model = tf.sequential();
  model.add(tf.layers.dense({
    inputShape: [484],
    units: 1
  }));
  model.compile({
    optimizer: tf.train.sgd(0.00000001),
    loss: 'meanSquaredError'
  });

  return await model.fitDataset(flattenedDataset, {
    epochs: 10,
    callbacks: {
      onEpochEnd: async (epoch, logs) => {
        console.log(epoch + ':' + logs.loss);
      }
    }
  });
}
run();
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

isLabel属性不应在labelDataset中使用,因为压缩后的数据。这将为标签创建一个嵌套对象。如果必须使用它,则必须使用运算符map仅检索ys的{​​{1}}属性。

labelDataset

然后 const csvDataset = tf.data.csv( csvUrlData,{ hasHeader: false, columnNames: headers_image_string }); const csvLabelset = tf.data.csv( csvUrlLabel, { columnConfigs: { label_numbers: { isLabel: true } } } ); const flattenedcsvDataset = csvDataset .map((data) => { return Object.values(data) }) const flattenedcsvLabelset = csvDataset .map((data) => { return Object.values(data) }) const flattenedDataset = tf.data.zip({xs: flattenedcsvDataset, ys: flattenedcsvLabelset}).batch(5); 可用于训练。

flattenedDataset
const csvUrl =
'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';

(async function run() {
 
  const csvDataset = tf.data.csv(
    csvUrl, {
      columnConfigs: {
       /* medv: {
          isLabel: true
        }*/
      }
    });

  // Number of features is the number of column names minus one for the label
  // column.
  const numOfFeatures = (await csvDataset.columnNames()).length ;

  // Prepare the Dataset for training.
  const flattenedDataset =
    csvDataset
    .map((data) =>
      {
      return Object.values(data)
      })
  
  const zip = tf.data.zip({xs: flattenedDataset, ys: flattenedDataset}).batch(10)
  
  // Define the model.
  const model = tf.sequential();
  model.add(tf.layers.dense({
    inputShape: [numOfFeatures],
    units: numOfFeatures
  }));
  model.compile({
    optimizer: tf.train.sgd(0.000001),
    loss: 'meanSquaredError'
  });

  // Fit the model using the prepared Dataset
  return model.fitDataset(zip, {
    epochs: 10,
    callbacks: {
      onEpochEnd: async (epoch, logs) => {
        console.log(epoch + ':' + logs.loss);
      }
    }
  });
})()