python - 相当于 tensorflow 中的 np.add.at

标签 python numpy tensorflow numpy-ufunc

如何将 np.add.at 语句转换为 tensorflow?

np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D))

编辑

self.dW.shape 为(V, D), self.D.shape 为(N, D) self.x.size 为N

最佳答案

对于 np.add.at,您可能想查看 tf.SparseTensor ,它通过值列表和索引列表表示张量(更适合稀疏数据,因此得名)。

所以对于你的例子:

np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D))

那将是(假设 dWxdout 是张量):

tf.sparse_add(dW, tf.SparseTensor(x, tf.reshape(dout, [-1])))

这是假设 x 的形状是 [n, nDims](即 x 是 n 个索引的“列表”,每个维度 nDims),dout 的形状为 [n]

关于python - 相当于 tensorflow 中的 np.add.at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40389822/

相关文章:

python - Keras/ tensorflow : Train multiple models on the same GPU in a loop or using Process

python - Tensorflow 导入元图占位符未提供

python - 如何编写 Keras 自定义指标来过滤或屏蔽某些值?

python - Scipy.Optimise Curve_fit 的指数拟合不起作用

python导入问题

python - 如何使用其他现有类别覆盖 pandas DataFrames 中的列

python-3.x - 为什么 numpy 对这个逆矩阵给出了错误的答案?

python - tf.estimator shuffle - 随机种子?

Python - 执行 shell 命令在 Linux 上不起作用

python - 如何在给定的不规则日期对时间序列重新采样