iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 13
0

前言:

2維的Tensor(張量)就是矩陣,所以矩陣的基本運算再tensorflow中是十分重要的,而再資訊工程中有一門必修就是線性代數,再那裡面會教到很多矩陣的東西,但因為是2年前的事情了,所以筆者記憶有點模糊了,但就加減複習一下吧。

程式開始:

相乘:

import tensorflow as tf
a = tf.Variable([[1,1,1]])
b = tf.Variable([[3,5],[4,5],[5,6]])
x = tf.matmul(a,b)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print (sess.run(x))
sess.close()

說明:

程式的意思是,有一個 13的矩陣 乘上 一個 32的矩陣,再矩陣的世界中要可以相乘,第一個矩陣的列數(column)和第二個矩陣的行數(row)相同時才可以。

答案:

[(1x3+1x4+1x5) (1x5+1x5+1x6)]=[12 16]

執行結果:

https://ithelp.ithome.com.tw/upload/images/20180101/20107535uet4fXdyJI.png

https://ithelp.ithome.com.tw/upload/images/20180101/20107535SSvDVa4R0r.png

相加:

import tensorflow as tf
a = tf.Variable([[1,2]])
b = tf.Variable([[-3,4]])
x = a+b
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print (sess.run(x))
sess.close()

說明:

程式的意思是,有一個 12的矩陣 加上 一個 12的矩陣,再矩陣的世界中要可以相加,兩個矩陣的大小必須一樣。

答案:

[(1+(-3)) (2+4)] = [-2 6]

執行結果:

https://ithelp.ithome.com.tw/upload/images/20180101/20107535ZRtQMGSI6I.png

https://ithelp.ithome.com.tw/upload/images/20180101/2010753512G5yGoyI7.png


上一篇
DAY12 TensorBoard
下一篇
DAY14 激勵函數
系列文
tensorflow python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言