2017-06-23 61 views
0

假設我有一個numpy數組'A',我該如何「同時」創建多個tensorflow常量,每個數組'a'都有一個shuffle?創建從numpy陣列洗牌的多個tensorflow常量

A = np.random.rand(5000) 
c1 = tf.constant(shuffle(A)) # <- how does this shuffle implement? 
c2 = tf.constant(shuffle(A)) # <- how does this shuffle implement? 
.... more constants follow 

正如你所看到的,每個這種混洗是相互獨立的,我該如何平行運行它們?

+0

我的回答對你有幫助嗎? – hars

回答

0

洗牌

import tensorflow as tf 
import numpy as np 

A=np.random.randint(10,size=(10)) 
c1 = tf.constant(A) 
c1_s = tf.random_shuffle(c1) 
sess = tf.Session() 
print sess.run(c1) 
print sess.run(c1_s) 

輸出:

[9 7 2 6 9 1 2 4 2 2] 
[2 1 2 9 7 2 4 9 6 2] 

一起

import tensorflow as tf 
import numpy as np 

A=np.random.randint(10,size=(10)) 
N = 3 
At = tf.constant(A) 
Bt = (tf.tile(tf.expand_dims(At, 0), [N,1])) 
fn_to_map = lambda x: tf.random_shuffle(x) # Where `f` instantiates myCustomOp. 
Ct = tf.map_fn(fn_to_map, Bt) 

sess = tf.Session() 
At_v,Bt_v,Ct_v = sess.run([At,Bt,Ct]) 

print 'A' 
print At_v 
print 'B' 
print sess.run(Bt) 
print 'C' 
print sess.run(Ct) 

輸出繼電器

A 
[4 3 2 0 7 1 9 1 2 4] 
B 
[[4 3 2 0 7 1 9 1 2 4] 
[4 3 2 0 7 1 9 1 2 4] 
[4 3 2 0 7 1 9 1 2 4]] 
C 
[[4 3 7 9 1 2 2 4 0 1] 
[0 1 2 4 4 2 1 9 3 7] 
[1 2 9 4 7 3 2 0 1 4]] 

希望這有助於!