Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 将一维张量的元素除以相应的索引_Python 2.7_Tensorflow_Tensor - Fatal编程技术网

Python 2.7 将一维张量的元素除以相应的索引

Python 2.7 将一维张量的元素除以相应的索引,python-2.7,tensorflow,tensor,Python 2.7,Tensorflow,Tensor,我在Python2.7中使用Tensorflow 我有一个二元1-D张量,如下所示: a = [ 1 , 0, 1, 0, 1] 我想计算张量的每一个元素的比率之和,通过相应的指数 例如,我需要计算: [ 1/1 + 0/2 + 1/3 + 0/4 + 0/5 ] 是否有一个Tensorflow函数可以这样做?您可以使用和/运算符,如 # a is your 1-D binary tensor r = tf.range(1, a.get_shape()[0].value) result =

我在Python2.7中使用Tensorflow

我有一个二元1-D张量,如下所示:

a = [ 1 , 0, 1, 0, 1]
我想计算张量的每一个元素的比率之和,通过相应的指数

例如,我需要计算:

[ 1/1 + 0/2 + 1/3 + 0/4 + 0/5 ]
是否有一个Tensorflow函数可以这样做?

您可以使用和/运算符,如

# a is your 1-D binary tensor
r = tf.range(1, a.get_shape()[0].value)
result = a / r

注意:a和r必须是同一类型的

您可以使用
tf.shape
或作为Dzjkb的答案
get_shape
来获得张量大小

import tensorflow as tf
sess = tf.Session()
x = tf.constant([1,0,1,0,1])
y = tf.range(1, tf.shape(x)[0] + 1)
#y = tf.range(1, x.get_shape()[0].value + 1)
z = x / y
sess.run(z)
输出:

array([ 1.        ,  0.        ,  0.33333333,  0.        ,  0.2       ])