首页 > 解决方案 > What does the error 'The feature data generated by the dataset lacks the required input key' mean in tensorflow js?

问题描述

What does the error Uncaught (in promise) Error: The feature data generated by the dataset lacks the required input key 'dense_Dense1_input'. mean? I tried different things to solve this, such as different input shapes and different batch size, but nothing seems to work. I have a data input with 484 features and 30 rows, and a label set with 1 column and 30 rows.

The exact error is:

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)

My code

<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>

标签: javascriptcsvtensorflow.js

解决方案


The isLabel property should not be used in the labelDataset since the data is zipped after. This will created a nested object for the label. If it has to be used, then the operator map neeeds to be used to retrieve only the ys property of the 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);

The flattenedDataset can then be used for training.

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);
      }
    }
  });
})()
<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>


</script>
</body>
</html>


推荐阅读