要使用TensorFlow, 我們選擇的語(yǔ)言是python, 使用python的包管理工具Anaconda
配置安裝
配置順序:
- 安裝Anaconda, 自帶jupyter (或者使用pip來(lái)安裝
$pip3 install --upgrade pip$pip3 install jupyter) - 安裝TensorFlow
pip3 install tensorflow
使用TensorFlow
這次僅僅實(shí)現(xiàn)創(chuàng)建圖, 啟動(dòng)圖
- 導(dǎo)入TensorFlow
- 創(chuàng)建常量op
- 創(chuàng)建一個(gè)矩陣乘法
- 定義一個(gè)會(huì)話
- 調(diào)用sess的run來(lái)執(zhí)行矩陣乘法
import tensorflow as tf
#創(chuàng)建一個(gè)常量op
m1 = tf.constant([[3, 3]])
#創(chuàng)建一個(gè)常量op
m2 = tf.constant([[2], [3]])
#創(chuàng)建一個(gè)矩陣乘法
product = tf.matmul(m1, m2)
print(product)
#定義一個(gè)會(huì)話
sess = tf.Session()
#調(diào)用sess的run來(lái)執(zhí)行矩陣乘法
result = sess.run(product)
print(result)
sess.close()
注意下半部分的代碼可以使用with語(yǔ)法簡(jiǎn)化:
with tf.Session() as sess:
result = sess.run(product)
print(result)
關(guān)于with的語(yǔ)法, 可以參考這里
簡(jiǎn)而言之就是with可以不用再去管close了