import tensorflow as tf
import numpy as np
點乘,支持broadcasting
- 乘號* 和 multiply等價
- mul已經廢棄不用了
- matmul 是矩陣相乘
broadcasting參見:
http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
原則:
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when
- they are equal, or
- one of them is 1
a = tf.placeholder("float")
b = tf.placeholder("float")
y = a * b
y2 = tf.multiply(a,b)
sess = tf.Session()
r, r2 = sess.run([y,y2], feed_dict={a:[3,1], b:[[4],[1]]})
print r
print r2
sess.close()
切片 slice
- begin 起點的坐標
- size 切片的大小
- begin[i] + size[i] <=input.shape[i]
分割 split, 沿着某一維度將tensor分割為n個tensor list
- 分割后list中的每個tensor的維度不降
input = tf.placeholder('float')
begin = tf.placeholder('int32')
size = tf.placeholder('int32')
out = tf.slice(input, begin, size)
s = tf.split(input, num_or_size_splits=3, axis=0)
sess= tf.Session()
input_ = [[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]
begin_ = [1, 0, 0]
size_ = [1,1,3]
o = sess.run(out, feed_dict={input:input_, begin:begin_, size:size_})
print o
s_ = sess.run(s, feed_dict={input:input_})
print(s_)
print(type(s_))
print(s_[0])
print(type(s_[0]))
print(type(s))
print(type(out))
sess.close()
[[[ 3. 3. 3.]]]
[array([[[ 1., 1., 1.],
[ 2., 2., 2.]]], dtype=float32), array([[[ 3., 3., 3.],
[ 4., 4., 4.]]], dtype=float32), array([[[ 5., 5., 5.],
[ 6., 6., 6.]]], dtype=float32)]
<type 'list'>
[[[ 1. 1. 1.]
[ 2. 2. 2.]]]
<type 'numpy.ndarray'>
<type 'list'>
<class 'tensorflow.python.framework.ops.Tensor'>
tensor concat 沿着某一維度連接tensor
tensor stack 將一系列rank=R的tensor 打包為rank=R+1的tensor
tensor pack 已被廢棄,用stack代替
t1 = tf.constant([[1,2,3],[4,5,6]])
t2 = tf.constant([[7,8,9],[10,11,12]])
c1= tf.concat([t1,t2], 0)
c2= tf.concat([t1,t2], 1)
r1 = tf.reshape(c1, [-1])
r2 = tf.reshape(c2, [-1])
p1 = tf.stack([t1,t2],0)
p2 = tf.stack([t1,t2],1)
p3 = [t1, t2]
with tf.Session() as sess:
print(sess.run([c1, c2]))
print(sess.run([tf.rank(t1),tf.rank(c1)]))
print("=======")
print(sess.run([r1, r2]))
print("=======")
print(sess.run([p1, p2]))
print(sess.run([tf.rank(t1),tf.rank(p1)]))
print(sess.run(tf.shape([p1, p2])))
print("=======")
print(sess.run(p3))
[array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]], dtype=int32), array([[ 1, 2, 3, 7, 8, 9],
[ 4, 5, 6, 10, 11, 12]], dtype=int32)]
[2, 2]
=======
[array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32), array([ 1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12], dtype=int32)]
=======
[array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]], dtype=int32), array([[[ 1, 2, 3],
[ 7, 8, 9]],
[[ 4, 5, 6],
[10, 11, 12]]], dtype=int32)]
[2, 3]
=======
[array([[1, 2, 3],
[4, 5, 6]], dtype=int32), array([[ 7, 8, 9],
[10, 11, 12]], dtype=int32)]
=======
[2 2 2 3]
c1 = tf.constant([1])
c2 = tf.constant([2])
c3 = tf.constant([3])
con = tf.concat([c1,c2,c3], 0)
with tf.Session() as sess:
print(sess.run(tf.shape(c1)[0]))
print(sess.run(con))
1
[1 2 3]
可以認為 tensor 是一個n維的數組(array)或列表(list), tensor具有靜態的type 和動態的維度
一個scalar 就是0維的數組,rank = 0,shape是[]
下面的
c1 rank=0, shap=[]
c2 rank=1, shap=[1]
c3 rank=1, shap=[2]
c1 = tf.constant(1)
c2 = tf.constant([1])
c3 = tf.constant([1,2])
s1 = tf.shape(c1)
s2 = tf.shape(c2)
s3 = tf.shape(c3)
with tf.Session() as sess:
c1_, c2_, c3_ = sess.run([c1,c2,c3])
print(c1_, c2_, c3_)
print(type(c1_), type(c2_), type(c3_))
print(c1_.shape)
print("================")
o1, o2,o3 = sess.run([s1,s2,s3])
print(o1, o2, o3)
print(type(o1), type(o2), type(o3))
print(o1.shape)
(1, array([1], dtype=int32), array([1, 2], dtype=int32))
(<type 'numpy.int32'>, <type 'numpy.ndarray'>, <type 'numpy.ndarray'>)
()
================
(array([], dtype=int32), array([1], dtype=int32), array([2], dtype=int32))
(<type 'numpy.ndarray'>, <type 'numpy.ndarray'>, <type 'numpy.ndarray'>)
(0,)
li = np.zeros(5)
li[0] = tf.constant(1) # tensor是個數組,哪怕是純量
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-66-07967146eb99> in <module>()
1 li = np.zeros(5)
----> 2 li[0] = tf.constant(1)
ValueError: setting an array element with a sequence.
placeholder, 不指定shape時候,可以feed任何shape的數據;指定的話,必須按照指定的shape feed 數據
x = tf.placeholder(shape=(2,1), dtype=tf.int32)
m = tf.placeholder(dtype=tf.int32)
y = x*2
n = m*2
with tf.Session() as sess:
m_ = [[2.0],[2.0]]
m_o = sess.run(m, feed_dict={m:m_})
print(m_)
print(m_o)
print(m_ - m_o)
print(type(m_), type(m_o))
print(sess.run(n, feed_dict={m:[[2,2],[1,1]]}))
[[2.0], [2.0]]
[[2]
[2]]
[[ 0.]
[ 0.]]
(<type 'list'>, <type 'numpy.ndarray'>)
[[4 4]
[2 2]]
li = []
li.append([tf.constant(1)])
li.append([tf.constant(2)])
li2 = tf.concat(li, axis=0)
with tf.Session() as sess:
print(sess.run(li))
print(sess.run(li2))
[[1], [2]]
[1 2]
li = []
li.append(tf.constant(1))
li.append(tf.constant(2))
li2 = tf.stack(li, axis=0)
#li2 = tf.concat(li, axis=0) # 錯誤, 純量不能使用cancat
with tf.Session() as sess:
print(sess.run(li))
print(sess.run(li2))
[1, 2]
[1 2]