TensorFlow的基本知識

2021-08-08 10:49:25 字數 3174 閱讀 4278

graph:圖,表示具體的計算任務

session:會話,圖需要在會話中執行,乙個會話可以包含很多圖

tensor:張量,在此表示資料

variable:就是本意變數,圖的重要組成部分

operation:簡稱op,是圖中計算的節點

feed、fetch:意思是給圖新增資料和獲取圖中的資料,因為訓練過程中有些資料需要動態獲得、臨時給予資料

tensor

tensorflow使用tensor資料結構來代表所有的資料,計算圖中,操作間傳遞的資料都是tensor.可以將tensor看做是乙個陣列或者是列表

variable(變數)
當建立乙個變數時,表明乙個張量作為初始值傳入建構函式variable()。引數用來初始化張量,呼叫tf.variable()新增一些操作(op,operation)到graph

tf.variable(initializer, name):initializer是初始化引數,可以有tf.random_normal,tf.constant,tf.constant等,name就是變數的名字

a = tf.variable(2, name='scalar')

b = tf.variable([2, 3], name='vector')

c = tf.variable([[0, 1], [2, 3]], name='matrix')

w = tf.variable(tf.zeros([784, 10]), name='weight')

變數有著下面幾個操作

x = tf.variable()

x.initializer

# 初始化

x.eval() # 讀取裡面的值

x.assign() # 分配值給這個變數

注意一點,在使用變數之前必須對其進行初始化,初始化可以看作是一種變數的分配值操作。最簡單的初始化方式是一次性初始化所有的變數

init = tf.global_variables_initializer()

with tf.session() as sess:

sess.run(init)

也可以對某一部分變數進行初始化

init_ab = tf.variable_initializer([a, b], name='init_ab')

with tf.session() as sess:

sess.run(init_ab)

或者是對某乙個變數進行初始化

w = tf.variable(tf.zeros([784, 10]))

with tf.session() as sess:

sess.run(w.initializer)

如果我們想取出變數的值,有兩種方法

w = tf.variable(tf.truncated_normal([10, 10], name='normal'))

with tf.session() as sess:

sess.run(w.initializer)

print(w.eval()) # 方法一

print(sess.run(w)) # 方法二

placeholders(佔位符)
tensorflow第一步是定義圖,第二步是在session中進行圖中的計算。對於圖中我們暫時不知道值的量,我們可以定義為佔位符,之後再用feed_dict去賦值,傳入sess.run()函式。

tf.placeholder(dtype, shape=none, name=none)

dtype是必須要指定的引數,shape如果是none,說明任何大小的tensor都能夠接受,使用shape=none很容易定義好圖,但是在debug的時候這將成為噩夢,所以最好是指定好shape。

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

c = a + b

with tf.session() as sess:

print(sess.run(c, feed_dict=))

constant(常數型別)
tf.constant(value, dtype=none, shape=none, name=』const』, verify_shape=false)

可以是一維向量和矩陣,或者是使用特殊值的向量建立,也有和numpy類似的序列建立

a = tf.constant([2, 2], name='a')

b = tf.constant([[0, 1], [2, 3]], name='b')

fetch
input1 = tf.constant([3.0])    

input2 = tf.constant([2.0])

input3 = tf.constant([5.0])

intermed = tf.add(input2,input3)

mul = tf.multiply(input1,intermed)

with tf.session() as sess:

result = sess.run([mul,intermed])

print(result)

feed
input1 = tf.placeholder(tf.float32)

input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1,input2)

with tf.session() as sess:

print(sess.run([output],feed_dict=))

tensorflow基本知識1

1 使用圖 graphs 表示計算任務。在被稱之為會話 session 的上下文 context 中執行圖 使用tensor表示資料 通過變數 variable 維護狀態 使用feed和fetch可以為任意的操作賦值或者從其中獲取資料 2 tensorflow是乙個程式設計系統,使用圖 graphs...

學習進度筆記 TensorFlow基本知識總結 1

tensorflow是全面的深度學習框架支援非常全面不是專門為客戶端設計 特點 1 真正的可移植性 引入各種計算裝置的支援包括cpu gpu tpu,以及能夠很好地執行在移動端,如安卓裝置 ios 樹莓派等等 2 多語言支援 tensorflow 有乙個合理的c 使用介面,也有乙個易用的python...

BI的基本知識

1,什麼是bi bi 即商業智慧型,商業智慧型是一種解決方案,通過抽取歷史資料,進行分析,挖掘,從中提取業務人員 可以理解的的資訊,通過這些資訊,進一步輔助業務人員進行決策 2,bi 的歷史上世紀70到 80年代,市場上出現了分析軟體。但是缺乏計算能力,使用者友好性差。與交易系統的整合困難,甚至手動...