如何在tensorflow.js神经网络中使用文本作为输入

时间:2018-08-07 03:57:28

标签: javascript tensorflow.js

当我尝试训练和测试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();
    });

1 个答案:

答案 0 :(得分:0)

您不能使用字符串创建张量。当输入为字符串时,您需要根据输入创建向量。考虑答案here