Skip to content

Commit ffb1eaf

Browse files
authored
Merge pull request SciSharp#188 from PppBr/master
implemented 1) tf.sqrt 2)tf.distributions.normal
2 parents 423dd1e + 2cdbdaf commit ffb1eaf

11 files changed

Lines changed: 78 additions & 25 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public static partial class tf
8+
{
9+
public static class distributions
10+
{
11+
public static Normal Normal(Tensor loc,
12+
Tensor scale,
13+
bool validate_args = false,
14+
bool allow_nan_stats = true,
15+
string name = "Normal") => new Normal(loc, scale, validate_args = false, allow_nan_stats = true, "Normal");
16+
}
17+
}
18+
}

‎src/TensorFlowNET.Core/APIs/tf.math.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static partial class tf
1010

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

13+
public static Tensor sqrt(Tensor a, string name = null) => gen_math_ops.sqrt(a, name);
14+
1315
public static Tensor subtract<T>(Tensor x, T[] y, string name = null) where T : struct
1416
=> gen_math_ops.sub(x, ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y"), name);
1517

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Framework
6+
{
7+
public static class SparseTensor
8+
{
9+
private static Tensor _dense_shape { get; set; }
10+
11+
}
12+
}

‎src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
using System.Text;
55

66

7+
78
namespace Tensorflow
89
{
9-
abstract class _BaseDistribution : Python
10+
public class _BaseDistribution : Python
1011
{
1112
// Abstract base class needed for resolving subclass hierarchy.
1213
}
@@ -16,10 +17,10 @@ abstract class _BaseDistribution : Python
1617
/// Distribution is a base class for constructing and organizing properties
1718
/// (e.g., mean, variance) of random variables (e.g, Bernoulli, Gaussian).
1819
/// </summary>
19-
class Distribution : _BaseDistribution
20+
public class Distribution : _BaseDistribution
2021
{
2122
public TF_DataType _dtype {get;set;}
22-
public ReparameterizationType _reparameterization_type {get;set;}
23+
//public ReparameterizationType _reparameterization_type {get;set;}
2324
public bool _validate_args {get;set;}
2425
public bool _allow_nan_stats {get;set;}
2526
public Dictionary<string, object> _parameters {get;set;}

‎src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System.Collections.Generic;
2+
using Tensorflow;
23

34
namespace Tensorflow
45
{
5-
class Normal : Distribution
6+
public class Normal : Distribution
67
{
78
public Tensor _loc { get; set; }
89
public Tensor _scale { get; set; }
910

10-
Dictionary<string, object> parameters = new Dictionary<string, object>();
11+
public Dictionary<string, object> parameters = new Dictionary<string, object>();
1112
/// <summary>
1213
/// The Normal distribution with location `loc` and `scale` parameters.
1314
/// Mathematical details
@@ -39,7 +40,7 @@ public Normal (Tensor loc, Tensor scale, bool validate_args=false, bool allow_na
3940
this._loc = array_ops.identity(loc, name);
4041
this._scale = array_ops.identity(scale, name);
4142
base._dtype = this._scale.dtype;
42-
base._reparameterization_type = new ReparameterizationType("FULLY_REPARAMETERIZED");
43+
// base._reparameterization_type = new ReparameterizationType("FULLY_REPARAMETERIZED");
4344
base._validate_args = validate_args;
4445
base._allow_nan_stats = allow_nan_stats;
4546
base._parameters = parameters;
@@ -56,25 +57,25 @@ public Normal (Tensor loc, Tensor scale, bool validate_args=false, bool allow_na
5657
/// <returns></returns>
5758
public Tensor loc()
5859
{
59-
return this._loc;
60+
return _loc;
6061
}
6162
/// <summary>
6263
/// Distribution parameter for standard deviation."
6364
/// </summary>
6465
/// <returns></returns>
6566
public Tensor scale()
6667
{
67-
return this._scale;
68+
return _scale;
6869
}
6970

7071
public Tensor _batch_shape_tensor()
7172
{
72-
return array_ops.broadcast_dynamic_shape(array_ops.shape(this._loc), array_ops.shape(this._scale));
73+
return array_ops.broadcast_dynamic_shape(array_ops.shape(_loc), array_ops.shape(_scale));
7374
}
7475

7576
public Tensor _batch_shape()
7677
{
77-
return array_ops.broadcast_static_shape(new Tensor(this._loc.shape), new Tensor(this._scale.shape));
78+
return array_ops.broadcast_static_shape(new Tensor(_loc.shape), new Tensor(_scale.shape));
7879
}
7980

8081
}

‎src/TensorFlowNET.Core/Operations/OpDefLibrary.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value)
305305
case "list(type)":
306306
attr_value.List.Type.AddRange((value as IList<TF_DataType>).Select(x => _MakeType(x, attr_def)));
307307
break;
308+
case "list(int)":
309+
attr_value.List.I.AddRange((value as int[]).Select(x => Convert.ToInt64(x)));
310+
break;
308311
case "bool":
309312
attr_value.B = (bool)value;
310313
break;

‎src/TensorFlowNET.Core/Operations/gen_array_ops.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static Tensor zeros_like(Tensor x, string name = null)
164164
}
165165
public static Tensor stop_gradient(Tensor x, string name = null)
166166
{
167-
var _op = _op_def_lib._apply_op_helper("StopGradient", name, args: new { x });
167+
var _op = _op_def_lib._apply_op_helper("StopGradient", name, args: new { input = x, name });
168168

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

186186
return _op.outputs[0];
187187
}

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public static Tensor mean(Tensor input, Tensor axis, bool keep_dims= false, stri
2727
return _op.outputs[0];
2828
}
2929

30+
public static Tensor mean(Tensor input, int[] axis, bool keep_dims = false, string name = null)
31+
{
32+
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims, name });
33+
34+
return _op.outputs[0];
35+
}
36+
3037
public static Tensor add(Tensor x, Tensor y, string name = null)
3138
{
3239
var _op = _op_def_lib._apply_op_helper("Add", name, args: new { x, y });
@@ -36,7 +43,7 @@ public static Tensor add(Tensor x, Tensor y, string name = null)
3643

3744
public static Tensor squared_difference(Tensor x, Tensor y, string name = null)
3845
{
39-
var _op = _op_def_lib._apply_op_helper("SquaredDifference", name, args: new { x, y });
46+
var _op = _op_def_lib._apply_op_helper("SquaredDifference", name, args: new { x, y, name });
4047

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

65+
public static Tensor sqrt(Tensor x, string name = null)
66+
{
67+
var _op = _op_def_lib._apply_op_helper("Sqrt", name, args: new { x });
68+
69+
return _op.outputs[0];
70+
}
71+
5872
public static Tensor sub<Tx, Ty>(Tx x, Ty y, string name = null)
5973
{
6074
var _op = _op_def_lib._apply_op_helper("Sub", name, args: new { x, y });

‎src/TensorFlowNET.Core/Operations/math_ops.py.cs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using NumSharp.Core;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

@@ -37,8 +38,8 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
3738
/// <param name="name"> A name for the operation (optional).</param>
3839
public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
3940
{
40-
var r = _ReductionDims(input_tensor, new Tensor(axis));
41-
var m = gen_math_ops.mean(input_tensor, r);
41+
var r = _ReductionDims(input_tensor, axis);
42+
var m = gen_math_ops.mean(input_tensor, (int[]) r, keepdims, name);
4243
return _may_reduce_to_scalar(keepdims,axis, m);
4344
}
4445
/// <summary>
@@ -135,8 +136,8 @@ private static Tensor _ReductionDims(Tensor x, Tensor axis)
135136
return range(0, rank, 1);
136137
}
137138
}
138-
139-
private static int[] _ReductionDims(Tensor x, int[] axis)
139+
140+
private static object _ReductionDims(Tensor x, int[] axis)
140141
{
141142
if (axis != null)
142143
{
@@ -147,12 +148,12 @@ private static int[] _ReductionDims(Tensor x, int[] axis)
147148
var rank = array_ops.rank(x);
148149
if (rank != null)
149150
{
150-
// return constant_op.constant();
151+
return constant_op.constant(np.arange(rank), TF_DataType.TF_INT32);
151152
}
152-
// return range(0, rank, 1);
153-
throw new NotFiniteNumberException();
153+
return range(0, rank, 1);
154154
}
155155
}
156+
156157

157158
public static Tensor range(object start, object limit = null, object delta = null, TF_DataType dtype = TF_DataType.DtInvalid, string name = "range" )
158159
{

‎src/TensorFlowNET.Core/Operations/nn_impl.py.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ public static (Tensor, Tensor) moments(Tensor x,
2626
// on 32-bit floats before converting the mean and variance back to fp16
2727
var y = math_ops.cast(x, TF_DataType.TF_FLOAT);
2828
// Compute true mean while keeping the dims for proper broadcasting.
29-
var mean = math_ops.reduce_mean(y, axes, keep_dims = true, name = "mean");
29+
var mean = math_ops.reduce_mean(y, axes, true, name = "mean");
3030
// Sample variance, not unbiased variance
3131
// Note: stop_gradient does not change the gradient that gets
3232
// backpropagated to the mean from the variance calculation,
3333
// because that gradient is zero
34-
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"));
34+
var variance = math_ops.reduce_mean(math_ops.square_difference(y, array_ops.stop_gradient(mean)), axes, true, name = "Variance");
3535
if (!keep_dims)
3636
{
3737
mean = array_ops.squeeze(mean, axes);
3838
variance = array_ops.squeeze(variance, axes);
3939
}
4040
// TODO: if x.dtype == dtypes.float16:
41-
if (x.dtype == TF_DataType.TF_FLOAT)
41+
if (x.dtype == TF_DataType.TF_HALF)
4242
return (math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
4343
else
4444
return (mean, variance);

0 commit comments

Comments
 (0)