tensorflow.js 19 basic ohisama1 关注 2018-04-16 12:43:23 许可证: MIT许可证 派生0 收藏0 浏览量105 播放 停止 刷新 全屏查看 智能机大小查看 Readme JavaScript 54 行 HTML 4 行 CSS 10 行 tensorflow.js 19 basic const xs = tf.tensor1d([0, 1, 2, 3]); const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]); const a = tf.scalar(Math.random()).variable(); const b = tf.scalar(Math.random()).variable(); const c = tf.scalar(Math.random()).variable(); //const f = x => a.mul(x.square()).add(b.mul(x)).add(c); function func(x) { return a.mul(x.square()).add(b.mul(x)).add(c); } //const loss = (pred, label) => pred.sub(label).square().mean(); function loss(pred, label) { //return pred.sub(label).square().mean(); return tf.losses.softmaxCrossEntropy(pred, label).mean(); } const learningRate = 0.01; const optimizer = tf.train.sgd(learningRate); for (let i = 0; i < 10; i++) { const cost = optimizer.minimize(() => loss(func(xs), ys), true); alert(cost); } /* function loss(y, x) { return tf.losses.softmaxCrossEntropy(y, x).mean(); } function model(x) { return x.as2D(-1, W1.shape[0]).matMul(W1).add(b1); } const optimizer = tf.train.sgd(0.1); const W1 = tf.variable(tf.randomNormal([4, 20], 0, 1)); const b1 = tf.variable(tf.zeros([20])); const xs = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]); const ys = tf.tensor2d([[1], [1], [0], [0]], [4, 1]); alert(xs); alert(ys); for (let i = 0; i < 100; i++) { const cost = optimizer.minimize(() => { return loss(ys, model(xs)); }, true); tf.nextFrame(); } alert(cost); */ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.8.0"></script> tensorflow.js 19 basic * { margin: 0; padding: 0; border: 0; } body { background: #ffd; font: 30px sans-serif; } const xs = tf.tensor1d([0, 1, 2, 3]); const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]); const a = tf.scalar(Math.random()).variable(); const b = tf.scalar(Math.random()).variable(); const c = tf.scalar(Math.random()).variable(); //const f = x => a.mul(x.square()).add(b.mul(x)).add(c); function func(x) { return a.mul(x.square()).add(b.mul(x)).add(c); } //const loss = (pred, label) => pred.sub(label).square().mean(); function loss(pred, label) { //return pred.sub(label).square().mean(); return tf.losses.softmaxCrossEntropy(pred, label).mean(); } const learningRate = 0.01; const optimizer = tf.train.sgd(learningRate); for (let i = 0; i < 10; i++) { const cost = optimizer.minimize(() => loss(func(xs), ys), true); alert(cost); } /* function loss(y, x) { return tf.losses.softmaxCrossEntropy(y, x).mean(); } function model(x) { return x.as2D(-1, W1.shape[0]).matMul(W1).add(b1); } const optimizer = tf.train.sgd(0.1); const W1 = tf.variable(tf.randomNormal([4, 20], 0, 1)); const b1 = tf.variable(tf.zeros([20])); const xs = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]); const ys = tf.tensor2d([[1], [1], [0], [0]], [4, 1]); alert(xs); alert(ys); for (let i = 0; i < 100; i++) { const cost = optimizer.minimize(() => { return loss(ys, model(xs)); }, true); tf.nextFrame(); } alert(cost); */ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.8.0"></script> * { margin: 0; padding: 0; border: 0; } body { background: #ffd; font: 30px sans-serif; } 请使用支持内嵌框架的浏览器 到jsdo.it games上查看 作者信息 分享 嵌入博客 QR二维码 标签 下载 成功! 描述 是怎样的游戏? 控制设备 智能机遥控器 jsdo.it WebSocket Controller» 鼠标 键盘 触摸设备 全屏 有效 无效 从jsdo.it games上删除 提交 作者信息 ohisama1 Tweet 默认面板 自动播放 截图 Readme JavaScript HTML CSS 部件尺寸 宽度: px 高度: px 部件代码 <script type="text/javascript" src="http://jsdo.it/blogparts/WyrA/js"></script> application 讨论区 试着提些关于代码的问题! 标签 application