《简单粗暴TensorFlow2.0》—学习笔记

编程入门 行业动态 更新时间:2024-10-27 07:16:46

《简单<a href=https://www.elefans.com/category/jswz/34/1720115.html style=粗暴TensorFlow2.0》—学习笔记"/>

《简单粗暴TensorFlow2.0》—学习笔记

文章目录

  • 基础
    • 安装和环境配置
    • TensorFlow基础
    • TensorFlow模型建立与训练
      • 模型(Model)与层(Layer)
      • 多层感知机(MLP)
            • 信息论
            • 机器学习中交叉熵的应用
      • 卷积神经网络(CNN)
      • 循环神经网络(RNN)
      • 深度强化学习(DRL)
      • Keras Pipeline*
      • 自定义层、损失函数和评估指标*
    • TensorFlow 常用模块
      • tf.train.Checkpoint:变量的保存与恢复
      • TensorBoard:训练过程可视化
        • Permission denied: '/tmp/.tensorboard-info/pid-30349.info'
      • tf.data:数据的构建与预处理
      • TFRecord:TensorFlow数据集存储格式
      • @tf.function:Graph Execution模式 *
      • tf.TensorArray:TensorFlow动态数组 *
      • tf.config:GPU的使用与分配 *
  • 部署
    • TensorFlow模型导出
    • TensorFlow Serving
    • TensorFlow Lite
    • TensorFlow in JavaScript
  • 大规模训练与加速
    • TensorFlow分布式训练
    • 使用TPU训练TensorFlow模型
  • 扩展
    • TensorFlow Hub模型复用
    • TensorFlow Datasets数据集载入

  • 中文版手册:简单粗暴 TensorFlow 2.0 | A Concise Handbook of TensorFlow 2.0
  • github:snowkylin/tensorflow-handbook
    TensorFlow 2.0是基于Keras和Eager Execution(即时运行)模式。

基础

安装和环境配置

  • pycharm

TensorFlow基础







TensorFlow模型建立与训练

模型(Model)与层(Layer)






多层感知机(MLP)








交叉熵(cross entropy)用来求目标与预测值之间的差距。

信息论
  • 信息量

  • 相对熵(KL散度)
    相对熵又称KL散度,如果我们对于同一个随机变量 x 有两个单独的概率分布 P(x) 和 Q(x),我们可以使用 KL 散度(Kullback-Leibler (KL) divergence)来衡量这两个分布的差异。
  • 交叉熵
机器学习中交叉熵的应用
  • 单分类问题中的应用

  • 多分类问题中的应用

卷积神经网络(CNN)







循环神经网络(RNN)




深度强化学习(DRL)




Keras Pipeline*



自定义层、损失函数和评估指标*


TensorFlow 常用模块

tf.train.Checkpoint:变量的保存与恢复










TensorBoard:训练过程可视化





Permission denied: ‘/tmp/.tensorboard-info/pid-30349.info’


tf.data:数据的构建与预处理














TFRecord:TensorFlow数据集存储格式





@tf.function:Graph Execution模式 *









tf.TensorArray:TensorFlow动态数组 *


tf.config:GPU的使用与分配 *





部署

TensorFlow模型导出











TensorFlow Serving








TensorFlow Lite


















TensorFlow in JavaScript














<html><head><script src="/@tensorflow/tfjs"></script><script src="/@tensorflow-models/mobilenet"> </script>
</head><video width=400 height=300></video>
<p></p>
<img width=400 height=300 /><script>const video = document.querySelector('video')const image = document.querySelector('img')const status = document.querySelector("p")const canvas = document.createElement('canvas')const ctx = canvas.getContext('2d')let modelmain()async function main () {status.innerText = "Model loading..."model = await mobilenet.load()status.innerText = "Model is loaded!"const stream = await navigator.mediaDevices.getUserMedia({ video: true })video.srcObject = streamawait video.play()canvas.width = video.videoWidthcanvas.height = video.videoHeightrefresh()}async function refresh(){ctx.drawImage(video, 0,0)image.src = canvas.toDataURL('image/png')await model.load()const predictions = await model.classify(image)const className = predictions[0].classNameconst percentage = Math.floor(100 * predictions[0].probability)status.innerHTML = percentage + '%' + ' ' + classNamesetTimeout(refresh, 100)}</script></html>




<html>
<head><script src="/@tensorflow/tfjs/dist/tf.min.js"></script><script>const xsRaw = tf.tensor([2013, 2014, 2015, 2016, 2017])const ysRaw = tf.tensor([12000, 14000, 15000, 16500, 17500])// 归一化const xs = xsRaw.sub(xsRaw.min()).div(xsRaw.max().sub(xsRaw.min()))const ys = ysRaw.sub(ysRaw.min()).div(ysRaw.max().sub(ysRaw.min()))const a = tf.scalar(Math.random()).variable()const b = tf.scalar(Math.random()).variable()// y = a * x + b.const f = (x) => a.mul(x).add(b)const loss = (pred, label) => pred.sub(label).square().mean()const learningRate = 1e-3const optimizer = tf.train.sgd(learningRate)// 训练模型for (let i = 0; i < 10000; i++) {optimizer.minimize(() => loss(f(xs), ys))}// 预测console.log(`a: ${a.dataSync()}, b: ${b.dataSync()}`)const preds = f(xs).dataSync()const trues = ys.arraySync()preds.forEach((pred, i) => {console.log(`x: ${i}, pred: ${pred.toFixed(2)}, true: ${trues[i].toFixed(2)}`)})</script>
</head>
</html>



大规模训练与加速

TensorFlow分布式训练










使用TPU训练TensorFlow模型










扩展

TensorFlow Hub模型复用










TensorFlow Datasets数据集载入


参考资料
一文搞懂交叉熵在机器学习中的使用,透彻理解交叉熵背后的直觉
tensorboard 提示权限不足

更多推荐

《简单粗暴TensorFlow2.0》—学习笔记

本文发布于:2024-03-08 06:48:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1720108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:粗暴   学习笔记   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!