zoukankan      html  css  js  c++  java
  • 身高体重预测

    <script src="js/tf.js">
    </script>
    <script src="js/vis.js"></script>
    <script>
        window.onload = async() => {
            const heights = [150, 160, 170];
            const weights = [40, 50, 60];

            tfvis.render.scatterplot({
                name: '身高体重训练数据'
            }, {
                values: heights.map((x, i) => ({
                    x,
                    y: weights[i]
                }))
            }, {
                xAxisDomain: [140, 180],
                yAxisDomain: [30, 70]
            });
            // 进行数据归一化
            const inputs = tf.tensor(heights).sub(150).div(20);
            const labels = tf.tensor(weights).sub(40).div(20);
            // 简历连续性模型
            const model = tf.sequential();
            // 添加全连接层
            model.add(tf.layers.dense({
                // 神经元个数
                units: 1,
                // 输入数据的形状
                inputShape: [1]
            }));
            // 设置损失函数和优化器
            model.compile({
                loss: tf.losses.meanSquaredError,
                // 随机梯度下降法
                optimizer: tf.train.sgd(0.1)
            });

            await model.fit(inputs, labels, {
                batchSize: 3, //批量数据
                epochs: 200,
                callbacks: tfvis.show.fitCallbacks({
                    name: '训练过程'
                }, ['loss'])
            });

            const output = model.predict(tf.tensor([180]).sub(150).div(20));
            alert(`如果身高为 180cm,那么预测体重为 ${output.mul(20).add(40).dataSync()[0]}kg`);
        }
    </script>
  • 相关阅读:
    Webfunny Js错误分析讲解
    Webfunny漏斗分析功能讲解
    Webfunny自定义埋点功能讲解
    Webfunny连线用户功能讲解
    Webfunny用户细查功能讲解
    C语言打印数字前补0
    github上新晋star3K的开源AI模型,包含情感分析等
    IT系统架构的演化
    微服务架构与SOA架构的区别与联系
    开源的分布式事务-Seata的设计原理
  • 原文地址:https://www.cnblogs.com/liuhao-web/p/13711054.html
Copyright © 2011-2022 走看看