1. 程式人生 > >np.tile(A,reps)建立一個重複數/陣列A reps 次的陣列;tensor轉換為numpy陣列:

np.tile(A,reps)建立一個重複數/陣列A reps 次的陣列;tensor轉換為numpy陣列:

>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
       [[0, 1, 2, 0, 1, 2]]])
>>>
>>> b = np.array([[1, 2], [3, 4]])
>>> np.tile(b, 2)
array([[1, 2, 1, 2],
       [3, 4, 3, 4]])
>>> np.tile(b, (2, 1))
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])
>>>
>>> c = np.array([1,2,3,4])
>>> np.tile(c,(4,1))
array([[1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4]])

2.tensor轉換為numpy陣列:

對於tensorflow中的tensor “T”,可以用:

T.eval() 函式來轉換為np陣列,
也可以用tf.convert_to_tensor(numpy陣列)來把一個numpy陣列轉換為一個tensor。如:

import tensorflow as tf
img1 = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]]]],dtype=tf.float32)
img2 = tf.constant(value=[[[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]],[[1],[1],[1],[1]]]],dtype=tf.float32)
img = tf.concat(values=[img1,img2],axis=3)
sess=tf.Session()
#sess.run(tf.initialize_all_variables())
sess.run(tf.global_variables_initializer())
print("out1=",type(img))
#轉化為numpy陣列
img_numpy=img.eval(session=sess)
print("out2=",type(img_numpy))
#轉化為tensor
img_tensor= tf.convert_to_tensor(img_numpy)
print("out2=",type(img_tensor))

---------------------

本文來自 ljs_a 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/ljs_a/article/details/78758116?utm_source=copy