Tensorflow學習教程------建立圖啟動圖
阿新 • • 發佈:2018-11-14
Tensorflow作為目前最熱門的機器學習框架之一,受到了工業界和學界的熱門追捧。以下幾章教程將記錄本人學習tensorflow的一些過程。
在tensorflow這個框架裡,可以講是弱資料型別,也就是說不嚴格宣告資料是什麼型別,因為在整個過程中玩的都是向量,或者說矩陣和陣列,所有的資料都被看做是一個tensor, 一個或者幾個tensor經過一個op(operation)之後,產生新的tensor。首先將所有tensor和op都定義好,然後把這套tensor和op的組合放到預設的圖裡,用會話啟動圖,最後我們就看到結果了。以下是建立圖和啟動圖的程式碼以及結果。
1 2 3 4 5 6 7 8 9 10 11 12 |
#coding:utf-8
import
tensorflow as tf
m1
=
tf.constant([[
3
,
3
]])
m2
=
tf.constant([[ 2
],[
3
]])
product
=
tf.matmul(m1,m2)
print
(product)
#定義一個會話 啟動預設圖
sess
=
tf.Session() #呼叫sess的run方法來執行矩陣乘法
result
=
sess.run(product)
print
(result)
sess.close()
|
結果如下
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally Tensor("MatMul:0", shape=(1, 1), dtype=int32) W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate (GHz) 1.582 pciBusID 0000:03:00.0 Total memory: 10.91GiB Free memory: 10.24GiB I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0: Y I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0) [[15]]