開始在B站上學(xué)習(xí)TensorFlow, 這個系列大概長這樣。
Screen Shot 2017-06-05 at 9.40.36 AM.png
這個repo提供了代碼,ppt,以及視屏的鏈接:
https://github.com/glossary95/TensorFlow-Tutorial
這篇筆記是1-3的內(nèi)容,這部分講了一點基本的小語法,作為正式開始機(jī)器學(xué)習(xí)之前的準(zhǔn)備。下面逐段講解該部分的代碼示例:
1.先引入幾個基本概念:
import tensorflow as tf
print('Load TV version',tf._version_)
#Tensor 在數(shù)學(xué)中是“張量”
#標(biāo)量,矢量\向量,張量
#簡單地理解
#標(biāo)量表示值
#矢量表示位置(空間中的一個點)
#張量表示整個空間
#一維數(shù)組是矢量
#多維數(shù)組是張量,矩陣也是張量
#4個重要的類型
#@Variable 計算圖譜中的變量
#@Tensor 一個多維矩陣,帶有很多方法
#@Graph 一個計算圖譜
#@Session 用來運(yùn)行一個計算圖譜
計算圖譜呢,官網(wǎng)提供的長這樣,先有一個粗略認(rèn)識:
Screen Shot 2017-06-02 at 9.02.08 AM.png
2. 三個重要的函數(shù): tf.func_name
#三個重要的函數(shù)
#Variable 變量
#以下是變量這個類的constructor
#tf.Variable.__init__(
# initial_value=None,@Tensor
# traibale=True,
# collections=None,
# validate_shape=True,
# caching_device=None,
# name=None,
# variable_def=None
# dtype=None
#)
#注意: Variable 是一個Class,Tensor也是一個Class
#Constant 常數(shù)
# tf.constant(value,dtype=None,shape=None,name='Const')
# return : a constant @Tensor
#Placeholder 暫時變量?
#tf.placeholder(dtype,shape=None,name=None)
#return :一個還尚未存在的 @Tensor
注意: tf.constant和tf.placeholder返回的都是一個張量tensor
下面實現(xiàn)一個四則運(yùn)算的函數(shù) basic_operation():
變量 + 變量 = 張量(addv)
常量就是一個張量,常量 + 常量 = 張量
運(yùn)行session,初始化所有的 變量
張量.eval(session = my_session_name)與my_session_name.run(張量)等價。
在with tf.Session(graph=graph) as mySess:這個代碼塊里,該寫session=sess的地方,都可以省略,比如run(),比如eval()。因為這個塊里面,使用的就是mySess。
def basic_operation():
v1 = tf.Variable(10)
v2 = tf.Variable(5)
addv = v1 + v2
print(addv)
print(type(addv)) //Tensor
print(type(v1)) //Variable
c1 = tf.constant(10)
c2 = tf.constant(5)
addc = c1 + c2
print(addc)
print(type(addc)) //Tensor
print(type(c1)) //Tensor
#用來運(yùn)行計算圖譜的對象\實例?
#session is a runtime
sess = tf.Session();
#Variable ->初始化 ->有值的Tensor
tf.initialize_all_varibales().run(session=sess)
print('變量是需要初始化的')
#以下兩行的作用是一樣的:在session下算出addv的值
print('加法(v1,v2)=',addv.eval(session=sess))
print('加法(v1,v2)=',sess.run(addv))
print('加法(c1,c2)=',addc.eval(session=sess))
#上面的一切,可以改寫成這樣:
#有一個圖,里面有一些數(shù)據(jù),然后有一個針對這個圖的session
#tf.Graph.__init__()
#Creates a new empty Graph
graph = tf.Graph()
with graph.as_default():
value1 = tf.constant([1,2])
value2 = tf.Variable([3,4])
mul = value1 * value2
mul2 = value1 / value2
with tf.Session(graph=graph) as mySess:
tf.initialize_all_variables().run()
print('一一對應(yīng)乘法(value1,value2)=',mySess.run(mul))
print('一一對應(yīng)乘法(value1,value2)=',mul.eval())
print('一一對應(yīng)的除法(value1, value2) = ', mySess.run(mul2))
print('一一對應(yīng)的除法(value1, value2) = ', mul2.eval())
# tensor.eval(session=sess)
# sess.run(tensor)
3. Placeholder很重要
# 省內(nèi)存?placeholder才是王道
# def use_placeholder():
graph = tf.Graph()
with graph.as_default():
value1 = tf.placeholder(dtype=tf.float64)
value2 = tf.Variable([3, 4], dtype=tf.float64)
mul = value1 * value2
with tf.Session(graph=graph) as mySess:
tf.initialize_all_variables().run()
# 我們想象一下這個數(shù)據(jù)是從遠(yuǎn)程加載進(jìn)來的
# 文件,網(wǎng)絡(luò)
# 假裝是 10 GB
value = load_from_remote()
for partialValue in load_partial(value, 2):
# runResult = mySess.run(mul, feed_dict={value1: partialValue})
evalResult = mul.eval(feed_dict={value1: partialValue})
print('乘法(value1, value2) = ', runResult)
# cross validation
def load_from_remote():
return [-x for x in range(1000)]
# 自定義的 Iterator
# yield, generator function
def load_partial(value, step):
index = 0