y=2x-1
<template>
<div class="home">
<div>
<el-input v-model="input" placeholder="请输入内容"></el-input>
<el-button plain @click="predictBtn">确定</el-button>
<span>Result:{
{ results }} </span>
</div>
</div>
</template>
<script>
import * as tf from '@tensorflow/tfjs';
var model = tf.sequential();
export default {
name: 'HomeView',
data() {
return {
input: '',
results:''
}
},
created() {
this.getModelFun();
},
methods: {
test() {
console.log('测试', [123, 23]);
},
getModelFun() {
let that=this;
// const shape=[2,3]
// const a= tf.tensor([1.0,2.0,3.0,10.0,20.0,30.0],shape)
// a.print();
// const b=tf.tensor([[1.0,2.0,3.0],[10.0,20.0,30.0]])
// b.print()
// const a= tf.scalar(3.14);
// a.print()
// const b= tf.tensor2d([[2,3,4],[5,6,7]])
// b.print();//输出二维张量
// const c=tf.tensor3d([[[1,2,3],[3,4,5]]])
// c.print()
// const a = tf.ones([2,3])
// a.print()
// const inita=tf.zeros([5])
// inita.print()
// const biases= tf.variable(inita)
// biases.print()
// const updata= tf.tensor1d([0,1,2,3,4])
// biases.assign(updata)
// biases.print()
//---创建模型的2种方法1.layers 2.core
model.add(tf.layers.dense({ units:1, inputShape: [1] }));
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'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1])
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
model.predict(tf.tensor2d([5], [1, 1])).print();
});
},
predictBtn(){
let val=parseInt(this.input);
this.results= model.predict(tf.tensor2d([val], [1, 1]));
}
}
}
</script>
<style lang="scss">
.home{
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
&>div{
width: 150px;
}
}
</style>