¿Cómo usar un número aleatorio en la operación de tensorflow definida por el usuario?

¿Cómo usar un número aleatorio en la operación de tensorflow definida por el usuario?

Estoy escribiendo una operación en cpp que necesita un número aleatorio en la función Calcular.

pero parece que no debería usar la biblioteca aleatoria cpp directamente, ya que eso no puede ser controlado portf.set_random_seed.

Mi código actual es algo como lo siguiente, ¿qué debo hacer en la funciónsome_interesting_random_function ?

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/common_shape_fns.h"
#include <iostream>
#include <typeinfo>
#include <random>

using namespace tensorflow;

REGISTER_OP("MyRandom")
.Output("random: int32")
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
        c->set_output(0, c->Scalar());
        return Status::OK();
});

int some_interesting_random_function(){
    return 10;
}

class MyRandomOp : public OpKernel {
    public:
        explicit MyRandomOp(OpKernelConstruction* context) : OpKernel(context) {}
        void Compute(OpKernelContext* context) override {
            Tensor* res;
            TensorShape shape;
            int dims[] = {};
            TensorShapeUtils::MakeShape(dims, 0, &shape);
            OP_REQUIRES_OK(context, context->allocate_output(0, shape,
                        &res));
            auto out1 = res->flat<int32>();
            out1(0) = some_interesting_random_function();
        }
};

REGISTER_KERNEL_BUILDER(Name("MyRandom").Device(DEVICE_CPU), MyRandomOp);

Respuestas a la pregunta(1)

Su respuesta a la pregunta