文章目录
1.1 tensorflow常量变量定义
import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32) #定义常量
data2 = tf.Variable(10,name='var') #定义变量
print(data1)
print(data2)
#输出结果:没有把数据内容打印出来,而是打印数据的描述信息
Tensor("Const:0", shape=(), dtype=int32) #描述数据1:张量(常量,维度,数据类型)
<tf.Variable 'var:0' shape=() dtype=int32_ref> #描述数据2:变量,32位int类型
打印常量变量数据内容
import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32) #定义常量
data2 = tf.Variable(10,name='var') #定义变量
print(data1)
print(data2)
sess = tf.Session()
print(sess.run(data1))
init = tf.global_variables_initializer() #变量初始化
sess.run(init)
print(sess.run(data2))
sess.close()
#输出结果
Tensor("Const:0", shape=(), dtype=int32)
<tf.Variable 'var:0' shape=() dtype=int32_ref>
2
10
TensorFlow的实质:张量tensor(数据) + 计算图grahps(数据和操作的类型)
1.2 tensorflow常量变量四则运算
import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.Variable(2)
dataAdd = tf.add(data1,data2)
dataCopy = tf.assign(data2, dataAdd) # 把dataAdd的值赋给data2
dataMul = tf.multiply(data1,data2)
dataSub = tf.subtract(data1,data2)
dataDiv = tf.divide(data1,data2)
init = tf.global_variables_initializer() #变量初始化
with tf.Session() as sess:
sess.run(init)
print(sess.run(dataAdd))
print(sess.run(dataMul))
print(sess.run(dataSub))
print(sess.run(dataDiv))
print("sess.run(dataCopy) = ",sess.run(dataCopy)) # 8
print("dataCopy.eval() = ", dataCopy.eval()) # 8+6 -> 14
print('end')
#运算结果
8
12
4
3.0
sess.run(dataCopy) = 8
dataCopy.eval() = 14
end
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}))
# feed_dict = { 参数1:6,参数2:2},字典格式
print('end!')
#运算结果
8.0
end
1.3 tensorflow矩阵基础
#矩阵运算 M行*N列
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.shape) #打印矩阵的维度
#打印矩阵中的具体值
with tf.Session() as sess:
print(sess.run(data4)) #打印整体内容
print(sess.run(data4[0])) #打印某一行
print(sess.run(data4[:,0])) #打印某一列
print(sess.run(data4[0, 0])) # 打印第一行第一列
运行结果
(3, 2)
[[1 2]
[3 4]
[5 6]]
[1 2]
[1 3 5]
1
1.4 tensorflow矩阵运算
"""
1. tf.add(x, y, name=None)
矩阵x和矩阵y对应位置的元素相加
2. tf.subtract(x, y, name=None)
矩阵x和矩阵y对应位置的元素相减
3. tf.multiply(x, y, name=None)
矩阵x和矩阵y对应位置的元素相乘
4. tf.divide(x, y, name=None)
矩阵x和矩阵y对应位置的元素相除
5. tf.matmul(x, y)
矩阵x乘以矩阵y,返回的矩阵行数等矩阵x的行数,列数等于矩阵y的列数
"""
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) #矩阵乘法
matMul2 = tf.multiply(data1,data2) #
matAdd = tf.add(data1,data3) #矩阵加法
with tf.Session() as sess:
print(sess.run(matMul))
print(sess.run(matMul2))
print(sess.run(matAdd))
print(sess.run([matMul,matMul2])) #同时打印多个值
运行结果:
[[24]]
[[12 12]
[12 12]]
[[9 9]]
[array([[24]]), array([[12, 12],
[12, 12]])]
1.5 tensorflow特殊矩阵的初始化
import tensorflow as tf
mat1 = tf.zeros([2,3],dtype= tf.int16) #定义一个2*3的空矩阵
mat2 = tf.ones([3,2]) #定义一个3*2的全1矩阵
mat3 = tf.fill([2,3],15) #定义一个填充矩阵
mat4 = tf.zeros_like(mat1) #打印一个同维度的0矩阵
mat5 = tf.linspace(0.0,2.0,11) #把0-2之间的数据分成相等的10份
mat6 = tf.random_uniform([2,3],-1,2) #生成一个2*3的随机矩阵,范围在-1-2之间
with tf.Session() as sess:
print(sess.run(mat1))
print(sess.run(mat2))
print(sess.run(mat3))
print(sess.run(mat4))
print(sess.run(mat5))
print(sess.run(mat6))
#运行结果
[[0 0 0]
[0 0 0]]
[[1. 1.]
[1. 1.]
[1. 1.]]
[[15 15 15]
[15 15 15]]
[[0 0 0]
[0 0 0]]
[0. 0.2 0.4 0.6 0.8 1. 1.2
1.4 1.6 1.8000001 2. ]
[[ 1.7501397 -0.39935505 -0.3315227 ]
[ 0.54127705 1.6272781 1.8856971 ]]
1.6 tensorflow中numpy模块使用
import numpy as np
data1 = np.array([1,2,3,4,5])
data2 = np.array([[1,2],
[3,4]])
print(data1)
print(data1.shape)
print(data2)
print(data2.shape)
print(np.zeros([2,3]),'\n',np.ones([2,3]))
#改查
data2[1,0] = 5
print(data2)
print(data2[1,1])
#运行结果
[1 2 3 4 5]
(5,)
[[1 2]
[3 4]]
(2, 2)
[[0. 0. 0.]
[0. 0. 0.]]
[[1. 1. 1.]
[1. 1. 1.]]
[[1 2]
[5 4]]
4
1.7 tensorflow中matplotlib模块使用
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8])
y = np.random.randint(5,size = 8)
plt.plot(x,y,'g',lw = 5) # 折线
plt.bar(x,y,0.5,alpha=1,color='b') # 饼状
plt.show()