Russell C.
Russell C.

Reputation: 608

How to use text as input in tensorflow.js neural network

When I try to train and test a tensorflow.js model, I get NaN as the output:

Tensor
[[NaN, NaN, NaN],
 [NaN, NaN, NaN]]

After doing some debugging, I have discovered that I am getting NaN as a result because I am attempting to use a string as the input. Here is an example of a json object that I would run through the neural network:

{
    "raw_sentence" : "Apple - a delicious, juicy red fruit",
    "term_index": 0,
    "definition_start_index": 2,
    "definition_end_index": 6
}

I am using raw_sentence as the input. Here is my code (the training data is assigned to variable "training" and the testing data is assigned to variable "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();
    });

Upvotes: 0

Views: 2707

Answers (1)

edkeveked
edkeveked

Reputation: 18401

You cannot use string to create a tensor. When the input is a string, you need to create a vector from your input. Consider the answer here.

Upvotes: 1

Related Questions