我已经使用TensorFlow while_loop
和大矩阵实现了一个算法,我最近注意到了一些奇怪的行为:我在不同的运行中获得了不同的结果,有时甚至是nan
值.我花了一些时间来缩小问题范围,现在我有了以下最小的例子.我用一个大小的矩阵K 15000x15000
填充1,然后计算K⁵u为你的向量u.在一次迭代之后,我期望结果填充的向量15000
.但这不是发生的事情.
import numpy as np
import tensorflow as tf
n = 15000
np_kernel_mat = np.ones((n, n), dtype=np.float32)
kernel_mat = tf.constant(np_kernel_mat)
# for debugging
def compare_kernel(kernel_matrix):
print("AverageDifference:" + str(np.average(np.abs(np_kernel_mat - kernel_matrix))))
print("AmountDifferent:" + str(np.count_nonzero(np.abs(np_kernel_mat - kernel_matrix))))
return True
# body of the loop
def iterate(i, u):
# for debugging
with tf.control_dependencies(tf.py_func(compare_kernel, [kernel_mat], [tf.bool])):
u = tf.identity(u)
# multiply
u = tf.matmul(kernel_mat, u)
# check result and kernel
u = tf.Print(u, [tf.count_nonzero(tf.abs(kernel_mat-np_kernel_mat))], "AmountDifferentKernel: ") …
Run Code Online (Sandbox Code Playgroud) 我想使用带数组的call-by-reference并在这里找到一些似乎可以解决我的问题的东西.但是我现在将其更改为此,我收到错误消息
"模板参数演绎/替换失败".
如果我放的话它会起作用
bool a[3];
Run Code Online (Sandbox Code Playgroud)
但不是变量m.
#include <assert.h>
template <typename T, int Size>
void dosth(T (&a)[Size])
{
assert(Size > 2);
a[2] = false;
}
int main()
{
int m=3;
bool a[m];
dosth(a);
}
Run Code Online (Sandbox Code Playgroud)