javascript - 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>
解决方案
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>
推荐阅读
- timer - 我们如何检测重复访问者 IP 并在液体中创建条件?
- java - 使用 Apache Commons 读取 CSV 文件
- docker - Windows10 上的 Docker:没有匹配的清单未知?
- reactjs - React-router、Switch 和 Redux:Route 莫名其妙不渲染,没有报错
- sql - 百万条记录的 SQL 查询优化
- selenium - Selenium Grid:具有许多实例的节点与每个节点的实例
- iis - 是否可以将 DebugDiag 配置为不为计划的 IIS 应用程序池回收生成转储?
- ios - 如何将数据从模态呈现的 ViewController 传递到 Xamarin.iOS 中的另一个 ViewController?
- python - 通过 AWS Athena 查询结果分页时如何跳过标题
- sql - 获取存储在 varchar2 列中的 XML 标记数据