tensorflowjs的机器学习案例

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值