環(huán)境配置和基本使用
python3.6下tensorflow安裝
TensorFlow即可以支持CPU,也可以支持CPU+GPU。前者的環(huán)境需求簡(jiǎn)單,后者需要額外的支持。TensorFlow是基于VC++2015開(kāi)發(fā)的疟暖,所以需要下載安裝VisualC++ Redistributable for Visual Studio 2015?來(lái)獲取MSVCP140.DLL的支持。
因?yàn)椋乙呀?jīng)安裝好了python3俐巴,所以通過(guò)pip包管理工具就可以安裝tensorflow
打開(kāi)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字符串锦募,則安裝成功。
通過(guò)矩陣加法計(jì)算求和來(lái)理解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來(lái)對(duì)變量賦值:
首先邻遏,使用placeholder()函數(shù)來(lái)占位糠亩,在通過(guò)feed_dict()來(lái)添加數(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)]