1 /** 2 Contains functions for generating random numbers. 3 4 Authors: Henry Gouk 5 */ 6 module dopt.core.ops.random; 7 8 import dopt.core.ops; 9 import dopt.core.types; 10 11 import std.array; 12 import std.functional; 13 import std.variant; 14 15 package 16 { 17 void initialize() 18 { 19 registerOperation("uniform", OpDef(toDelegate(&verifyUniform), toDelegate(&judgeRandom))); 20 } 21 } 22 23 private 24 { 25 bool verifyUniform(Operation op) 26 { 27 auto shape = "shape" in op.attributes; 28 29 return (shape !is null) && (shape.peek!(size_t[]) !is null); 30 } 31 32 TensorType judgeRandom(Operation op) 33 { 34 return TensorType(DataType.float32, op.attributes["shape"].get!(size_t[])); 35 } 36 } 37 38 public 39 { 40 /** 41 Creates a tensor that will contain different randomly generated data each time it is evaluated. 42 43 All elements in the tensor will be drawn from uniformly from the interval (0, 1] 44 45 Params: 46 shape = The shape of the random tensor 47 */ 48 Operation uniformSample(size_t[] shape, string mod = __MODULE__, size_t line = __LINE__) 49 { 50 return createOperation("uniform", [], ["shape": Variant(shape)], mod, line); 51 } 52 }