運行機制:張量tensor+計算圖graphs
1.tensor張量表示數(shù)據(jù):常量,變量
2.op:operation:四則運算等
本質(zhì):tf=tensor+計算圖
tensor 數(shù)據(jù)
graphs 數(shù)據(jù)操作
session是執(zhí)行的核心,交互過程
運算時需要插入的數(shù)據(jù)
3.變量
x=tf.Variable()
# 變量賦值
state=tf.Variable(0)
new_value=tf.add(state,1)
update=tf.assign(state,new_value)
# 變量初始化
init=tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(sess.run(update))
4.fetch
sess.run([x1,x2])
5.feed
sess.run(output,feed={input1:x1,input2:x2})
6.梯度下降法
# 定義損失函數(shù)
loss=tf.reduce_mean(tf.square(y_data_y))
# 定義優(yōu)化器-->梯度優(yōu)化
optimizer=tf.train.GradientDescentOptimizer(0.2)
# 通過優(yōu)化器最小化損失函數(shù)
train=optimizer.minimize(loss)
# 循環(huán)train
with tf.Session() as sess:
for step in range(2000):
sess.run(train)
- tf.random.normal()
tf.random_normal()函數(shù)用于從“服從指定正態(tài)分布的序列”中隨機取出指定個數(shù)的值。
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
shape: 輸出張量的形狀,必選
mean: 正態(tài)分布的均值,默認為0
stddev: 正態(tài)分布的標準差,默認為1.0
dtype: 輸出的類型,默認為tf.float32
seed: 隨機數(shù)種子,是一個整數(shù),當設置之后,每次生成的隨機數(shù)都一樣
name: 操作的名稱
tf.arg_max()
tf.argmax(input,axis)根據(jù)axis取值的不同返回每行或者每列最大值的索引。
axis=0 返回每列
axis=1 返回每行tf.cast()
tf.cast()函數(shù)的作用是執(zhí)行 tensorflow 中張量數(shù)據(jù)類型轉(zhuǎn)換,比如讀入的圖片如果是int8類型的,一般在要在訓練前把圖像的數(shù)據(jù)格式轉(zhuǎn)換為float32。
cast定義:
cast(x, dtype, name=None)
第一個參數(shù) x: 待轉(zhuǎn)換的數(shù)據(jù)(張量)
第二個參數(shù) dtype: 目標數(shù)據(jù)類型
第三個參數(shù) name: 可選參數(shù),定義操作的名稱
- tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
功能說明:產(chǎn)生截斷正態(tài)分布隨機數(shù),取值范圍【mean-2stddev,mean+2stddev】
參數(shù)列表:2020-03-30 18-36-49屏幕截圖.png
