Vue.js “如何修复”;必须是数字张量,但得到字符串张量“;错误
嘿,这里我有一个问题,这段代码,我试图从教程。我一直在尝试创建一个简单的带有数字预测的机器学习代码Vue.js “如何修复”;必须是数字张量,但得到字符串张量“;错误,vue.js,tensorflow.js,Vue.js,Tensorflow.js,嘿,这里我有一个问题,这段代码,我试图从教程。我一直在尝试创建一个简单的带有数字预测的机器学习代码 <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=
<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>
增值
训练
预测
预测
从“@tensorflow/tfjs”导入*作为tf;
导出默认值{
数据(){
返回{
训练:错,
xvalue:[1,2,3,4,5,6],
Y值:[1,3,5,7,9,11],
预测值:'点击列车',
值预测:“”
}
},
方法:{
附加项(){
这个.xValues.push(0);
此.y值为.push(0);
},
列车(){
//定义线性回归的模型。
const model=this.model=tf.sequential();
add(tf.layers.dense({units:1,inputShape:[1]}));
//为培训准备模型:指定损失和优化器。
compile({loss:'meanSquaredError',优化器:'sgd'});
常数xs=tf.tensor2d(this.xValues,[this.xValues.length,1]);
常数ys=tf.tensor2d(this.yValues,[this.yValues.length,1]);
//使用数据训练模型。
fit(xs,ys,{epochs:50})。然后(()=>{
这是真的;
this.predictedValue='准备好进行预测';
});
},
预测{
//使用模型对模型以前从未见过的数据点进行推断:
this.predictedValue=this.model.predict(tf.tensor2d([this.valueToPredict],[1,1])).get(0,0);
}
}
}
我收到了此错误消息,但在VisualStudio中似乎一切正常
传递给“slice2d”的参数“x”必须是数值张量,但得到了字符串张量
呈现页面时出错。检查开发人员工具控制台以了解详细信息检查此行:
this.predictedValue = this.model.predict(tf.tensor2d([this.valueToPredict], [1, 1])).get(0, 0);
Tensorflow希望二维张量中的值是数值。当您从HTML输入字段读取值时,此.valueToPredict
将是一个字符串,因此会显示错误消息
只需使用
parseInt(this.valueToPredict)
将原始值转换为一个数字,例如一个整数,它应该可以工作。我只需使用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
嘿,很抱歉回答晚了,我已经用修改了密码;this.predictedValue=this.model.predict(tf.tensor2d([parseInt(this.valueToPredict)],[1,1])).get(0,0);但是什么都没做你能为你的用例创建一个提琴吗?我会看一看,你能不能先在不需要登录和注册的地方创建它?没有帐户我无法访问此页面。