0
votes

hey there I am having a problem with this code that I am trying from the tutorial. I have been trying to create a simple machine learning code with number prediction.

  <div v-for="(item, index) in xValues" v-bind:key="index">
        <div>

          <div class="col-sm-1">
            <input class="field field-x" v-model="xValues[index]" type="number">
            <input class="field field-y" v-model="yValues[index]" type="number">
          </div>
      </div>
      </div>

      <button class="button-add-example button--green" v-on:click="addItem">Add Value</button>
      <button class="button-train button--green" v-on:click="train">Train</button>
    </div>

    <div class="predict-controls">
      <h2 class="section col-sm-1">Predicting</h2>
      <input class="field element" v-model="valueToPredict" type="number" placeholder="Enter a number"><br>
      <div class="element" {{predictedValue}}></div>
      <button class="element button--green" v-on:click="predict" :disabled="!trained">Predict</button>
    </div>
  </div>
</template>

<script>
import * as tf from '@tensorflow/tfjs';
export default {
  data() {
    return {
      trained: false,
      xValues: [1,2,3,4,5,6],
      yValues: [1,3,5,7,9,11],
      predictedValue:'Click on train',
      valueToPredict: ''
    }
  },
  methods: {
    addItem() {
      this.xValues.push(0);
      this.yValues.push(0);
    },
    train() {
      // Define a model for linear regression.
      const model = this.model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));
      // Prepare the model for training: Specify the loss and the optimizer.
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
      const xs = tf.tensor2d(this.xValues, [this.xValues.length, 1]);
      const ys = tf.tensor2d(this.yValues, [this.yValues.length, 1]);
      // Train the model using the data.
      model.fit(xs, ys, {epochs: 50}).then(() => {
        this.trained = true;
        this.predictedValue = 'Ready for making predictions';
      });
    },
    predict() {
      // Use the model to do inference on a data point the model hasn't seen before:
      this.predictedValue = this.model.predict(tf.tensor2d([this.valueToPredict], [1, 1])).get(0, 0);
    }
  }
}
</script>

I got this error message but everyting seems fine in visual studio

Argument 'x' passed to 'slice2d' must be numeric tensor, but got string tensor An error occurred while rendering the page. Check developer tools console for details

2

2 Answers

0
votes

Check this line:

this.predictedValue = this.model.predict(tf.tensor2d([this.valueToPredict], [1, 1])).get(0, 0);

Tensorflow expects the values inside a 2d tensor to be numeric. As you are reading the value from an HTML input field this.valueToPredict is going to be a string hence the error message.

Simply convert your raw value to a number, for example an integer using parseInt(this.valueToPredict) and it should work.

0
votes

I was able to resolve this error by simply using parseInt.

   let val = parseInt(document.getElementById('inputValue').value);
   console.log(tf.tensor2d([val],[1,1]));
   document.getElementById('output').innerText = model.predict(tf.tensor2d([val],[1,1]));
});

Output for Console Log:
dataId: {}
dtype: "float32"
id: 35515
isDisposed: (...)
isDisposedInternal: false
kept: false
rank: (...)
rankType: "2"
shape: (2) [1, 1]
size: 1
strides: [1]
__proto__: Object