《TensorFlow Machine Learning Cookbook》--1.5

本文深入讲解TensorFlow中的各种运算操作,包括加、减、乘、除等基础运算,以及abs、ceil、cos等高级数学函数,同时介绍了div、truediv、floordiv等特定除法运算,并探讨了如何通过组合基本运算构建复杂函数。
部署运行你感兴趣的模型镜像

声明运算(或者说操作,operations)

现在我们必须学习可以添加到Tensorflow计算图中的其他操作了。

 

准备工作

除了标准的算法运算,Tensorflow还给我们提供了更多运算,我们应该了解掌握。在继续往下进行之前,应该知道如何使用它们。跟之前一样,可以运行下面的代码来创建一个图会话:

import tensorflow as tf
sess = tf.Session()

 

如何做...

 

Tensorflow对张量提供了标准运算:add(), sub(), mul() 和div()。需要注意的是,这个章节中的所有运算都是将输入按元素进行计算求值,除非有其他特别说明:

1.Tensorflow提供了几个关于div()的变体及相关函数。

2.值得一提的是div()返回(张量)的类型和输入是一样的。这意味着,如果输入是整数,它返回除法商的“取地板”(floor)值(类似于Python2中的对应除法);要返回Python3版本的类型,Tensorflow使用函数 truediv() ,在除之前将输入的整数强制转换成浮点型,最后返回浮点数。如下代码所示:

print(sess.run(tf.div(3, 4)))
# 0
print(sess.run(tf.truediv(3, 4)))
# 0.75

3.如果我们使用的是浮点型数,想进行整数除法,可以使用函数 floordir() . 此函数返回一个浮点型数,但会四舍五入到最近的整数。函数使用方法见下面代码:

print(sess.run(tf.floordiv(3.0, 4.0)))
# 0.0

4.另外一个重要的函数是 mod() ,它返回除法运算后的余数。看下面代码:

print(sess.run(tf.mod(22.0, 5.0)))
# 2.0

5.两个张量之间的向量叉积由函数 cross() 完成,向量叉积只对两个三维向量有定义,所以它只接收两个三维的张量。如下代码所示:

printf(sess.run(tf.cross([1., 0., 0.], [0., 1., 0.])))
# [0.   0.   1.0]

6.下面给出一个其他常用函数的表格,这些函数都是进行元素级别的运算。

abs()输入张量的绝对值 
ceil()输入张量的天花板函数
cos()输入张量的余弦函数
exp()输入张量的e为底的指数函数
floor()输入张量的地板函数
inv()输入张量的倒数(即1/x)
log()输入张量的自然对数函数
maxinum()两个张量中按元素比较最大值函数
minimun()两个张量中按元素比较最小值函数
neg()输入张量的负数
pow()第一个张量的第个二量的幂次方
round()输入张量的四舍五入值
rsqrt()输入张量的平方根之倒数
sign()输入张量的符号函数
sin()输入张量的正弦函数
sqrt()输入张量的平方根
square()输入张量的平方
  

7.专用数学函数:以下是一些专用于机器学习的函数,这里特别提一下;Tensorflow为它们提供了一些内部函数。再次强调,这些函数进行元素级别的操作,除非有特别说明:

digamma()Psi函数,lgamma()函数的导数
erf()张量的高斯误差函数,元素级别的
erfc()张量的互补误差函数
igamma()低正则化不完全误差函数
igammac()高正则化不完全误差函数
lbeta()beta函数的绝对值的自然对数
lgamma()gamma函数的绝对值的自然对数
squared_difference()两个张量之间的方差

 

如何工作...

 

明白什么样的函数可以让我们添加到计算图中,这点很重要。大多数情况下,我们会关心前面提到的函数。我们也可以将前面的函数进行组合,进而生成许多不同的定制函数,例如:

# Tangent function (tan(pi/4)=1)
print(sess.run(tf.div(tf.sin(3.1416/4.), tf.cos(3.1416/4.))))
# 1.0

 

更多...

 

如何希望给计算图添加一些这儿没有列出的运算,我们则可以基于前面的这些函数创建自己的运算。下面的例子就是把前面没有列出的运算增加到计算图中,添加了多项式3x^{2}-x+10:

def custom_polynomial(value):
    return(tf.sub(3 * tf.square(value), value) + 10)
print(sess.run(custom_polynomial(11)))
# 362

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

Explore machine learning concepts using the latest numerical computing library — TensorFlow — with the help of this comprehensive cookbook About This Book Your quick guide to implementing TensorFlow in your day-to-day machine learning activities Learn advanced techniques that bring more accuracy and speed to machine learning Upgrade your knowledge to the second generation of machine learning with this guide on TensorFlow Who This Book Is For This book is ideal for data scientists who are familiar with C++ or Python and perform machine learning activities on a day-to-day basis. Intermediate and advanced machine learning implementers who need a quick guide they can easily navigate will find it useful. What You Will Learn Become familiar with the basics of the TensorFlow machine learning library Get to know Linear Regression techniques with TensorFlow Learn SVMs with hands-on recipes Implement neural networks and improve predictions Apply NLP and sentiment analysis to your data Master CNN and RNN through practical recipes Take TensorFlow into production In Detail TensorFlow is an open source software library for Machine Intelligence. The independent recipes in this book will teach you how to use TensorFlow for complex data computations and will let you dig deeper and gain more insights into your data than ever before. You’ll work through recipes on training models, model evaluation, sentiment analysis, regression analysis, clustering analysis, artificial neural networks, and deep learning – each using Google’s machine learning library TensorFlow. This guide starts with the fundamentals of the TensorFlow library which includes variables, matrices, and various data sources. Moving ahead, you will get hands-on experience with Linear Regression techniques with TensorFlow. The next chapters cover important high-level concepts such as neural networks, CNN, RNN, and NLP. Once you are familiar and comfortable with the TensorFlow ecosystem, the last chapter will show you how to take it to production. Style and approach This book takes a recipe-based approach where every topic is explicated with the help of a real-world example.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值