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
12 changes: 7 additions & 5 deletions src/TensorFlowNET.Core/APIs/tf.ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ namespace Tensorflow
{
public static partial class tf
{
public static object get_collection(string key, string scope = "") => get_default_graph()
.get_collection(key, scope: scope);
public static Tensor assign(Tensor @ref, object value, bool validate_shape = true, bool use_locking = true, string name = null)
=> state_ops.assign(@ref, value, validate_shape, use_locking, name);

public static object get_collection(string key, string scope = "")
=> get_default_graph().get_collection(key, scope: scope);

/// <summary>
/// Returns a context manager that creates hierarchical names for operations.
Expand All @@ -28,8 +31,7 @@ public static object get_collection(string key, string scope = "") => get_defaul
/// <param name="default_name">The default name to use if the name argument is None.</param>
/// <param name="values">The list of Tensor arguments that are passed to the op function.</param>
/// <returns>The scope name.</returns>
public static ops.NameScope name_scope(string name,
string default_name = "",
object values = null) => new ops.NameScope(name, default_name, values);
public static ops.NameScope name_scope(string name, string default_name = "", object values = null)
=> new ops.NameScope(name, default_name, values);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using NumSharp;
using Tensorflow.Framework;

namespace Tensorflow.Contrib.Learn.Estimators
{
public static class tensor_signature
{
public static bool is_compatible_with(this Tensor self, Tensor other)
{
bool _shape_is_compatible_0dim(Shape _this, Shape _other)
{
var __other = tensor_shape.as_shape(_other);
if (_this.Dimensions == null || __other.Dimensions == null)
return true;

if (_this.NDim != __other.NDim)
return false;

foreach (var (x_dim, y_dim) in _this.Dimensions.Zip(__other.Dimensions, (x_dim, y_dim) => (x_dim, y_dim)))
{
if (x_dim != y_dim)
return false;
}

return true;
}

if (other.is_sparse())
{
return self.dtype.is_compatible_with(other.dtype);
}

return self.dtype.is_compatible_with(other.dtype) &&
_shape_is_compatible_0dim(self.shape, other.shape) &&
!self.is_sparse();
}
}
}
13 changes: 12 additions & 1 deletion src/TensorFlowNET.Core/Framework/sparse_tensor.py.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
namespace Tensorflow.Framework
{
public static class SparseTensor
public interface _TensorLike
{ }

public class SparseTensor : CompositeTensor, _TensorLike
{
private static Tensor _dense_shape { get; set; }

}

public static class sparse_tensor
{
public static bool is_sparse(this _TensorLike x)
{
return x is SparseTensor;
}
}
}
34 changes: 34 additions & 0 deletions src/TensorFlowNET.Core/Framework/tensor_shape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Linq;
using System.Text;
using NumSharp;
using Tensorflow.Contrib.Learn.Estimators;

namespace Tensorflow.Framework
{
public static class tensor_shape
{
public static void assert_is_compatible_with(this Tensor self, Tensor other)
{
if (!self.is_compatible_with(other))
{
var selfDim = self.shape
.Aggregate(new StringBuilder("{"), (sb, i) => sb.Append(i).Append(", "), sb => sb.ToString())
.Replace(", }", "}");

var otherDim = other.shape
.Aggregate(new StringBuilder("{"), (sb, i) => sb.Append(i).Append(", "), sb => sb.ToString())
.Replace(", }", "}");

throw new ArgumentException($"Dimensions {selfDim} and {otherDim} are not compatible");
}
}

public static TensorShape as_shape(this Shape shape)
{
if (shape is TensorShape tshape)
return tshape;
return new TensorShape(shape);
}
}
}
57 changes: 56 additions & 1 deletion src/TensorFlowNET.Core/Operations/resource_variable_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ You may obtain a copy of the License at
limitations under the License.
******************************************************************************/

using System;
using Tensorflow.Framework;

namespace Tensorflow
{
/// <summary>
/// tensorflow\python\ops\resource_variable_ops.py
/// </summary>
public class resource_variable_ops
public static class resource_variable_ops
{
public static ITensorOrOperation shape_safe_assign_variable_handle(Tensor handle, int[] shape, Tensor value, string name = null)
{
Expand All @@ -29,9 +32,61 @@ public static ITensorOrOperation shape_safe_assign_variable_handle(Tensor handle
name: name);
}

/// <summary>
///
/// </summary>
/// <param name="self"></param>
/// <param name="value"></param>
/// <param name="use_locking"></param>
/// <param name="read_value"></param>
/// <returns>
/// If `read_value` is `True`, this method will return the new value of the
/// variable after the assignment has completed.Otherwise, when in graph mode
/// it will return the `Operation` that does the assignment, and when in eager
/// mode it will return `None`.
/// </returns>
public static Operation assign(this Tensor self, Tensor value, bool use_locking = false, string name = null, bool read_value = true)
{
var value_tensor = ops.convert_to_tensor(value, dtype: self.dtype);
self.assert_is_compatible_with(value_tensor);
var assign_op = gen_resource_variable_ops.assign_variable_op(self, value_tensor, name: name);
if (read_value)
{
return self._lazy_read(assign_op);
}

return assign_op;
}

public static Operation _lazy_read(this Tensor self, Operation op)
{
variable_accessed(self);
throw new NotImplementedException();
}

public static void variable_accessed(this Tensor variable)
{
throw new NotImplementedException();
}

public static bool is_resource_variable(VariableV1 var)
{
return var is ResourceVariable;
}

/// <summary>
/// Represents a future for a read of a variable.
/// Pretends to be the tensor if anyone looks.
/// </summary>
public class _UnreadVariable : BaseResourceVariable
{
}

/// <summary>
/// A python variable from an existing handle.
/// </summary>
public class BaseResourceVariable : VariableV1
{
}
}
}
5 changes: 4 additions & 1 deletion src/TensorFlowNET.Core/Tensors/Tensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Tensorflow.Framework;
using static Tensorflow.Python;

namespace Tensorflow
Expand All @@ -27,7 +28,7 @@ namespace Tensorflow
/// A tensor is a generalization of vectors and matrices to potentially higher dimensions.
/// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.
/// </summary>
public partial class Tensor : IDisposable, ITensorOrOperation
public partial class Tensor : IDisposable, ITensorOrOperation, _TensorLike
{
private IntPtr _handle;

Expand Down Expand Up @@ -109,6 +110,8 @@ public void SetShape(Shape shape)
this.shape = shape.Dimensions;
}

public int[] dims => shape;

/// <summary>
/// number of dimensions
/// 0 Scalar (magnitude only)
Expand Down
5 changes: 5 additions & 0 deletions src/TensorFlowNET.Core/Tensors/dtypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,10 @@ public static bool is_ref_dtype(this TF_DataType type)
{
return (int)type > 100;
}

public static bool is_compatible_with(this TF_DataType self, TF_DataType other)
{
return self.as_datatype_enum() == other.as_datatype_enum();
}
}
}
6 changes: 6 additions & 0 deletions src/TensorFlowNET.Core/Tensors/tensor_util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
else
nparray = Convert.ToString(values);
break;
case "Boolean":
if (values.GetType().IsArray)
nparray = np.array((bool[])values, np_dt);
else
nparray = Convert.ToBoolean(values);
break;
default:
throw new NotImplementedException($"make_tensor_proto: Support for type {np_dt.Name} Not Implemented");
}
Expand Down
2 changes: 2 additions & 0 deletions src/TensorFlowNET.Core/Variables/RefVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public partial class RefVariable : VariableV1, IProtoBuf<VariableDef, RefVariabl

public override string name => _variable.name;

public Tensor eval() => _variable;

public RefVariable(object initial_value = null,
bool trainable = true,
List<string> collections = null,
Expand Down
Loading