public static TensorFlow.TFScope WithScope(this TFGraph graph, string nameScopeDesc) { return(graph.WithScope(nameScopeDesc)); }
public static TensorFlow.TFOutput SeluGrad(this TFGraph graph, TensorFlow.TFOutput gradients, TensorFlow.TFOutput outputs, string operName = null) { return(graph.SeluGrad(gradients, outputs, operName)); }
public static TensorFlow.TFOutput ApplyProximalGradientDescent(this TFGraph graph, TensorFlow.TFOutput var, TensorFlow.TFOutput alpha, TensorFlow.TFOutput l1, TensorFlow.TFOutput l2, TensorFlow.TFOutput delta, System.Boolean?use_locking = null, string operName = null) { return(graph.ApplyProximalGradientDescent(var, alpha, l1, l2, delta, use_locking, operName)); }
public static TensorFlow.TFOutput MatrixLogarithm(this TFGraph graph, TensorFlow.TFOutput input, string operName = null) { return(graph.MatrixLogarithm(input, operName)); }
public static TensorFlow.TFOutput ReverseSequence(this TFGraph graph, TensorFlow.TFOutput input, TensorFlow.TFOutput seq_lengths, long seq_dim, long?batch_dim = null, string operName = null) { return(graph.ReverseSequence(input, seq_lengths, seq_dim, batch_dim, operName)); }
public static void Main(string [] args) { var files = options.Parse(args); if (dir == null) { dir = "/tmp"; //Error ("Must specify a directory with -m to store the training data"); } //if (files == null || files.Count == 0) // Error ("No files were specified"); if (files.Count == 0) { files = new List <string> () { "/tmp/demo.jpg" } } ; ModelFiles(dir); // Construct an in-memory graph from the serialized form. var graph = new TFGraph(); // Load the serialized GraphDef from a file. var model = File.ReadAllBytes(modelFile); graph.Import(model, ""); using (var session = new TFSession(graph)) { var labels = File.ReadAllLines(labelsFile); foreach (var file in files) { // Run inference on the image files // For multiple images, session.Run() can be called in a loop (and // concurrently). Alternatively, images can be batched since the model // accepts batches of image data as input. var tensor = ImageUtil.CreateTensorFromImageFile(file); var runner = session.GetRunner(); runner.AddInput(graph ["input"] [0], tensor).Fetch(graph ["output"] [0]); var output = runner.Run(); // output[0].Value() is a vector containing probabilities of // labels for each image in the "batch". The batch size was 1. // Find the most probably label index. var result = output [0]; var rshape = result.Shape; if (result.NumDims != 2 || rshape [0] != 1) { var shape = ""; foreach (var d in rshape) { shape += $"{d} "; } shape = shape.Trim(); Console.WriteLine($"Error: expected to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape [{shape}]"); Environment.Exit(1); } // You can get the data in two ways, as a multi-dimensional array, or arrays of arrays, // code can be nicer to read with one or the other, pick it based on how you want to process // it bool jagged = true; var bestIdx = 0; float p = 0, best = 0; if (jagged) { var probabilities = ((float [] [])result.GetValue(jagged: true)) [0]; for (int i = 0; i < probabilities.Length; i++) { if (probabilities [i] > best) { bestIdx = i; best = probabilities [i]; } } } else { var val = (float [, ])result.GetValue(jagged: false); // Result is [1,N], flatten array for (int i = 0; i < val.GetLength(1); i++) { if (val [0, i] > best) { bestIdx = i; best = val [0, i]; } } } Console.WriteLine($"{file} best match: [{bestIdx}] {best * 100.0}% {labels [bestIdx]}"); } } }
public static TensorFlow.TFOutput TensorSliceDataset(this TFGraph graph, TensorFlow.TFOutput[] components, TensorFlow.TFShape[] output_shapes, string operName = null) { return(graph.TensorSliceDataset(components, output_shapes, operName)); }
/* * public static TFOutput cond(this TFGraph g, TFOutput pred, Func<TFOutput> true_fn = null, Func<TFOutput> false_fn = null, bool strict = false, string operName = null) * { * using (var name = g.WithScope(g.MakeName("cond", operName))) * { * // Add the Switch to the graph. * (TFOutput p_2, TFOutput p_1) = g.Switch(pred, pred); * TFOutput pivot_1 = g.Identity(p_1, operName: "switch_t"); * TFOutput pivot_2 = g.Identity(p_2, operName: "switch_f"); * pred = g.Identity(pred, operName: "pred_id"); * * // Disable the fetching of tensors that are only on one branch of cond. * foreach (TFOutput tensor in new[] { p_1, p_2, pivot_1, pivot_2, pred }) * g.PreventFetching(tensor.Operation); * * // Build the graph for the true branch in a new context. * CondContext context_t = new CondContext(pred, pivot_1, branch: 1); * context_t.Enter(); * (TFTensor orig_res_t, TFTensor res_t) = context_t.BuildCondBranch(true_fn); * if (orig_res_t == null) * throw new ArgumentException("true_fn must have a return value."); * context_t.ExitResult(res_t); * context_t.Exit(); * * // Build the graph for the false branch in a new context. * CondContext context_f = new CondContext(pred, pivot_2, branch: 0); * context_f.Enter(); * (TFTensor orig_res_f, TFTensor res_f) = context_f.BuildCondBranch(false_fn); * if (orig_res_f == null) * throw new ArgumentException("false_fn must have a return value."); * context_f.ExitResult(res_f); * context_f.Exit(); * * if (!strict) * { * orig_res_t = _UnpackIfSingleton(orig_res_t); * orig_res_f = _UnpackIfSingleton(orig_res_f); * } * * // Check that the return values of the two branches have the same structure. * try * { * nest.assert_same_structure(orig_res_t, orig_res_f); * } * catch (InvalidOperationException e) * { * throw new InvalidOperationException("Incompatible return values of true_fn and false_fn", e); * } * * // Add the final merge to the graph. * if (res_t == null) * throw new ArgumentException("true_fn and false_fn must return at least one result."); * * TFTensor res_t_flat = nest.flatten(res_t); * TFTensor res_f_flat = nest.flatten(res_f); * * foreach (var (x, y) in Enumerable.Zip(res_t_flat, res_f_flat, (a, b) => (a, b))) * { * Trace.Assert((isinstance(x, ops.IndexedSlices) && * isinstance(y, ops.IndexedSlices)) || * (isinstance(x, sparse_tensor.SparseTensor) && * isinstance(y, sparse_tensor.SparseTensor)) || * (isinstance(x, ops.Tensor) && isinstance(y, ops.Tensor))); * val_x = isinstance(x, ops.Tensor) ? x : x.values; * val_y = isinstance(y, ops.Tensor) ? y : y.values; * if (val_x.dtype.base_dtype != val_y.dtype.base_dtype) * throw new ArgumentException("Outputs of true_fn and false_fn must have the same type: %s, %s" % (val_x.dtype.name, val_y.dtype.name)); * } * * merges = [merge(pair)[0] for pair in zip(res_f_flat, res_t_flat)]; * merges = _convert_flows_to_tensorarrays(nest.flatten(orig_res_t), merges); * * // Add to collections * ops.add_to_collection(ops.GraphKeys.COND_CONTEXT, context_t); * ops.add_to_collection(ops.GraphKeys.COND_CONTEXT, context_f); * * merges = nest.pack_sequence_as(structure: orig_res_t, flat_sequence: merges); * * // Singleton lists and tuples are automatically unpacked if strict == False. * if (!strict) * merges = _UnpackIfSingleton(merges); * return merges; * } * } */ public static void PreventFetching(this TFGraph g, TFOperation op) { }
public static TensorFlow.TFOutput SparseSegmentSumWithNumSegments(this TFGraph graph, TensorFlow.TFOutput data, TensorFlow.TFOutput indices, TensorFlow.TFOutput segment_ids, TensorFlow.TFOutput num_segments, string operName = null) { return(graph.SparseSegmentSumWithNumSegments(data, indices, segment_ids, num_segments, operName)); }
public static TensorFlow.TFOutput[] QueueDequeue(this TFGraph graph, TensorFlow.TFOutput handle, TensorFlow.TFDataType[] component_types, long?timeout_ms = null, string operName = null) { return(graph.QueueDequeue(handle, component_types, timeout_ms, operName)); }
public static TensorFlow.TFOutput Diag(this TFGraph graph, TensorFlow.TFOutput diagonal, string operName = null) { return(graph.Diag(diagonal, operName)); }
public static TensorFlow.TFOutput Mod(this TFGraph graph, TensorFlow.TFOutput x, TensorFlow.TFOutput y, string operName = null) { return(graph.Mod(x, y, operName)); }
public static string get_CurrentNameScope(this TFGraph graph) { return(graph.get_CurrentNameScope()); }
public static TensorFlow.TFOutput MatrixBandPart(this TFGraph graph, TensorFlow.TFOutput input, TensorFlow.TFOutput num_lower, TensorFlow.TFOutput num_upper, string operName = null) { return(graph.MatrixBandPart(input, num_lower, num_upper, operName)); }
public static TensorFlow.TFOutput SparseTensorDenseMatMul(this TFGraph graph, TensorFlow.TFOutput a_indices, TensorFlow.TFOutput a_values, TensorFlow.TFOutput a_shape, TensorFlow.TFOutput b, System.Boolean?adjoint_a = null, System.Boolean?adjoint_b = null, string operName = null) { return(graph.SparseTensorDenseMatMul(a_indices, a_values, a_shape, b, adjoint_a, adjoint_b, operName)); }
public static TensorFlow.TFOutput Cumsum(this TFGraph graph, TensorFlow.TFOutput x, TensorFlow.TFOutput axis, System.Boolean?exclusive = null, System.Boolean?reverse = null, string operName = null) { return(graph.Cumsum(x, axis, exclusive, reverse, operName)); }
public static TensorFlow.TFOutput MatMul(this TFGraph graph, TensorFlow.TFOutput a, TensorFlow.TFOutput b, System.Boolean?transpose_a = null, System.Boolean?transpose_b = null, string operName = null) { return(graph.MatMul(a, b, transpose_a, transpose_b, operName)); }
/// <summary> /// Initializes a new instance of <see cref="ModelCompilationContext"/> /// </summary> /// <param name="graph">Graph to use for compiling the model</param> public ModelCompilationContext(TFGraph graph) { Graph = graph; _parameters = new List <TFOutput>(); _initializers = new List <TFOperation>(); }
public static TensorFlow.TFOperation Assert(this TFGraph graph, TensorFlow.TFOutput condition, TensorFlow.TFOutput[] data, long?summarize = null, string operName = null) { return(graph.Assert(condition, data, summarize, operName)); }
public static TensorFlow.TFOutput TensorArrayWriteV3(this TFGraph graph, TensorFlow.TFOutput handle, TensorFlow.TFOutput index, TensorFlow.TFOutput value, TensorFlow.TFOutput flow_in, string operName = null) { return(graph.TensorArrayWriteV3(handle, index, value, flow_in, operName)); }
public static TensorFlow.TFOutput Floor(this TFGraph graph, TensorFlow.TFOutput x, string operName = null) { return(graph.Floor(x, operName)); }
public static TensorFlow.TFOutput ResizeNearestNeighbor(this TFGraph graph, TensorFlow.TFOutput images, TensorFlow.TFOutput size, System.Boolean?align_corners = null, string operName = null) { return(graph.ResizeNearestNeighbor(images, size, align_corners, operName)); }
public static TensorFlow.TFOutput StackPush(this TFGraph graph, TensorFlow.TFOutput handle, TensorFlow.TFOutput elem, System.Boolean?swap_memory = null, string operName = null) { return(graph.StackPush(handle, elem, swap_memory, operName)); }
/// <summary> /// Creates a session and graph from a saved session model /// </summary> /// <returns>On success, this populates the provided <paramref name="graph"/> with the contents of the graph stored in the specified model and <paramref name="metaGraphDef"/> with the MetaGraphDef of the loaded model.</returns> /// <param name="sessionOptions">Session options to use for the new session.</param> /// <param name="runOptions">Options to use to initialize the state (can be null).</param> /// <param name="exportDir">must be set to the path of the exported SavedModel.</param> /// <param name="tags">must include the set of tags used to identify one MetaGraphDef in the SavedModel.</param> /// <param name="graph">This must be a newly created graph.</param> /// <param name="metaGraphDef">On success, this will be populated on return with the contents of the MetaGraphDef (can be null).</param> /// <param name="status">Status buffer, if specified a status code will be left here, if not specified, a <see cref="T:TensorFlow.TFException"/> exception is raised if there is an error.</param> /// <remarks> /// This function creates a new session using the specified <paramref name="sessionOptions"/> and then initializes /// the state (restoring tensors and other assets) using <paramref name="runOptions"/> /// </remarks> public static TFSession FromSavedModel(TFSessionOptions sessionOptions, TFBuffer runOptions, string exportDir, string[] tags, TFGraph graph, string device, TFStatus status = null) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (tags == null) { throw new ArgumentNullException(nameof(tags)); } if (exportDir == null) { throw new ArgumentNullException(nameof(exportDir)); } var cstatus = TFStatus.Setup(status); unsafe { var h = TF_LoadSessionFromSavedModelOnDevice(sessionOptions.handle, runOptions == null ? null : runOptions.LLBuffer, exportDir, tags, tags.Length, graph.handle, device, cstatus.handle); if (cstatus.CheckMaybeRaise(status)) { return(new TFSession(h, graph)); } } return(null); }
public static TensorFlow.TFOutput Print(this TFGraph graph, TensorFlow.TFOutput input, TensorFlow.TFOutput[] data, string message = null, long?first_n = null, long?summarize = null, string operName = null) { return(graph.Print(input, data, message, first_n, summarize, operName)); }
public static TensorFlow.TFOutput MaxPoolGrad(this TFGraph graph, TensorFlow.TFOutput orig_input, TensorFlow.TFOutput orig_output, TensorFlow.TFOutput grad, long[] ksize, long[] strides, string padding, string data_format = null, string operName = null) { return(graph.MaxPoolGrad(orig_input, orig_output, grad, ksize, strides, padding, data_format, operName)); }
public void MNISTTwoHiddenLayerNetworkTest() { // Parameters var learningRate = 0.1f; var epochs = 5; var mnist = new Mnist(); mnist.ReadDataSets("/tmp"); int batchSize = 100; int numBatches = mnist.TrainImages.Length / batchSize; using (var graph = new TFGraph()) { var X = graph.Placeholder(TFDataType.Float, new TFShape(-1, 784)); var Y = graph.Placeholder(TFDataType.Float, new TFShape(-1, 10)); graph.Seed = 1; var initB = (float)(4 * Math.Sqrt(6) / Math.Sqrt(784 + 500)); var W1 = graph.Variable(graph.RandomUniform(new TFShape(784, 500), minval: -initB, maxval: initB), operName: "W1"); var b1 = graph.Variable(graph.Constant(0f, new TFShape(500), TFDataType.Float), operName: "b1"); var layer1 = graph.Sigmoid(graph.Add(graph.MatMul(X, W1.Read), b1.Read, operName: "layer1")); initB = (float)(4 * Math.Sqrt(6) / Math.Sqrt(500 + 100)); var W2 = graph.Variable(graph.RandomUniform(new TFShape(500, 100), minval: -initB, maxval: initB), operName: "W2"); var b2 = graph.Variable(graph.Constant(0f, new TFShape(100), TFDataType.Float), operName: "b2"); var layer2 = graph.Sigmoid(graph.Add(graph.MatMul(layer1, W2.Read), b2.Read, operName: "layer2")); initB = (float)(4 * Math.Sqrt(6) / Math.Sqrt(100 + 10)); var W3 = graph.Variable(graph.RandomUniform(new TFShape(100, 10), minval: -initB, maxval: initB), operName: "W3"); var b3 = graph.Variable(graph.Constant(0f, new TFShape(10), TFDataType.Float), operName: "b3"); var layer3 = graph.Add(graph.MatMul(layer2, W3.Read), b3.Read, operName: "layer3"); // No support for computing gradient for the SparseSoftmaxCrossEntropyWithLogits function // instead using SoftmaxCrossEntropyWithLogits var cost = graph.ReduceMean(graph.SoftmaxCrossEntropyWithLogits(layer3, Y, "cost").loss); var prediction = graph.ArgMax(graph.Softmax(layer3), graph.Const(1)); var labels = graph.ArgMax(Y, graph.Const(1)); var areCorrect = graph.Equal(prediction, labels); var accuracy = graph.ReduceMean(graph.Cast(areCorrect, TFDataType.Float)); var sgd = new SGD(graph, learningRate, 0.9f); var updateOps = sgd.Minimize(cost); using (var sesssion = new TFSession(graph)) { sesssion.GetRunner().AddTarget(graph.GetGlobalVariablesInitializer()).Run(); var expectedLines = File.ReadAllLines(Path.Combine(_testDataPath, "SGDMnist", "expected.txt")); for (int i = 0; i < epochs; i++) { var reader = mnist.GetTrainReader(); float avgLoss = 0; float avgAccuracy = 0; for (int j = 0; j < numBatches; j++) { var batch = reader.NextBatch(batchSize); var tensors = sesssion.GetRunner() .AddInput(X, batch.Item1) .AddInput(Y, batch.Item2) .AddTarget(updateOps).Fetch(cost, accuracy, prediction, labels).Run(); avgLoss += (float)tensors[0].GetValue(); avgAccuracy += (float)tensors[1].GetValue(); } var output = $"Epoch: {i}, loss(Cross-Entropy): {avgLoss / numBatches:F4}, Accuracy:{avgAccuracy / numBatches:F4}"; Assert.Equal(expectedLines[i], output); } } } }
public static TensorFlow.TFOperation SaveSlices(this TFGraph graph, TensorFlow.TFOutput filename, TensorFlow.TFOutput tensor_names, TensorFlow.TFOutput shapes_and_slices, TensorFlow.TFOutput[] data, string operName = null) { return(graph.SaveSlices(filename, tensor_names, shapes_and_slices, data, operName)); }
public static TensorFlow.TFOutput SparseSegmentMean(this TFGraph graph, TensorFlow.TFOutput data, TensorFlow.TFOutput indices, TensorFlow.TFOutput segment_ids, string operName = null) { return(graph.SparseSegmentMean(data, indices, segment_ids, operName)); }
public static TensorFlow.TFOutput RefSelect(this TFGraph graph, TensorFlow.TFOutput index, TensorFlow.TFOutput[] inputs, string operName = null) { return(graph.RefSelect(index, inputs, operName)); }