【问题标题】:Create multiple tensorflow constants shuffled from a numpy array从 numpy 数组中创建多个 tensorflow 常量
【发布时间】:2017-06-23 21:11:55
【问题描述】:

假设我有一个 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

如您所见,每个洗牌都是相互独立的,我该如何并行运行它们?

【问题讨论】:

  • 我的回答对你有帮助吗?

标签: arrays numpy tensorflow


【解决方案1】:

随机播放

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2018-08-09
    • 2016-03-25
    • 2019-10-05
    • 2018-01-15
    • 2017-12-30
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多