0

我有这个使用Tensorflow 框架的 python 函数:

def compute_ap(gain_vector):

    #this vector must fit the dimension of the gain_vector
    index_vector = tf.range(1, gain_vector.get_shape()[0],dtype=tf.float32)

    ap = tf.div(tf.reduce_sum(tf.div(tf.cast(gain_vector,tf.float32), index_vector), 1),tf.reduce_sum(tf.cast(gain_vector,tf.float32), 1))
    return ap 

当我运行程序时,我得到这个错误:

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("inputs/strided_slice:0", shape=(), dtype=int32)'

好像gain_vector.get_shape()[0]没有得到增益向量的向量,有什么问题?

4

1 回答 1

2

tf.range()只接受 type 的参数int32

Args:
start: 类型的 0-D(标量)int32。顺序的第一个条目。
默认为 0。

因此,您可以创建一个int32张量并将其投射到float32以后。所以,使用这样的东西:

In [80]: index_vector = tf.range(1, tf.shape(gain_vector)[0])
In [81]: vec_float32 = tf.cast(index_vector, dtype=tf.float32)
于 2017-04-08T16:03:07.257 回答