《TensorFlow Machine Learning Cookbook》--1.2

6 篇文章 0 订阅
5 篇文章 0 订阅
本文详细介绍了在 TensorFlow 中创建和使用张量的方法,包括固定大小的张量、相似形状的张量、序列张量和随机张量。通过实例展示了如何声明变量和占位符,并解释了张量在计算图中的作用。此外,还提到了将 numpy 数组转换为张量以及在函数中使用张量进行参数传递的技巧。
摘要由CSDN通过智能技术生成

声明张量(Tensor)
张量是TensorFlow操作计算图(computational graph)时使用的主要的数据结构;我们可以把这些张量声明为变量,或者占位符(placeholder)以便喂入(feed)数据.首先必须知道如何创建张量.


准备工作


当我们创建一个张量并把它声明为一个变量时,TensorFlow在我们的计算图中创建了几个图结构;很有必要指出:创建张量时,TensorFlow并没有往计算图中加入任何东西,只有在根据张量创建变量之后TensorFlow才做这些工作.可以从下一节中关于变量和占位符的相关段落了解到更多信息.


如何做...


以下讲述一下在TensorFlow中创建张量的主要方法:
1.固定大小的张量:
#创建零填充的张量.使用以下代码:

    zero_tsr = tf.zeros([row_dim, col_dim])


#创建1填充的张量.使用以下代码:

    ones_tsr = tf.ones([row_dim, col_dim])


#创建常量填充的张量.使用以下代码:

    filled_tsr = tf.fill([rom_dim, col_dim], 42)


#根据已知的常量创建张量.使用以下代码:

    constant_tsr = tf.constant([1,2,3])


注解:tf.constant可以把单个数值填充(broadcast)到一个阵列(array)中去,比如,tf.constant(42, [row_dim, col_dim])和tf.fill([rom_dim, col_dim], 42)效果一样.
2.相似形状(shape)的张量
#我们可以像下面这样做,根据一个张量的形状来初始化一个变量:

    zeros_similar = tf.zeros_like(constant_tsr)
    ones_similar = tf.ones_like(constant_tsr)


注解:因为这些张量取决于之前的张量,我们必须按顺序初始化它们.如果一次初始化所有的就会导致出错.可以参考下一节中关于变量及占位符段落的"更多...".
3.序列(sequence)张量:
#TensorFlow允许我们指定包含一定间隔的张量,下面函数的表现很像numpy中的range()及linspace():

    linear_tsr = tf.linspace(start=0,stop=1,interval=3)#实际上应该是    tf.linspace(start=0.,stop=1,num=3)


#上述结果是序列[0.0,0.5,1.0].这个结果包括指定的结束值.看下述函数:

    integer_seq_tsr = tf.range(start=6, limit=5, delta=3)


#上述结果是[6,9,12].注意这个结果不包括结束值.
4.随机张量:
#下述代码产生的随机数取样于均匀分布:

    randunif_tsr = tf.random_uniform([row_dim, col_dim], minval=0, maxval=1)


#注意上述均匀分布的随机数包括最小值但不包括最大值(minval <= x < maxval).
#要获取取样于正态分布的随机数张量,可以这样做:

    randnorm_tsr = tf.random_normal([row_dim, col_dim], mean=0.0, stddev=1.0)


#有时我们希望产生的正态分布的随机数在一定范围内,就可以用tf.truncated_normal()来在指定均值的两个标准差之间拾取随机数.

    runcnorm_tsr = tf.truncated_normal([row_dim, col_dim], mean=0.0, stddev=1.0)


#我们也可能想要把阵列的各项都随机化;有两个函数可以做到这些:tf.random_shuffle()及tf.random_crop():

    shuffled_output = tf.random_shuffle(input_tensor)
    cropped_output = tf.random_crop(input_tensor, crop_size)


#本书中稍后的章节中,需要对有三个色谱的图片大小进行随机裁剪.为了修复输出张量的某个维度,必须给出相应维度的最大值:
 

    cropped_image = tf.random_crop(my_image, [height/2,width/2,3])


如何工作...


一旦我们确定了如何创建张量,就可以通过tf.Variable()来包装对应的变量.如下所示,更多例子会出现在下个章节.

    my_var = tf.Variable(tf.zeros[row_dim, col_dim])


更多...


当然并不局限于TensorFlow的内置函数,我们可以把numpy的阵列转换成Python列表,或者通过tf.convert_to_tensor()把常量转换成张量.同时注意当我们想要在函数内部把一个计算一般化的时候可以让它接收张量作为输入.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值