Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/TensorFlowNET.Core/Clustering/KMeans.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using static Tensorflow.Python;

namespace Tensorflow.Clustering
{
/// <summary>
/// Creates the graph for k-means clustering.
/// </summary>
public class KMeans : Python
public class KMeans
{
public const string CLUSTERS_VAR_NAME = "clusters";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Tensorflow.Python;

namespace Tensorflow.Clustering
{
/// <summary>
/// Internal class to create the op to initialize the clusters.
/// </summary>
public class _InitializeClustersOpFactory : Python
public class _InitializeClustersOpFactory
{
Tensor[] _inputs;
Tensor _num_clusters;
Expand Down
3 changes: 2 additions & 1 deletion src/TensorFlowNET.Core/Framework/importer.py.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Linq;
using System.Text;
using static Tensorflow.OpDef.Types;
using static Tensorflow.Python;

namespace Tensorflow
{
public class importer : Python
public class importer
{
public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
Dictionary<string, Tensor> input_map = null,
Expand Down
3 changes: 2 additions & 1 deletion src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Linq;
using System.Text;
using System.Threading;
using static Tensorflow.Python;

namespace Tensorflow
{
public class gradients_impl : Python
public class gradients_impl
{
public static Tensor[] gradients(Tensor[] ys,
Tensor[] xs,
Expand Down
3 changes: 2 additions & 1 deletion src/TensorFlowNET.Core/Gradients/math_grad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Tensorflow.Python;

namespace Tensorflow.Gradients
{
/// <summary>
/// Gradients for operators defined in math_ops.py.
/// </summary>
public class math_grad : Python
public class math_grad
{
public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
{
Expand Down
221 changes: 111 additions & 110 deletions src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using Tensorflow.Operations.ControlFlows;
using static Tensorflow.Python;

namespace Tensorflow.Operations
{
Expand Down Expand Up @@ -46,9 +47,9 @@ public CondContext(Tensor pred = null,
if (pred == null && context_def == null) return;

_name = ops.get_default_graph().unique_name(name);
if (context_def != null)
{
_init_from_proto(context_def, import_scope: import_scope);
if (context_def != null)
{
_init_from_proto(context_def, import_scope: import_scope);
}
else
{
Expand All @@ -66,16 +67,16 @@ public CondContext(Tensor pred = null,
}
}

private void _init_from_proto(CondContextDef context_def, string import_scope = null)
{
var g = ops.get_default_graph();
_name = ops.prepend_name_scope(context_def.ContextName, import_scope);
var p1 = ops.prepend_name_scope(context_def.PredName, import_scope);
_pred = g.as_graph_element(p1) as Tensor;
var p2 = ops.prepend_name_scope(context_def.PivotName, import_scope);
_pivot = g.as_graph_element(p2) as Tensor;
_branch = context_def.Branch;
__init__(values_def: context_def.ValuesDef, import_scope: import_scope);
private void _init_from_proto(CondContextDef context_def, string import_scope = null)
{
var g = ops.get_default_graph();
_name = ops.prepend_name_scope(context_def.ContextName, import_scope);
var p1 = ops.prepend_name_scope(context_def.PredName, import_scope);
_pred = g.as_graph_element(p1) as Tensor;
var p2 = ops.prepend_name_scope(context_def.PivotName, import_scope);
_pivot = g.as_graph_element(p2) as Tensor;
_branch = context_def.Branch;
__init__(values_def: context_def.ValuesDef, import_scope: import_scope);
}

/// <summary>
Expand All @@ -90,8 +91,8 @@ public override Tensor AddValue(Tensor val)
// Use the real value if it comes from outer context. This is needed in
// particular for nested conds.
if (_external_values.ContainsKey(val.name))
result = _external_values[val.name];

result = _external_values[val.name];

result = result == null ? val : result;
}
else
Expand All @@ -107,10 +108,10 @@ public override Tensor AddValue(Tensor val)
}

with(ops.control_dependencies(null), ctrl =>
{
var results = control_flow_ops._SwitchRefOrTensor(result, _pred);
result = results[_branch];
if (_outer_context != null)
{
var results = control_flow_ops._SwitchRefOrTensor(result, _pred);
result = results[_branch];
if (_outer_context != null)
_outer_context.AddInnerOp(result.op);
});

Expand All @@ -127,87 +128,87 @@ public override Tensor AddValue(Tensor val)
}
_external_values[val.name] = result;
}
return result;
}

return result;
}

/// <summary>
/// Add the subgraph defined by fn() to the graph.
/// </summary>
public (T, Tensor) BuildCondBranch<T>(Func<T> fn)
{
// Add the subgraph defined by fn() to the graph.
var pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);
var original_result = fn();
var post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);

//TODO: port this chunck of missing code:
/*
if len(post_summaries) > len(pre_summaries):
new_summaries = post_summaries[len(pre_summaries):]
summary_ref = ops.get_collection_ref(ops.GraphKeys._SUMMARY_COLLECTION) # pylint: disable=protected-access
summary_ref[:] = pre_summaries
with ops.control_dependencies(new_summaries):
if original_result is None:
return no_op(), None
else:
original_result = nest.map_structure(array_ops.identity,
original_result)
*/
if (original_result == null)
return (original_result, null);

switch (original_result)
{
case Tensor result:
return (original_result, _BuildCondTensor(result));
case Operation op:
return (original_result, _BuildCondTensor(op));
public (T, Tensor) BuildCondBranch<T>(Func<T> fn)
{
// Add the subgraph defined by fn() to the graph.
var pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);
var original_result = fn();
var post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);

//TODO: port this chunck of missing code:
/*
if len(post_summaries) > len(pre_summaries):
new_summaries = post_summaries[len(pre_summaries):]
summary_ref = ops.get_collection_ref(ops.GraphKeys._SUMMARY_COLLECTION) # pylint: disable=protected-access
summary_ref[:] = pre_summaries
with ops.control_dependencies(new_summaries):
if original_result is None:
return no_op(), None
else:
original_result = nest.map_structure(array_ops.identity,
original_result)
*/
if (original_result == null)
return (original_result, null);

switch (original_result)
{
case Tensor result:
return (original_result, _BuildCondTensor(result));
case Operation op:
return (original_result, _BuildCondTensor(op));
case float[] fv:
{
var result = ops.convert_to_tensor(fv[0]);
return (original_result, _BuildCondTensor(result));
}
default:
return (original_result, null);
}
}

public (T[], Tensor[]) BuildCondBranch<T>(Func<T[]> fn)
{
// Add the subgraph defined by fn() to the graph.
var pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);
var original_result = fn();
var post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);

switch (original_result)
{
case Tensor[] results:
return (original_result, results.Select(_BuildCondTensor).ToArray());
case Operation[] results:
return (original_result, results.Select(_BuildCondTensor).ToArray());
case float[] fv:
var result = ops.convert_to_tensor(fv[0]);
return (original_result, new Tensor[] { result });
default:
return (original_result, new Tensor[0]);
}
}

private Tensor _BuildCondTensor(ITensorOrOperation v)
{
switch (v)
{
case Operation op:
// Use pivot as the proxy for this op.
return control_flow_ops.with_dependencies(new Operation[] { op }, _pivot);
case Tensor t:
return _ProcessOutputTensor(t);
default:
return _ProcessOutputTensor(ops.convert_to_tensor(v));

}
}

}
default:
return (original_result, null);
}
}

public (T[], Tensor[]) BuildCondBranch<T>(Func<T[]> fn)
{
// Add the subgraph defined by fn() to the graph.
var pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);
var original_result = fn();
var post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION);

switch (original_result)
{
case Tensor[] results:
return (original_result, results.Select(_BuildCondTensor).ToArray());
case Operation[] results:
return (original_result, results.Select(_BuildCondTensor).ToArray());
case float[] fv:
var result = ops.convert_to_tensor(fv[0]);
return (original_result, new Tensor[] { result });
default:
return (original_result, new Tensor[0]);
}
}

private Tensor _BuildCondTensor(ITensorOrOperation v)
{
switch (v)
{
case Operation op:
// Use pivot as the proxy for this op.
return control_flow_ops.with_dependencies(new Operation[] { op }, _pivot);
case Tensor t:
return _ProcessOutputTensor(t);
default:
return _ProcessOutputTensor(ops.convert_to_tensor(v));

}
}

/// <summary>
/// Process an output tensor of a conditional branch.
/// </summary>
Expand Down Expand Up @@ -238,7 +239,7 @@ private Tensor _ProcessOutputTensor(Tensor val)
}
return real_val;
}


protected override void _AddOpInternal(Operation op)
{
if (op.inputs.Length == 0)
Expand Down Expand Up @@ -324,20 +325,20 @@ public override bool back_prop
}
}

public CondContextDef to_proto(string export_scope)
{
throw new NotImplementedException();
}

public CondContext from_proto(CondContextDef proto, string import_scope)
{
var ret = new CondContext(context_def: proto, import_scope: import_scope);

ret.Enter();
foreach (var nested_def in proto.NestedContexts)
from_control_flow_context_def(nested_def, import_scope: import_scope);
ret.Exit();
return ret;
}
}
public CondContextDef to_proto(string export_scope)
{
throw new NotImplementedException();
}

public CondContext from_proto(CondContextDef proto, string import_scope)
{
var ret = new CondContext(context_def: proto, import_scope: import_scope);

ret.Enter();
foreach (var nested_def in proto.NestedContexts)
from_control_flow_context_def(nested_def, import_scope: import_scope);
ret.Exit();
return ret;
}
}
}
Loading