我正在为GPU编写自定义张量流Op,作为正向计算输出的一部分,我需要一个形状为(N,H,W)的随机张量。
template <typename Device, typename T>
struct TensorRandom {
void operator()(const Device& d, typename TTypes<T>::Flat t) {
t.device(d) = t.random(); // this is my only change to the TensorZeros function
}
};
Run Code Online (Sandbox Code Playgroud)
在我的op的实现/内核中,我有一些类似的代码
TensorShape my_shape({N, H, W});
Tensor* random_mat;
OP_REQUIRES_OK(ctx, ctx->allocate_output("random_mat", my_shape, &random_mat));
const Device& device = ctx->eigen_device<Device>(); // this will be a gpu
functor::TensorRandom<Device, T>()(device, random_mat.flat<T>());
VLOG(1) << "Random Mat " << random_mat.shape().DebugString()
<< random_mat.SummarizeValue(N * H * W);
Run Code Online (Sandbox Code Playgroud)
当我编译并运行它时,我在下面得到这个奇怪的模式。如果将张量展平,每个元素在i%4==0或i%4==1处重复,周期为4。其他数字似乎足够随机。 …