当我尝试训练和测试tensorflow.js模型时,我得到NaN作为输出:
Tensor
[[NaN, NaN, NaN],
[NaN, NaN, NaN]]
进行一些调试之后,我发现得到的结果是NaN,因为我试图使用字符串作为输入。这是我将通过神经网络运行的json对象的示例:
{
"raw_sentence" : "Apple - a delicious, juicy red fruit",
"term_index": 0,
"definition_start_index": 2,
"definition_end_index": 6
}
我正在使用raw_sentence
作为输入。这是我的代码(将训练数据分配给变量“ training”,将测试数据分配给变量“ testing”):
const trainingData = tf.tensor2d(training.map(item => [
item.raw_sentence,
]));
const outputData = tf.tensor2d(training.map(item => [
item.term_index,
item.definition_start_index,
item.definition_end_index
]));
const testingData = tf.tensor2d(testing.map(item => [
item.raw_sentence
]));
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [1],
activation: "softplus",
units: 2,
}));
model.add(tf.layers.dense({
inputShape: [2],
activation: "softplus",
units: 3,
}));
model.add(tf.layers.dense({
activation: "softplus",
units: 3,
}));
model.compile({
loss: "meanSquaredError",
optimizer: tf.train.adam(.06),
});
const startTime = Date.now();
model.fit(trainingData, outputData, {epochs: 12})
.then((history) => {
console.log(history);
console.log("Done training in " + (Date.now()-startTime) / 1000 + " seconds.");
model.predict(testingData).print();
});