0

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();
    });
3
  • What is the meaning of term_index, definition_start_index and definition_end_index ? Commented Aug 7, 2018 at 4:35
  • @edkeveked The index of the term (e.g. "apple"), the index of the start of the definition, and the index of the end of the definition ("a delicious, juicy red fruit") respectively. Commented Aug 7, 2018 at 4:48
  • could you please explain what is the problem you're solving? Commented Aug 7, 2018 at 5:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Would that work even if I do not know what string will be inputted into the neural network until real-time?
The answer shows how to transform a string into a vector where the features are computed using the frequencies of the word of the string. It is a general use case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.