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();
    });
edkeveked
  • 17,989
  • 10
  • 55
  • 93
Russell C.
  • 588
  • 6
  • 25
  • What is the meaning of `term_index`, `definition_start_index` and `definition_end_index` ? – edkeveked Aug 07 '18 at 04: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. – Russell C. Aug 07 '18 at 04:48
  • could you please explain what is the problem you're solving? – edkeveked Aug 07 '18 at 05:55

1 Answers1

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.

edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • Would that work even if I do not know what string will be inputted into the neural network until real-time? – Russell C. Aug 07 '18 at 04:45
  • 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 – edkeveked Aug 07 '18 at 06:02