《TensorFlow Machine Learning Cookbook》--1.4

6 篇文章 0 订阅
5 篇文章 0 订阅

使用矩阵

理解TensorFlow如何使用(或者说如何处理)矩阵,对于理解计算图(computational graphs)如何处理数据流,是非常重要的。

 

准备工作

 

很多算法依赖矩阵操作,Tensorflow给我们提供了易于使用的方法以便进行这种矩阵计算。对于以下所有的例子,我们可以通过运行以下代码来创建图会话(graph session):

    import tensorflow as tf
    sess = tf.Session()

 

如何做...

 

1.创建矩阵 正如前面的章节对“张量”(tensors)描述的那样,我们可以从 numpy 阵列(numpy arrays)或者嵌套的列表(nested lists)来乌创建二维矩阵,也可以使用张量创建函数并给此类的函数指定一个二维类型(shape)。这样的函数有 zeros(), ones(), truncated_normal()等等。Tensorflow也允许我们使用函数 diag() 从一维数组或者列表来创建对角矩阵,比如:

identity_matrix = tf.diag([1.0, 1.0, 1.0])
A = tf.truncated_normal([2, 3])
B = tf.fill([2, 3], 5.0)
C = tf.random_uniform([3, 2])
D = tf.convert_to_tensor(np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]]))

可以通过运行以下代码看上述结果

print(sess.run(identity_matrix))
'''
[[ 1. 0. 0.]
 [ 0. 1. 0.]
 [ 0. 0. 1.]]
'''
print(sess.run(A))
'''
[[ 0.96751703  0.11397751  -0.3438891 ]
 [-0.10132604 -0.8432678    0.29810596]]
'''
print(sess.run(B))
'''
[[ 5.  5.  5.]
 [ 5.  5.  5.]]
'''
print(sess.run(C))
'''
[[ 0.33184157  0.08907614]
 [ 0.53189191  0.67605299]
 [ 0.95889051  0.67061249]]
'''
print(sess.run(D))
'''
[[ 1.  2.  3.]
 [-3. -7. -1.]
 [ 0.  5. -2.]]
'''

要注意的是,如果我们再次运行 sess.run(C), 会再次初始化随机变量,最后会有不同的随机值。

2.使用以下函数进行加减运算:

print(sess.run(A+B))
'''
[[ 4.61596632  5.39771316  4.4325695 ]
 [ 3.26792376  5.14477345  4.98265553]]
'''
print(sess.run(B-B))
'''
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
'''
print(sess.run(tf.matmul(B, identity_matrix)))
'''
[[ 5.  5.  5.]
 [ 5.  5.  5.]]
'''

3.另外,函数 matmul() 可以通过参数指定在相乘之前是否对矩阵进行转置,或者指定每个矩阵是否为稀疏矩阵。

4.转置运算如下:

print(sess.run(tf.transpose(C)))
'''
[[ 0.67124544  0.26766731  0.99068872]
 [ 0.25006068  0.86560275  0.58411312]]
'''

5.再次值得一提的是,重新初始化会给出和之前相比不同的值。

6.计算行列式和逆矩阵,使用下述函数:

print(sess.run(tf.matrix_determinant(D)))
'''
-38.0
'''
print(sess.run(tf.matrix_inverse(D)))
'''
[[-0.5          -0.5        -0.5       ]
 [ 0.15789474    0.05263158  0.21052632]
 [ 0.39473684    0.13157894  0.02631579]]
'''

7.分解,乔列斯基分解:

print(sess.run(tf.cholesky(identity_matrix)))
'''
[[ 1.  0.  1.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
'''

8.求特征值及特征向量:

print(sess.run(tf.self_adjoint_eig(D)))
'''
[[-10.65907521    -0.22750691    2.88658212]
 [  0.21749542     0.63250104   -0.74339638]
 [  0.84526515     0.2587998     0.46749277]
 [ -0.4880805      0.73004459    0.47834331]] 
'''

注意:函数 self_adjoint_eig() 在第一行输出特征值。数学中,这就是所谓的矩阵的特征值分解。

 

如何工作...

 

Tensorflow给我们提供了工具来开始数值计算并把这种计算添加到图中。这些符号标记对于简单的矩阵操作来说似乎有些复杂。要记住:我们把这些操作添加到图里再告诉Tensorflow哪些张量会进行这些操作。它们现在看起来有些繁琐,但对理解后面章节中的符号标记很有用处,尤其是当这种计算方式使得完成我们的目标更容易的时候。

 

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值