小编Cas*_*sey的帖子

如何在自定义Tensorflow Op内核中生成随机张量

我正在为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==0i%4==1处重复,周期为4。其他数字似乎足够随机。 …

c++ eigen tensorflow

5
推荐指数
0
解决办法
117
查看次数

标签 统计

c++ ×1

eigen ×1

tensorflow ×1