java 线性计算_计算线性指数Tensorflow

下午好 . 我继续遇到通过索引更新张量流中的随机元素的问题 . 我想随机选择索引(例如,一半),然后设置为零元素对应于那些索引 . 这是有问题的部分:

with tf.variable_scope("foo", reuse=True):

temp_var = tf.get_variable("W")

size_2a = tf.get_variable("b")

s1 = tf.shape(temp_var).eval()[0]

s2 = tf.shape(size_2a).eval()[0]

row_indices = tf.random_uniform(dtype=tf.int32, minval=0, maxval = s1 - 1, shape=[s1]).eval()

col_indices = tf.random_uniform(dtype=tf.int32, minval=0, maxval = s2 - 1, shape=[s2]).eval()

ones_mask = tf.ones([s1,s2])

# turn 'ones_mask' into 1d variable since "scatter_update" supports linear indexing only

ones_flat = tf.Variable(tf.reshape(ones_mask, [-1]))

# no automatic promotion, so make updates float32 to match ones_mask

updates = tf.zeros(shape=(s1,), dtype=tf.float32)

# get linear indices

linear_indices = row_indices*s2 + tf.reshape(col_indices,s1*s2)

ones_flat = tf.scatter_update(ones_flat, linear_indices/2, updates)

#I want to set to zero only half of all elements,that's why linear_indices/2

# convert back into original shape

ones_mask = tf.reshape(ones_flat, ones_mask.get_shape())

它给了我ValueError:不能用10个元素重塑一个张量来为'foo_1 / Reshape_1'(op:'Reshape')塑造[784,10](7840个元素)输入形状:[10],[2] . 但是我不知道如何在没有重塑的情况下来到这里(我试图重塑s1和s2,没有用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值