Tensorflow,根据另一个张量声明一个向量

Tensorflow, declare a vector depending on another tensor

我是 tensorflow 的新手。

我想使用 tensorflow 定义一个向量,它取决于我的神经网络的输出来计算所需的成本函数:

# Build the neural network
X = tf.placeholder(tf.float32, shape=[None, n_inputs], name='X')
hidden = fully_connected(X, n_hidden, activation_fn=tf.nn.elu, weights_initializer=initializer)
logits = fully_connected(hidden, n_outputs, activation_fn=None, weights_initializer=initializer)
outputs = tf.nn.softmax(logits)

# Select a random action based on the probability
action = tf.multinomial(tf.log(outputs), num_samples=1)

# Define the target if the action chosen was correct and the cost function
y = np.zeros(n_outputs)
y[int(tf.to_float(action))] = 1.0
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits)

要定义 y,我需要 action 的值(在 0 到 9 之间),这样我的向量 y 就是 [0,0,0,1,0 ...],索引 [=25 处的 1 =].

但是 action 是张量而不是整数,所以我不能那样做!

这段代码崩溃之前因为我无法将 int 应用于 Tensor 对象...

我该怎么办?

非常感谢

tf.one_hot() 是您要查找的函数。

您必须执行以下操作:

action_indices = tf.cast(action, tf.int32)
y = tf.one_hot(action_indices)