Constructs a new session.
Not copyable
Closes and deletes input/output values explicitly.
Runs session to evaluate outputs by given inputs.
Runs in python-like usage.
nothrow, nogc, and safe usage
import std.typecons : tuple; import tfd.tensor : tensor, Tensor; import tfd.graph : newGraph; import tfd.op : Operation; with (newGraph) { Operation x = placeholder!int("x"); Operation two = constant(2); Operation add = x + two; Operation[1] inops; inops[0] = x; Tensor[1] inputs; inputs[0] = 3.tensor; Operation[1] outops; outops[0] = add; Tensor[1] outputs; session.run(inops, inputs, outops, outputs); assert(outputs[0].scalar!int == 5); write("tmp.pb"); } with (newGraph) { read("tmp.pb"); // auto x = operationByName("x"); // auto add = operationByName("add"); }
TODO(karita): more interesting example. e.g., logistic regression.
import tfd; /// scalar add with (newGraph) { Operation x = placeholder!int("x"); Operation two = constant(2); Operation add = x + two; Tensor addVal = session.run([add], [x: 3.tensor])[0]; assert(addVal.scalar!int == 5); } /// tensor add with (newGraph) { import mir.ndslice : as, iota; auto i = iota(2, 3, 4).as!float; Operation x = placeholder!float("x", 2, 3, 4); Operation two = constant(i); Operation add = x + two; Tensor addVal = session.run([add], [x: i.tensor])[0]; assert(addVal.sliced!(float, 3) == i * 2); }
Wrapper class for TF_Session.