• 身高体重预测


    <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>
  • 相关阅读:
    QSet<T>自定义类型需要定义==和qHash()函数
    《左耳听风》-ARTS-打卡记录-第十三周
    Windows中对窗口进行剪切
    Markdown 编写规范
    【洛谷 P1033】自由落体
    【GOJ 3032】司愁之路
    动态规划基础 3-解题报告
    前缀、中缀、后缀互相转换
    【GOJ 3015】疯狂外星人
    【GOJ 3010】有趣的数
  • 原文地址:https://www.cnblogs.com/liuhao-web/p/13711054.html
Copyright © 2020-2023  润新知