创建从数组的多个Tensorflow常数



假设我有一个numpy array'a',我如何"同时"创建多个Tensorflow常数,每个tensorflow常数来自数组'a'?

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

您可以看到,每个改组都是彼此独立的,我该如何并行运行?

shuffle

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)

ouput

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]]

希望这会有所帮助!

最新更新