创建从numpy数组

时间:2017-06-23 21:11:56

标签: arrays numpy tensorflow

假设我有一个numpy阵列' A',我如何"同时"创建多个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

正如您所看到的,每次改组都是相互独立的,我该如何并行运行?

1 个答案:

答案 0 :(得分: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]]

希望这有帮助!