建筑装饰和网站建设哪个好,常州市新北区城乡建设局网站,注册公司网页,仿造网站用侵权吗在 TensorFlow 2.0 及之后的版本中#xff0c;默认采用 Eager Execution 的方式#xff0c;不再使用 1.0 版本的 Session 创建会话。Eager Execution 使用更自然地方式组织代码#xff0c;无需构建计算图#xff0c;可以立即进行数学计算#xff0c;简化了代码调试的过程。… 在 TensorFlow 2.0 及之后的版本中默认采用 Eager Execution 的方式不再使用 1.0 版本的 Session 创建会话。Eager Execution 使用更自然地方式组织代码无需构建计算图可以立即进行数学计算简化了代码调试的过程。本文主要介绍 TensorFlow 的基本用法通过构建一个简单损失函数介绍 TensorFlow 优化损失函数的过程。 目录
1 tf.Tensor
2 tf.Variable
3 tf.GradientTape TensorFlow 是一个用于机器学习的端到端平台。它支持以下内容
基于多维数组的数值计算类似于 NumPyGPU 和分布式处理自动微分模型构建、训练和导出 1 tf.Tensor TensorFlow 用 tf.Tensor 对象处理多维数组或张量以下是一个 2 维张量例子
import tensorflow as tf
x tf.constant([[1., 2., 3.],[4., 5., 6.]])
print(x)
tf.Tensor( [[1. 2. 3.] [4. 5. 6.]], shape(2, 3), dtypefloat32) tf.Tensor 对象最重要的属性是 shape 与 dtype:
Tensor.shape 返回张量每个维度的大小Tensor.dtype 返回张量中元素的数据类型 print(x.shape)
(2, 3) print(x.dtype)
dtype: float32 TensorFlow 实现了张量的标准数学运算同时也包括为机器学习定制的运算。以下是一些示例
x x
5 * x
tf.transpose(x)
tf.nn.softmax(x, axis-1)
tf.reduce_sum(x) 2 tf.Variable 在 TensorFlow 中模型的权重用 tf.Variable 对象存储称为变量。 import tensorflow as tfx tf.Variable([0., 0., 0.])
x.assign([1, 2, 3])
tf.Variable UnreadVariable shape(3,) dtypefloat32, numpyarray([1., 2., 3.], dtypefloat32) tf.Variable 对象的数值可以改变在 TensorFlow 2.0 中不再使用 Session 启动计算变量可以直接算出结果。
x.assign_add([1, 1, 1,])
tf.Variable UnreadVariable shape(3,) dtypefloat32, numpyarray([2., 3., 4.], dtypefloat32) x.assign_sub([1, 1, 1])
tf.Variable UnreadVariable shape(3,) dtypefloat32, numpyarray([1., 2., 3.], dtypefloat32) 3 tf.GradientTape 梯度下降法与相关算法是现在机器学习的基础。TensorFLow 实现了自动微分来计算梯度通常用于计算机器学习模型的损失函数的梯度。 TensorFlow 2.0 提供了 tf.GradientTape 对象可以理解为“梯度流”顾名思义tf.GradientTape 是用来计算梯度用的。 以下是一个简单的示例
import tensorflow as tfdef f(x):return x**2 2*x - 5x tf.Variable(1.0)with tf.GradientTape() as tape:y f(x)
g_x tape.gradient(y, x) # 计算 y 在 x 1.0 处的梯度
print(g_x)
4.0 最后构建一个简单损失函数并使用 TensorFlow 计算最小值。
import tensorflow as tfdef loss(x):return x**2 - 10*x 25x tf.Variable(1.0) # 随机初始值losses [] # 记录损失函数值
for i in range(100):with tf.GradientTape() as tape:one_loss loss(x)lossed.append(one_loss)grad tape.gradient(one_loss, x)x.assign_sub(0.1 * grad) # 执行一次梯度下降法print(The mininum of loss function is: )
tf.print(x)
The mininum of loss function is:
4.99999905 # 可视化优化过程
import matplotlib
from matplotlib import pyplot as pltmatplotlib.rcParams[figure.figsize] [8, 5]plt.figure()
plt.plot(losses)
plt.title(Loss vs training iterations)
plt.xlabel(iterations)
plt.ylabel(loss) 文章转载自: http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.kongpie.com.gov.cn.kongpie.com http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.zzjpy.cn.gov.cn.zzjpy.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn