【问题标题】:Tensorflow, declare a vector depending on another tensorTensorflow,根据另一个张量声明一个向量
【发布时间】:2018-04-01 16:54:24
【问题描述】:

我是 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 ...],其中 1 在索引“action”处。

但是动作是张量而不是整数,所以我不能这样做!

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

我该怎么办?

非常感谢

【问题讨论】:

标签: python tensorflow tensor


【解决方案1】:

tf.one_hot() 是您正在寻找的功能。

您必须执行以下操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多