Tensorflow模型控制台错误:“错误:传递给'slice2d'的参数'x'必须是数字张量,但得到了字符串张量”

问题描述 投票:0回答:1

我试图在我的应用程序中使用基本的tensorflow模型,但遇到以下错误:

client.js?06a0:84错误:传递给'slice2d'的参数'x'必须是数字张量,但有字符串张量在vn(tf-core.esm.js?45ef:17)在gn(tf-core.esm.js?45ef:17)在slice2d_(tf-core.esm.js?45ef:17)在slice2d(tf-core.esm.js?45ef:17)在评估时(tf-layers.esm.js?271e:17)在评估时(tf-core.esm.js?45ef:17)在t.scopedRun(tf-core.esm.js?45ef:17)在t.tidy(tf-core.esm.js?45ef:17)在Ze(tf-core.esm.js?45ef:17)在sliceAlongFirstAxis(tf-layers.esm.js?271e:17)上vue__WEBPACK_IMPORTED_MODULE_22 __。default.config.errorHandler @client.js?06a0:84 globalHandleError @ vue.runtime.esm.js?2b0e:1870handleError @ vue.runtime.esm.js?2b0e:1839 invokeWithErrorHandling @vue.runtime.esm.js?2b0e:1862调用程序@ vue.runtime.esm.js?2b0e:2179original._wrapper @ vue.runtime.esm.js?2b0e:6917

我已经尝试过parseInt,但似乎没有什么不同。下面的代码:

<template>
  <div>
    <div class="train-controls">
      <h2 class="section col-sm-1">Training Data (x,y) pairs</h2>
      <div class="field-label">X</div><div class="field-label">Y</div>

      <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">+</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 an integer 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],
      yValues: [1,3,5,7],
      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>

<style>
.field, .field-label {
  height: 30px;
  padding: 0px 15px;
  float: left;
  width: 50%;
}

.field {
  border-radius: 0px 5px 5px 0px;
  border: 1px solid #eee;
  margin-bottom: 15px;
  height: 40px;
}

.col-sm-1:after {
    content: "";
    display: table;
    clear: both;
}

.section, .field-label {
  text-align: left;
  font-family: "Quicksand", "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; /* 1 */
  font-weight: 100;
}

.field-label {
  font-weight: 700;
}

.button-add-example {
  width: 100%;
  margin-bottom: 10px;
}

.button-train {
  width: 100%;
}

.predict-controls {
  padding-top: 30px;
  padding-bottom: 30px;
}

.predict-controls .element {
  width: 50%;
  display: block;
}

button {
  margin-top: 10px;
  font-family: "Quicksand", "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; /* 1 */
  font-weight: 700;
}

</style>
tensorflow vue.js nuxt.js
1个回答
0
投票

您的xValuesyValues被从对话中转换为字符串。

通过将其添加到train()方法的顶部,将它们重新转换为数字

      this.xValues = this.xValues.map(value=>{return parseInt(value)});
      this.yValues = this.yValues.map(value=>{return parseInt(value)});
© www.soinside.com 2019 - 2024. All rights reserved.