環(huán)境配置和基本使用
python3.6下tensorflow安裝
TensorFlow即可以支持CPU,也可以支持CPU+GPU。前者的環(huán)境需求簡單,后者需要額外的支持。TensorFlow是基于VC++2015開發(fā)的,所以需要下載安裝VisualC++ Redistributable for Visual Studio 2015?來獲取MSVCP140.DLL的支持。
因?yàn)椋乙呀?jīng)安裝好了python3,所以通過pip包管理工具就可以安裝tensorflow
打開windows的命令行窗口,安裝CPU版本輸入
pip3 install --upgrade tensorflow
驗(yàn)證TensorFlow安裝是否成功,可以在命令行窗口輸入python進(jìn)入python環(huán)境,或者運(yùn)行python3.5命令行后輸入以下代碼:
>>>import?tensorflow?as?tf
>>> hello = tf.constant(
'Hello, TensorFlow!')
>>> sess = tf.
Session()
>>>?
print(sess.run(hello))
如果能正常輸出hello字符串,則安裝成功。
通過矩陣加法計(jì)算求和來理解tensorflow的用法:
input1 = tf.constant(2.0)
input2 = tf.constant(3.0)
input3 = tf.constant(5.0)
intermd = tf.add(input1, input2)
mul = tf.multiply(input2, input3)with tf.Session() as sess:
?result = sess.run([mul, intermd])? # 一次執(zhí)行多個(gè)op? ?
?print result
?[15.0, 5.0]
使用feed來對變量賦值:
首先,使用placeholder()函數(shù)來占位,在通過feed_dict()來添加數(shù)據(jù):
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={input1:[7.0], input2:[2.0]})
[array([ 14.], dtype=float32)]