生活随笔
收集整理的這篇文章主要介紹了
深度学习-Tensorflow基本介绍01
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一,認(rèn)識(shí)TensorFlow
二,安裝Tensorflow
安裝的1.13版本,目前已更新2.0+版本
pip install tensorflow
== 1.13 .1 - i https
: // pypi
. tuna
. tsinghua
. edu
. cn
/ simple
三,TensorFlow圖的結(jié)構(gòu)
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
print ( a
, b
)
with tf
. Session
( ) as sess
: pass
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
print ( sum1
)
with tf
. Session
( ) as sess
: print ( sess
. run
( sum1
) )
1,數(shù)據(jù)流圖
數(shù)據(jù):tensor:張量 operation:(OP):專門(mén)運(yùn)算的操作節(jié)點(diǎn),所有操作都是一個(gè)op 圖:graph:你的整個(gè)程序的結(jié)構(gòu)
邊是由流動(dòng)的Tensor組成; 每個(gè)節(jié)點(diǎn)是Operation操作,即一些數(shù)學(xué)的操作、激勵(lì)函數(shù)的操作等,作為Operation的輸入和輸出都是Tensor(張量); 邊和節(jié)點(diǎn)共同構(gòu)成了Graph,即數(shù)據(jù)流圖; 對(duì)于數(shù)據(jù)流圖的運(yùn)行,需要?jiǎng)?chuàng)建Session這樣一個(gè)會(huì)話來(lái)運(yùn)行,Session可以在不同的設(shè)備上運(yùn)行,例如GPU、CPU等。
2,圖
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
graph
= tf
. get_default_graph
( )
print ( graph
)
with tf
. Session
( ) as sess
: print ( sess
. run
( sum1
) ) print ( a
. graph
) print ( sum1
. graph
) print ( sess
. graph
)
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
g
= tf
. Graph
( )
with g
. as_default
( ) : c
= tf
. constant
( 10.0 ) print ( c
. graph
)
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
graph
= tf
. get_default_graph
( )
print ( graph
)
with tf
. Session
( ) as sess
: print ( sess
. run
( sum1
) ) print ( sum1
. graph
)
創(chuàng)建一張圖(包含了一組op和tensor) op: 只要使用了TensorFlow的API定義的函數(shù),都是op 張量(tensor):指的就是數(shù)據(jù)
3,會(huì)話
在編譯環(huán)境中,輸入要執(zhí)行的Tensorflow代碼,當(dāng)創(chuàng)建會(huì)話執(zhí)行相應(yīng)的功能時(shí),首先客戶端會(huì)創(chuàng)建一個(gè)圖,接著,使用會(huì)話的run操作,這是會(huì)將需要執(zhí)行的操作傳入服務(wù)端,服務(wù)端使用C++語(yǔ)言完成相應(yīng)的操作, 得出結(jié)果后,將結(jié)果傳回客戶端,然后根據(jù)代碼命令最后輸出返回的結(jié)果。
import os
import tensorflow
as tf
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
graph
= tf
. get_default_graph
( )
print ( graph
)
with tf
. Session
( config
= tf
. ConfigProto
( log_device_placement
= True ) ) as sess
: print ( sess
. run
( sum1
) ) print ( sum1
. graph
)
import os
import tensorflow
as tf
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
graph
= tf
. get_default_graph
( )
print ( graph
)
with tf
. Session
( ) as sess
: print ( sess
. run
( sum1
) ) print ( "-" * 50 ) print ( sum1
. graph
) print ( "-" * 50 ) print ( sum1
. eval ( ) )
4,會(huì)話里的run()方法
import os
import tensorflow
as tf
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
graph
= tf
. get_default_graph
( ) with tf
. Session
( ) as sess
: print ( sess
. run
( [ a
, b
, sum1
] ) ) print ( "-" * 50 )
重載機(jī)制
import os
import tensorflow
as tf
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
)
aa
= 1.0
bb
= 2.0
sum2
= aa
+ bb
sum3
= a
+ aagraph
= tf
. get_default_graph
( ) with tf
. Session
( ) as sess
: print ( sess
. run
( sum3
) ) print ( "-" * 50 )
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
plt
= tf
. placeholder
( tf
. float32
, [ 2 , 3 ] ) with tf
. Session
( ) as sess
: print ( sess
. run
( plt
, feed_dict
= { plt
: [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] } ) )
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
plt
= tf
. placeholder
( tf
. float32
, [ None , 3 ] )
print ( plt
) with tf
. Session
( ) as sess
: print ( sess
. run
( plt
, feed_dict
= { plt
: [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] } ) )
5,張量的定義及數(shù)據(jù)
數(shù)據(jù)的類型代表進(jìn)度不一樣
import os
import tensorflow
as tf
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
a
= tf
. constant
( 5.0 )
b
= tf
. constant
( 6.0 )
sum1
= tf
. add
( a
, b
) graph
= tf
. get_default_graph
( ) with tf
. Session
( ) as sess
: print ( sum1
. graph
) print ( "-" * 50 ) print ( sum1
. name
) print ( "-" * 50 ) print ( sum1
. op
) print ( "-" * 50 ) print ( sum1
. shape
)
在TensorFlow中:打印出來(lái)的形狀表示如下: 0維:() 1維:(5) 2維:(5,6) 3維(2,3,4)
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
plt
= tf
. placeholder
( tf
. float32
, [ None , 3 ] )
print ( plt
)
plt
. set_shape
( [ 2 , 3 ] )
print ( plt
) with tf
. Session
( ) as sess
: pass
import tensorflow
as tf
plt
= tf
. placeholder
( tf
. float32
, [ None , 3 ] )
print ( plt
)
plt
. set_shape
( [ 4 , 3 ] )
print ( plt
)
plt_reshape
= tf
. reshape
( plt
, [ 2 , 6 ] )
print ( plt_reshape
) with tf
. Session
( ) as sess
: pass
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
plt
= tf
. placeholder
( tf
. float32
, [ None , 3 ] )
print ( plt
)
plt
. set_shape
( [ 4 , 3 ] )
print ( plt
)
plt_reshape
= tf
. reshape
( plt
, [ 2 , 6 ] )
print ( plt_reshape
)
plt_reshape2
= tf
. reshape
( plt
, [ 2 , 3 , 2 ] )
print ( plt_reshape2
) with tf
. Session
( ) as sess
: pass
6,運(yùn)算API介紹
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tfzero
= tf
. zeros
( [ 3 , 4 ] , tf
. float32
)
ones
= tf
. ones
( [ 3 , 4 ] , tf
. float32
) with tf
. Session
( ) as sess
: print ( zero
. eval ( ) ) print ( "-" * 50 ) print ( ones
. eval ( ) )
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tfa
= [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ]
b
= [ [ 7 , 8 , 9 ] , [ 10 , 11 , 12 ] ]
c
= tf
. concat
( [ a
, b
] , axis
= 0 )
d
= tf
. concat
( [ a
, b
] , axis
= 1 ) with tf
. Session
( ) as sess
: print ( sess
. run
( c
) ) print ( "-" * 50 ) print ( sess
. run
( d
) )
四,可視化學(xué)習(xí)
1,變量能夠持久化保存,普通張量op不能 2,當(dāng)定義一個(gè)變量op的時(shí)候,一定要在會(huì)話當(dāng)中去運(yùn)行初始化 3,name參數(shù):在tensorboard使用的時(shí)候顯示名字,可以讓相同的op名字進(jìn)行區(qū)分
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
a
= tf
. constant
( [ 1 , 2 , 3 , 4 , 5 ] ) var
= tf
. Variable
( tf
. random_normal
( [ 2 , 3 ] , mean
= 0.0 , stddev
= 1.0 ) )
print ( a
, var
)
init_op
= tf
. global_variables_initializer
( )
with tf
. Session
( ) as sess
: sess
. run
( init_op
) print ( sess
. run
( [ a
, var
] ) )
import os
os
. environ
[ 'TF_CPP_MIN_LOG_LEVEL' ] = '2'
import tensorflow
as tf
a
= tf
. constant
( [ 1 , 2 , 3 , 4 , 5 ] ) var
= tf
. Variable
( tf
. random_normal
( [ 2 , 3 ] , mean
= 0.0 , stddev
= 1.0 ) )
print ( a
, var
)
init_op
= tf
. global_variables_initializer
( )
with tf
. Session
( ) as sess
: sess
. run
( init_op
) filewriter
= tf
. summary
. FileWriter
( "./test" , graph
= sess
. graph
) print ( sess
. run
( [ a
, var
] ) )
總結(jié)
以上是生活随笔 為你收集整理的深度学习-Tensorflow基本介绍01 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。