前言
尝试根据b站大佬来扩充自己的知识面
本人使用的环境为tf2.0但是b站视频是1.0,因此与视频会稍有不符合
参考
B站视频
过程
代码片段1
这里仅贴出跟着b站视频写出来的代码,并在有些区域会有些许注释
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | import tensorflow as tf
 
 
 
 
 tf.compat.v1.disable_eager_execution()
 
 data1 = tf.compat.v1.placeholder(tf.float32)
 data2 = tf.compat.v1.placeholder(tf.float32)
 dataAdd = tf.add(data1, data2)
 
 
 
 
 with tf.compat.v1.Session() as sess:
 print(sess.run(dataAdd,  feed_dict={data1: 6, data2: 2}))
 
 print("end")
 
 
 | 
输出内容:
代码片段2
此部分代码表明Python的矩阵维度定义
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | import tensorflow as tf
 tf.compat.v1.disable_eager_execution()
 
 data1 = tf.compat.v1.constant([[6, 6]])
 data2 = tf.compat.v1.constant([[2], [2]])
 data3 = tf.compat.v1.constant([[3, 3]])
 data4 = tf.compat.v1.constant([[1, 2], [3, 4], [5, 6]])
 data5 = tf.compat.v1.constant([[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]])
 
 print(data5.shape)
 
 with tf.compat.v1.Session() as sess:
 print(sess.run(data5))
 print(sess.run(data5[0]))
 print(sess.run(data5[:, 0]))
 print(sess.run(data5[0][0]))
 
 
 
 | 
输出内容:
| 12
 3
 4
 5
 6
 7
 
 | (3, 3)[[1 2 3]
 [4 5 6]
 [7 8 9]]
 [1 2 3]
 [1 4 7]
 1
 
 | 
片段
代码片段3
此段代码讲解矩阵乘法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | import tensorflow as tf
 import os
 
 os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
 
 tf.compat.v1.disable_eager_execution()
 
 data1 = tf.compat.v1.constant([[6, 6]])
 data2 = tf.compat.v1.constant([[2], [2]])
 data3 = tf.compat.v1.constant([[3, 3]])
 data4 = tf.compat.v1.constant([[1, 2], [3, 4], [5, 6]])
 data5 = tf.compat.v1.constant([[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]])
 matMul = tf.matmul(data1, data2)
 matMul2 = tf.multiply(data1, data2)
 matAdd = tf.add(data1, data3)
 with tf.compat.v1.Session() as sess:
 print(sess.run(matMul))
 print(sess.run(matAdd))
 print(sess.run(matMul2))
 print(sess.run([matMul, matAdd]))
 
 
 
 | 
运行结果:
| 12
 3
 4
 5
 
 | [[24]][[9 9]]
 [[12 12]
 [12 12]]
 [array([[24]], dtype=int32), array([[9, 9]], dtype=int32)]
 
 | 
代码片段4
如何定义一个基本矩阵
代码如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | import tensorflow as tfimport os
 
 os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
 tf.compat.v1.disable_eager_execution()
 
 mat0 = tf.constant([[0, 0, 0], [0, 0, 0]])
 mat1 = tf.zeros([2, 3])
 mat2 = tf.ones([3, 2])
 mat3 = tf.fill([2, 3], 15)
 with tf.compat.v1.Session() as sess:
 print(sess.run(mat0))
 print(sess.run(mat1))
 print(sess.run(mat2))
 print(sess.run(mat3))
 
 | 
运行结果如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | [[0 0 0][0 0 0]]
 [[0. 0. 0.]
 [0. 0. 0.]]
 [[1. 1.]
 [1. 1.]
 [1. 1.]]
 [[15 15 15]
 [15 15 15]]
 
 | 
代码片段5
如何定义一个非基本矩阵
代码如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | import tensorflow as tf
 import os
 
 os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
 tf.compat.v1.disable_eager_execution()
 
 mat1 = tf.constant([[2], [3], [4]])
 mat2 = tf.zeros_like(mat1)
 mat3 = tf.linspace(0.0, 2.0, 11)
 mat4 = tf.compat.v1.random_uniform([2, 3], -1, 2)
 with tf.compat.v1.Session() as sess:
 print(sess.run(mat2))
 print(sess.run(mat3))
 print(sess.run(mat4))
 
 | 
运行结果如下:
| 12
 3
 4
 5
 6
 7
 8
 
 | [[0][0]
 [0]]
 [0.        0.2       0.4       0.6       0.8       1.        1.2
 1.4       1.6       1.8000001 2.       ]
 [[-0.988165   0.3698578 -0.68531  ]
 [-0.7001637  1.0496833  1.1087246]]
 
 
 | 
在tf中矩阵的定义
在python中,定义一个矩阵可以采用以下方法进行定义:
什么是placeholder
Tensorflow中的palceholder,中文翻译为占位符,什么意思呢?
在Tensoflow2.0以前,还是静态图的设计思想,整个设计理念是计算流图,在编写程序时,首先构筑整个系统的graph,代码并不会直接生效,这一点和python的其他数值计算库(如Numpy等)不同,graph为静态的,在实际的运行时,启动一个session,程序才会真正的运行。这样做的好处就是:避免反复地切换底层程序实际运行的上下文,tensorflow帮你优化整个系统的代码。我们知道,很多python程序的底层为C语言或者其他语言,执行一行脚本,就要切换一次,是有成本的,tensorflow通过计算流图的方式,可以帮你优化整个session需要执行的代码。
在代码层面,每一个tensor值在graph上都是一个op,当我们将train数据分成一个个minibatch然后传入网络进行训练时,每一个minibatch都将是一个op,这样的话,一副graph上的op未免太多,也会产生巨大的开销;于是就有了tf.placeholder(),我们每次可以将 一个minibatch传入到x = tf.placeholder(tf.float32,[None,32])上,下一次传入的x都替换掉上一次传入的x,这样就对于所有传入的minibatch x就只会产生一个op,不会产生其他多余的op,进而减少了graph的开销。
——CSDN
其他
错误汇总
报错信息为AttributeError: module 'tensorflow' has no attribute 'placeholder'
由于本人使用的环境不同,根据CSDN上的信息而言,表明在tf版本2.0之后,该方法就已经被进行了弃用,因此我们需要寻找到2.0版本的对应方法,对应方法已经在相关代码片段中进行提及