tf矩阵基础1
#placehold
import tensorflow as tf
data1 = tf.placeholder(tf.float32)#浮点类型
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))
# 参数1 dataAdd 参数2 data (feed_dict = {参数1:6,参数2:2})
print('end!')
-
问题
C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
-
解决(对应日志提示一个一个进行修改)
找到错误日志里面对应目录下面的文件进行修改(518行)C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or ‘1type’
将_np_qint16 = np.dtype([(“qint16”, np.int16, 1)])
改为_np_qint16 = np.dtype([(“qint16”, np.int16, (1,))])
-
解决以后输出正确结果
8.0 end!
-
#矩阵类比 数组 M行N列 外部一定要有[] 内部[] [里面 列数据] 里面[] 中括号的个数 行数 #[[6,6]] [[6,6]] import tensorflow as tf data1 = tf.constant([[6,6]])#一行:内部一个中括号,两列:两个数据 data2 = tf.constant([[2], [2]])#两行一列 data3 = tf.constant([[3,3]])#一行两列 data4 = tf.constant([[1,2], [3,4], [5,6]])#三行两列 print('打印data4的维度:',data4.shape)# 维度 #print(data4.shape)# 维度(行数,列数) with tf.Session() as sess: print('打印data4整体:\n',sess.run(data4)) #打印整体 print('打印data4第一行:',sess.run(data4[0]))# 打印某一行 print('打印data4第一列:',sess.run(data4[:,0]))#MN 列 print('打印data4第一行第二列:',sess.run(data4[0,1]))# 1 1 MN = 0 32 = M012 N01
运行结果:
打印data4的维度: (3, 2) 打印data4整体: [[1 2] [3 4] [5 6]] 打印data4第一行: [1 2] 打印data4第一列: [1 3 5] 打印data4第一行第二列: 2
3.矩阵运算理论
import tensorflow as tf data1 = tf.constant([[6,6]]) data2 = tf.constant([[2], [2]]) data3 = tf.constant([[3,3]]) data4 = tf.constant([[1,2], [3,4], [5,6]]) matMul = tf.matmul(data1,data2)#矩阵乘法: 1X2 2X1得1X2 matMul2 = tf.multiply(data1,data2)# matAdd = tf.add(data1,data3)#矩阵加法要相同1X2 with tf.Session() as sess: print('1.[[6,6]]乘[[2],[2]]:matmul普通乘法',sess.run(matMul))#1 维 M=1 N2. 1X2(MK) 2X1(KN) = 1 print('2.[[6,6]]加[[3,3]]:',sess.run(matAdd))#1行2列 print('3.[[6,6]]乘[[2],[2]]:multiply\n',sess.run(matMul2))# 1x2 2x1 = 2x2 multiply类似于加法(乘),但不需要相同 print('4.三种结果一次性打印:',sess.run([matMul,matAdd,matMul2]))#一次打印多种结果
-
运行结果:
1.[[6,6]]乘[[2],[2]]:matmul普通乘法 [[24]] 2.[[6,6]]加[[3,3]]: [[9 9]] 3.[[6,6]]乘[[2],[2]]:multiply [[12 12] [12 12]] 4.三种结果一次性打印: [array([[24]]), array([[9, 9]]), array([[12, 12], [12, 12]])]
矩阵初始化
- 矩阵初始化
import tensorflow as tf #初始化并指定行列 mat0 = tf.constant([[0,0,0],[0,0,0]])#两行三列初始化 mat1 = tf.zeros([2,3])#zeros初始化 mat2 = tf.ones([3,2]) mat3 = tf.fill([2,3],15) with tf.Session() as sess: print('1.直接赋值方式初始化:\n',sess.run(mat0)) print('2.使用 zeros 初始化0:\n',sess.run(mat1)) print('3.使用 ones 初始化1:\n',sess.run(mat2)) print('4.使用 fill 初始化15:\n',sess.run(mat3))
- 结果
1.直接赋值方式初始化: [[0 0 0] [0 0 0]] 2.使用 zeros 初始化0: [[0. 0. 0.] [0. 0. 0.]] 3.使用 ones 初始化1: [[1. 1.] [1. 1.] [1. 1.]] 4.使用 fill 初始化15: [[15 15 15] [15 15 15]]
- 一些特殊矩阵
import tensorflow as tf mat1 = tf.constant([[2],[3],[4]]) mat2 = tf.zeros_like(mat1)#与mat1维度相同并且初始化为0 mat3 = tf.linspace(0.0,2.0,11)#0.0-2.0分成十等分 中间11个数据 mat4 = tf.random_uniform([2,3],-1,2)#随机矩阵序列,并指定维度-1——2 with tf.Session() as sess: print('打印constant([[2],[3],[4]]):\n',sess.run(mat1)) print('打印zeros_like(mat1):\n',sess.run(mat2)) print('打印linspace(0.0,2.0,11):\n',sess.run(mat3)) print('打印random_uniform([2,3],-1,2):\n',sess.run(mat4))
- 结果:
打印constant([[2],[3],[4]]): [[2] [3] [4]] 打印zeros_like(mat1): [[0] [0] [0]] 打印linspace(0.0,2.0,11): [0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8000001 2. ] 打印random_uniform([2,3],-1,2): [[ 1.1290278 -0.16853952 0.76491785] [ 0.3711592 1.7550797 1.3344793 ]]
1.4 1.6 1.8000001 2. ] 打印random_uniform([2,3],-1,2): [[ 1.1290278 -0.16853952 0.76491785] [ 0.3711592 1.7550797 1.3344793 ]]
-