|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Tensorflow.Operations |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// The context for the conditional construct. |
| 9 | + /// </summary> |
| 10 | + public class CondContext : ControlFlowContext |
| 11 | + { |
| 12 | + private string _name; |
| 13 | + /// <summary> |
| 14 | + /// The boolean tensor for the cond predicate |
| 15 | + /// </summary> |
| 16 | + private Tensor _pred; |
| 17 | + /// <summary> |
| 18 | + /// The predicate tensor in this branch |
| 19 | + /// </summary> |
| 20 | + private Tensor _pivot; |
| 21 | + /// <summary> |
| 22 | + /// 0 or 1 representing this branch |
| 23 | + /// </summary> |
| 24 | + private int _branch; |
| 25 | + /// <summary> |
| 26 | + /// |
| 27 | + /// </summary> |
| 28 | + private List<string> _values = new List<string>(); |
| 29 | + private Dictionary<string, Tensor> _external_values = new Dictionary<string, Tensor>(); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// |
| 33 | + /// </summary> |
| 34 | + /// <param name="pred">The `boolean` tensor for the conditional predicate.</param> |
| 35 | + /// <param name="pivot">The predicate tensor in this branch.</param> |
| 36 | + /// <param name="branch">0 or 1 representing this branch.</param> |
| 37 | + /// <param name="name">Name of the `CondContext` python object.</param> |
| 38 | + /// <param name="context_def"></param> |
| 39 | + /// <param name="import_scope"></param> |
| 40 | + public CondContext(Tensor pred, |
| 41 | + Tensor pivot, |
| 42 | + int branch, |
| 43 | + string name = "cond_text", |
| 44 | + object context_def = null, |
| 45 | + string import_scope = null) |
| 46 | + { |
| 47 | + _name = ops.get_default_graph().unique_name(name); |
| 48 | + if (context_def != null) |
| 49 | + throw new NotImplementedException("CondContext context_def is not null"); |
| 50 | + else |
| 51 | + { |
| 52 | + // Initializes the default fields. |
| 53 | + base.__init__(); |
| 54 | + _pred = pred; |
| 55 | + _pivot = pivot; |
| 56 | + |
| 57 | + // Values considered to have been already seen in this context. pred is not |
| 58 | + // included in this context. |
| 59 | + _values.Add(pred.name); |
| 60 | + _external_values[pred.name] = pred; |
| 61 | + _values.Add(pivot.name); |
| 62 | + pivot.op._set_control_flow_context(this); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public (Tensor, Tensor, Tensor) BuildCondBranch(Func<(Tensor, Tensor, Tensor)> fn) |
| 67 | + { |
| 68 | + // Add the subgraph defined by fn() to the graph. |
| 69 | + var pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION); |
| 70 | + var original_result = fn(); |
| 71 | + var post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION); |
| 72 | + |
| 73 | + return original_result; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments