简体   繁体   English

交互式会话eval和tf.random_uniform无法产生预期的输出

[英]interactive session eval and tf.random_uniform do not produce expected output

I have a problem with the following code piece, the obs_pattern , obs_seq and obs_seq_s do not produce the expected behavior. 我对以下代码段有问题, obs_patternobs_seqobs_seq_s无法产生预期的行为。 I tried with TensorFlow 1.2.1. 我尝试使用TensorFlow 1.2.1。 I suspect there is something wrong. 我怀疑有问题。

import tensorflow as tf

sess = tf.InteractiveSession()
seq_length = 5
num_bits = 4
obs_pattern_shape = [num_bits, seq_length]
obs_pattern = tf.cast(
    tf.random_uniform(obs_pattern_shape, minval=0, maxval=2, seed=1234, dtype=tf.int32), 
    tf.float32)
print(obs_pattern.eval())
seq_length_zeros = tf.zeros([1, seq_length])
obs_seq = tf.concat([obs_pattern, seq_length_zeros], axis=0)
print(obs_seq.eval())
add_vec = tf.one_hot([num_bits], (num_bits + 1), on_value = 1.0, off_value=0.0, axis=0)
obs_seq_s = tf.concat([obs_seq, add_vec], axis=1)
print(obs_seq_s.eval())

sess.close()

obs_pattern obs_pattern

[[ 1.  1.  1.  0.  0.]
[ 0.  0.  0.  1.  0.]
[ 0.  1.  1.  0.  0.]
[ 0.  1.  0.  0.  0.]]

obs_seq obs_seq

[[ 1.  1.  0.  1.  1.]
[ 0.  1.  1.  0.  1.]
[ 1.  1.  1.  0.  0.]
[ 1.  1.  0.  1.  0.]
[ 0.  0.  0.  0.  0.]]

obs_seq_s obs_seq_s

[[ 1.  0.  0.  0.  0.  0.]
[ 0.  0.  1.  0.  0.  0.]
[ 0.  1.  0.  0.  0.  0.]
[ 0.  1.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  1.]]

EDIT: Based on the comment below I changed obs_pattern and it behaves as I'd thought 编辑:基于下面的评论,我更改了obs_pattern,它的行为与我想象的一样

import numpy as np
arr = np.random.randint(2, size=(num_bits, seq_length))
obs_pattern = tf.convert_to_tensor(arr, dtype=tf.float32)

It is expected each time you run sess on a tensor formed with obs_pattern it reruns a new random uniform. 期望每次在由obs_pattern形成的张量上运行sess时,它都会重新运行新的随机制服。 To get the behavior you expect generate ONCE a numpy array with numpy then feed it to a placeholder of the same shape : obs_pattern. 为了得到你的行为产生预期一旦与numpy的一个numpy的阵列,然后将其提供给同一形状的占位符:obs_pattern。 To clarify you used a seed but you ran the op a number of times so you get the first iterates of the sequence. 为了明确起见,您使用了一个种子, 但是您多次运行op,因此获得了序列的第一个迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM