在运行时使用 Tensorflow 拟合张量的索引向量



我有这个使用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] 没有得到增益向量的向量,问题是什么?

tf.range()只接受类型为 int32 的参数。

参数:
start:类型 int32 的 0-D(标量(。按顺序排列的第一个条目。
默认值为 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)

最新更新