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
18 changes: 18 additions & 0 deletions src/TensorFlowNET.Core/APIs/tf.distributions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow
{
public static partial class tf
{
public static class distributions
{
public static Normal Normal(Tensor loc,
Tensor scale,
bool validate_args = false,
bool allow_nan_stats = true,
string name = "Normal") => new Normal(loc, scale, validate_args = false, allow_nan_stats = true, "Normal");
}
}
}
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Core/APIs/tf.layers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow.Layers;
using Tensorflow.Keras.Layers;
using Tensorflow.Operations.Activation;

namespace Tensorflow
Expand Down
2 changes: 2 additions & 0 deletions src/TensorFlowNET.Core/APIs/tf.math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static partial class tf

public static Tensor sub(Tensor a, Tensor b) => gen_math_ops.sub(a, b);

public static Tensor sqrt(Tensor a, string name = null) => gen_math_ops.sqrt(a, name);

public static Tensor subtract<T>(Tensor x, T[] y, string name = null) where T : struct
=> gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name);

Expand Down
12 changes: 12 additions & 0 deletions src/TensorFlowNET.Core/Framework/sparse_tensor.py.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow.Framework
{
public static class SparseTensor
{
private static Tensor _dense_shape { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
using System.Text;



namespace Tensorflow
{
abstract class _BaseDistribution : Python
public class _BaseDistribution : Python
{
// Abstract base class needed for resolving subclass hierarchy.
}
Expand All @@ -16,10 +17,10 @@ abstract class _BaseDistribution : Python
/// Distribution is a base class for constructing and organizing properties
/// (e.g., mean, variance) of random variables (e.g, Bernoulli, Gaussian).
/// </summary>
class Distribution : _BaseDistribution
public class Distribution : _BaseDistribution
{
public TF_DataType _dtype {get;set;}
public ReparameterizationType _reparameterization_type {get;set;}
//public ReparameterizationType _reparameterization_type {get;set;}
public bool _validate_args {get;set;}
public bool _allow_nan_stats {get;set;}
public Dictionary<string, object> _parameters {get;set;}
Expand Down
15 changes: 8 additions & 7 deletions src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Collections.Generic;
using Tensorflow;

namespace Tensorflow
{
class Normal : Distribution
public class Normal : Distribution
{
public Tensor _loc { get; set; }
public Tensor _scale { get; set; }

Dictionary<string, object> parameters = new Dictionary<string, object>();
public Dictionary<string, object> parameters = new Dictionary<string, object>();
/// <summary>
/// The Normal distribution with location `loc` and `scale` parameters.
/// Mathematical details
Expand Down Expand Up @@ -39,7 +40,7 @@ public Normal (Tensor loc, Tensor scale, bool validate_args=false, bool allow_na
this._loc = array_ops.identity(loc, name);
this._scale = array_ops.identity(scale, name);
base._dtype = this._scale.dtype;
base._reparameterization_type = new ReparameterizationType("FULLY_REPARAMETERIZED");
// base._reparameterization_type = new ReparameterizationType("FULLY_REPARAMETERIZED");
base._validate_args = validate_args;
base._allow_nan_stats = allow_nan_stats;
base._parameters = parameters;
Expand All @@ -56,25 +57,25 @@ public Normal (Tensor loc, Tensor scale, bool validate_args=false, bool allow_na
/// <returns></returns>
public Tensor loc()
{
return this._loc;
return _loc;
}
/// <summary>
/// Distribution parameter for standard deviation."
/// </summary>
/// <returns></returns>
public Tensor scale()
{
return this._scale;
return _scale;
}

public Tensor _batch_shape_tensor()
{
return array_ops.broadcast_dynamic_shape(array_ops.shape(this._loc), array_ops.shape(this._scale));
return array_ops.broadcast_dynamic_shape(array_ops.shape(_loc), array_ops.shape(_scale));
}

public Tensor _batch_shape()
{
return array_ops.broadcast_static_shape(new Tensor(this._loc.shape), new Tensor(this._scale.shape));
return array_ops.broadcast_static_shape(new Tensor(_loc.shape), new Tensor(_scale.shape));
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/TensorFlowNET.Core/Operations/OpDefLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value)
case "list(type)":
attr_value.List.Type.AddRange((value as IList<TF_DataType>).Select(x => _MakeType(x, attr_def)));
break;
case "list(int)":
attr_value.List.I.AddRange((value as int[]).Select(x => Convert.ToInt64(x)));
break;
case "bool":
attr_value.B = (bool)value;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/TensorFlowNET.Core/Operations/gen_array_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static Tensor zeros_like(Tensor x, string name = null)
}
public static Tensor stop_gradient(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("StopGradient", name, args: new { x });
var _op = _op_def_lib._apply_op_helper("StopGradient", name, args: new { input = x, name });

return _op.outputs[0];
}
Expand All @@ -181,7 +181,7 @@ public static Tensor stop_gradient(Tensor x, string name = null)
/// <returns> A `Tensor`. Has the same type as `input`.</returns>
public static Tensor squeeze(Tensor input, int[] axis = null, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Squeeze", name, args: new { input, axis, name });
var _op = _op_def_lib._apply_op_helper("Squeeze", name, args: new { input, squeeze_dims = axis });

return _op.outputs[0];
}
Expand Down
16 changes: 15 additions & 1 deletion src/TensorFlowNET.Core/Operations/gen_math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public static Tensor mean(Tensor input, Tensor axis, bool keep_dims= false, stri
return _op.outputs[0];
}

public static Tensor mean(Tensor input, int[] axis, bool keep_dims = false, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims, name });

return _op.outputs[0];
}

public static Tensor add(Tensor x, Tensor y, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Add", name, args: new { x, y });
Expand All @@ -36,7 +43,7 @@ public static Tensor add(Tensor x, Tensor y, string name = null)

public static Tensor squared_difference(Tensor x, Tensor y, string name = null)
{
var _op = _op_def_lib._apply_op_helper("SquaredDifference", name, args: new { x, y });
var _op = _op_def_lib._apply_op_helper("SquaredDifference", name, args: new { x, y, name });

return _op.outputs[0];
}
Expand All @@ -55,6 +62,13 @@ public static Tensor neg(Tensor x, string name = null)
return _op.outputs[0];
}

public static Tensor sqrt(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Sqrt", name, args: new { x });

return _op.outputs[0];
}

public static Tensor sub<Tx, Ty>(Tx x, Ty y, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Sub", name, args: new { x, y });
Expand Down
17 changes: 9 additions & 8 deletions src/TensorFlowNET.Core/Operations/math_ops.py.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NumSharp.Core;
using System;
using System.Collections.Generic;
using System.Text;

Expand Down Expand Up @@ -37,8 +38,8 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
/// <param name="name"> A name for the operation (optional).</param>
public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
{
var r = _ReductionDims(input_tensor, new Tensor(axis));
var m = gen_math_ops.mean(input_tensor, r);
var r = _ReductionDims(input_tensor, axis);
var m = gen_math_ops.mean(input_tensor, (int[]) r, keepdims, name);
return _may_reduce_to_scalar(keepdims,axis, m);
}
/// <summary>
Expand Down Expand Up @@ -135,8 +136,8 @@ private static Tensor _ReductionDims(Tensor x, Tensor axis)
return range(0, rank, 1);
}
}

private static int[] _ReductionDims(Tensor x, int[] axis)
private static object _ReductionDims(Tensor x, int[] axis)
{
if (axis != null)
{
Expand All @@ -147,12 +148,12 @@ private static int[] _ReductionDims(Tensor x, int[] axis)
var rank = array_ops.rank(x);
if (rank != null)
{
// return constant_op.constant();
return constant_op.constant(np.arange(rank), TF_DataType.TF_INT32);
}
// return range(0, rank, 1);
throw new NotFiniteNumberException();
return range(0, rank, 1);
}
}


public static Tensor range(object start, object limit = null, object delta = null, TF_DataType dtype = TF_DataType.DtInvalid, string name = "range" )
{
Expand Down
6 changes: 3 additions & 3 deletions src/TensorFlowNET.Core/Operations/nn_impl.py.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public static (Tensor, Tensor) moments(Tensor x,
// on 32-bit floats before converting the mean and variance back to fp16
var y = math_ops.cast(x, TF_DataType.TF_FLOAT);
// Compute true mean while keeping the dims for proper broadcasting.
var mean = math_ops.reduce_mean(y, axes, keep_dims = true, name = "mean");
var mean = math_ops.reduce_mean(y, axes, true, name = "mean");
// Sample variance, not unbiased variance
// Note: stop_gradient does not change the gradient that gets
// backpropagated to the mean from the variance calculation,
// because that gradient is zero
var variance = math_ops.reduce_mean(math_ops.reduce_mean(math_ops.square_difference(y, array_ops.stop_gradient(mean)), axes, keep_dims = true, name = "Variance"));
var variance = math_ops.reduce_mean(math_ops.square_difference(y, array_ops.stop_gradient(mean)), axes, true, name = "Variance");
if (!keep_dims)
{
mean = array_ops.squeeze(mean, axes);
variance = array_ops.squeeze(variance, axes);
}
// TODO: if x.dtype == dtypes.float16:
if (x.dtype == TF_DataType.TF_FLOAT)
if (x.dtype == TF_DataType.TF_HALF)
return (math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
else
return (mean, variance);
Expand Down
3 changes: 2 additions & 1 deletion test/TensorFlowNET.Examples/NaiveBayesClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public void fit(NDArray X, NDArray y)
var tup = tf.nn.moments(cons, new int[]{1});
var mean = tup.Item1;
var variance = tup.Item2;

// Create a 3x2 univariate normal distribution with the
// Known mean and variance
// var dist = tf.distributions.Normal(loc=mean, scale=tf.sqrt(variance));
var dist = tf.distributions.Normal(mean, tf.sqrt(variance));

}

Expand Down