我正在尝试使用ML5库进行分类,在我正在构建的React应用程序中进行分类。
我的浏览器Error: You are passing a target array of shape 11342,1 while using a loss 'categorical_crossentropy'. 'categorical_crossentropy'expects targets to be binary matrices (1s and 0s) of shape [samples, classes].中有以下错误
在引发此错误的几个Github问题中,解释是That error indicates you have just 1 type of objects in your dataset. You must have 2 or more different object classes in your dataset.,即这里和这里链接中的解释。
我不知道这意味着什么。我的数据中有6个输入和2个输出。我的输入将会是这样的
let inputs = {
male: 1,
female: 0,
dob: 641710800000,
// have more, but keeping it simple for this example...
}我的输出将会是这样的
let output = {
job: 1 // or 0, if they have a job or not, for example. i.e., two possible outputs
}然而,我仍然会发现错误。有人能帮我弄明白为什么怎么修吗?
下面是我的代码:
people_arr = json.voters_arr;
keys = ["male", "female", "dob"];
let model_options = {
inputs: keys,
outputs: ["job"],
task: "classification"
};
let model = ml5.neuralNetwork(model_options);
for (let person of people_arr) {
let inputs = {
male: person.male,
female: person.female,
dob: person.dob
};
let output = {};
output.job = person.job; // either 0 or 1
model.addData(inputs, output);
}
model.normalizeData();
let train_options = { epochs: 100 }
model.train(train_options, whileTraining); // <-- error happening here
.then(() => {
console.log("pre classify");
return model.classify(new_person_arr);
})
.then((err, results) => {
if (err) { console.log("error") }
else {
let new_arr = results.splice(100);
console.log("results : ", new_arr);
setValues({...values, results: new_arr })
}
})
.catch((err) => { console.log("err : ", err) });发布于 2020-02-13 00:45:56
Categorical cross-entropy期望一个热向量作为标签,而不是一个数字.例如,假设有三个人:Michael、Jim和Dwight。Michael和Jim都有工作,而Dwight没有。假设没有工作就会把你归入0类别,而有一份工作会把你归入1类别。在这种情况下,标签应该如下所示:
[[0,1], # Michael's label
[0,1], # Jim's label
[1,0]] # Dwight's labelMichael和Jim属于1类,因此它们在索引1处有1,在所有其他索引上有0。Dwight属于0类,所以他在索引0处有一个1,在所有其他索引上都有一个0。
如果您想使用单个数字作为标签(即0或1),则应该使用sparse categorical cross-entropy。Sparse categorical cross-entropy为每个示例取一个整数作为标签,并假定存在从0到它所看到的最高值整数的类别。所以它会很好地与你已经拥有的工作。
https://stackoverflow.com/questions/60198845
复制相似问题