diff --git a/Backends/CNTK.CPU/CNTK (CPU).csproj b/Backends/CNTK.CPU/CNTK (CPU).csproj new file mode 100644 index 0000000..eab7772 --- /dev/null +++ b/Backends/CNTK.CPU/CNTK (CPU).csproj @@ -0,0 +1,80 @@ + + + + + Debug + AnyCPU + {99BEFBBE-88C2-426A-B687-0D0928B54F05} + Library + Properties + KerasSharp.Backends.CNTK.CPUOnly + KerasSharp.BackendsCNTK.CPUOnly + v4.6.2 + 512 + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\Accord.3.7.0\lib\net462\Accord.dll + + + ..\..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.dll + + + ..\..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.Core.dll + + + ..\..\packages\CNTK.CPUOnly.2.2.0\lib\net45\x64\Cntk.Core.Managed-2.2.dll + + + + + + + + + + + + + + + + + + + + {1478e4e5-f4d5-4066-886b-544d1e172374} + Keras Sharp + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/Backends/CNTK.CPU/Properties/AssemblyInfo.cs b/Backends/CNTK.CPU/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fcb6332 --- /dev/null +++ b/Backends/CNTK.CPU/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("KerasSharp.CNTK.CpuOnly")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("KerasSharp.CNTK.CpuOnly")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("99befbbe-88c2-426a-b687-0d0928b54f05")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Backends/CNTK.CPU/app.config b/Backends/CNTK.CPU/app.config new file mode 100644 index 0000000..96344c3 --- /dev/null +++ b/Backends/CNTK.CPU/app.config @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Backends/CNTK.CPU/packages.config b/Backends/CNTK.CPU/packages.config new file mode 100644 index 0000000..cbcd55d --- /dev/null +++ b/Backends/CNTK.CPU/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Backends/TensorFlow/Properties/AssemblyInfo.cs b/Backends/TensorFlow/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d0572b3 --- /dev/null +++ b/Backends/TensorFlow/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("KerasSharp.TensorFlow")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("KerasSharp.TensorFlow")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("31e970fd-4952-4a3a-9cc9-f9b519c3b20d")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Backends/TensorFlow/TensorFlow (CPU).csproj b/Backends/TensorFlow/TensorFlow (CPU).csproj new file mode 100644 index 0000000..ad1bf41 --- /dev/null +++ b/Backends/TensorFlow/TensorFlow (CPU).csproj @@ -0,0 +1,88 @@ + + + + + Debug + AnyCPU + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D} + Library + Properties + KerasSharp.Backends.TensorFlow + KerasSharp.Backends.TensorFlow + v4.6.2 + 512 + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\Accord.3.7.0\lib\net462\Accord.dll + + + ..\..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.dll + + + ..\..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.Core.dll + + + + + + + ..\..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll + + + + + + + + + ..\..\packages\TensorFlowSharp.1.3.1-pre1\lib\net461\TensorFlowSharp.dll + + + + + + + + + + + + + + + {1478e4e5-f4d5-4066-886b-544d1e172374} + Keras Sharp + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/Sources/Backends/TensorFlowBackend.cs b/Backends/TensorFlow/TensorFlowBackend.cs similarity index 83% rename from Sources/Backends/TensorFlowBackend.cs rename to Backends/TensorFlow/TensorFlowBackend.cs index 75b3418..f07ebe3 100644 --- a/Sources/Backends/TensorFlowBackend.cs +++ b/Backends/TensorFlow/TensorFlowBackend.cs @@ -35,8 +35,8 @@ namespace KerasSharp.Backends using KerasSharp.Losses; using KerasSharp.Models; using TensorFlow; - using Accord.Math; using static KerasSharp.Python; + using Accord.Math; // TODO: @@ -137,14 +137,14 @@ public void clear_session() public Tensor reshape(Tensor x, int[] shape) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L1724 - return tensor(tf.Reshape(x, constant(shape))); + return tensor(tf.Reshape(tensor: TF(x), shape: _constant(shape))); } public Tensor abs(Tensor input) { - throw new NotImplementedException(); + return tensor(tf.Abs(TF(input))); } @@ -184,24 +184,27 @@ public void batch_set_value(List<(Tensor, Array)> tuples) /// public Tensor binary_crossentropy(Tensor output, Tensor target, bool from_logits = false) { + var _output = TF(output); + var _target = TF(target); + // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L2792 // Note: tf.nn.sigmoid_cross_entropy_with_logits // expects logits, Keras expects probabilities. if (!from_logits) { // transform back to logits - TFOutput _epsilon = constant(epsilon(), dtype: output.dtype); - TFOutput o = output.output; - o = tf.ClipByValue(o, _epsilon, tf.Sub(constant(1f), _epsilon)); - o = tf.Log(tf.Div(output, (tf.Sub(tf.Const(1f), output)))); + TFOutput _epsilon = _constant(epsilon(), dtype: _output.dtype); + TFOutput o = _output.output; + o = tf.ClipByValue(o, _epsilon, tf.Sub(_constant(1f), _epsilon)); + o = tf.Log(tf.Div(_output, (tf.Sub(tf.Const(1f), _output)))); } - return tensor(tf.sigmoid_cross_entropy_with_logits(labels: target, logits: output)); + return tensor(tf.sigmoid_cross_entropy_with_logits(labels: _target, logits: _output)); } - public Tensor cast(Tensor x, TFDataType dataType) + public Tensor cast(Tensor x, DataType dataType) { - return tensor(tf.Cast(x, dataType)); + return tensor(tf.Cast(TF(x), TF(dataType))); } /// @@ -216,6 +219,9 @@ public Tensor cast(Tensor x, TFDataType dataType) /// public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false) { + var _target = TF(target); + var _output = TF(output); + // Note: tf.nn.softmax_cross_entropy_with_logits // expects logits, Keras expects probabilities. if (!from_logits) @@ -223,15 +229,15 @@ public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_l // scale preds so that the class probas of each sample sum to 1 int?[] shape = output.shape; var last = tf.Const(new TFTensor(shape.Length - 1)); - TFOutput o = tf.Div(output, tf.ReduceSum(output, axis: last, keep_dims: true)); + TFOutput o = tf.Div(_output, tf.ReduceSum(_output, axis: last, keep_dims: true)); // manual computation of crossentropy - TFOutput _epsilon = constant(epsilon(), dtype: output.dtype); - o = tf.ClipByValue(o, _epsilon, tf.Sub(constant(1f), _epsilon)); - o = tf.Neg(tf.ReduceSum(tf.Mul(target, tf.Log(output)), axis: last)); + TFOutput _epsilon = _constant(epsilon(), dtype: _output.dtype); + o = tf.ClipByValue(o, _epsilon, tf.Sub(_constant(1f), _epsilon)); + o = tf.Neg(tf.ReduceSum(tf.Mul(_target, tf.Log(_output)), axis: last)); return tensor(o); } - return tensor(tf.SoftmaxCrossEntropyWithLogits(target, output).loss); + return tensor(tf.SoftmaxCrossEntropyWithLogits(_target, _output).loss); } public Tensor clip(Tensor norms, int v, int maxValue) @@ -249,29 +255,32 @@ public Tensor clip_norm(Tensor g, double clipnorm, Tensor norm) throw new NotImplementedException(); } - public Tensor constant(T value, int?[] shape = null, TFDataType? dtype = null, string name = null) + public Tensor constant(T value, int?[] shape = null, DataType? dtype = null, string name = null) + { + return tensor(_constant(value, TF(dtype), name)); + } + + private TFOutput _constant(T value, TFDataType? dtype = null, string name = null) { TFTensor t = new TFTensor((dynamic)value); TFOutput o = dtype == null ? tf.Const(t, operName: name) : tf.Const(t, dtype: dtype.Value, operName: name); - - return tensor(o); + return o; } - public Tensor dropout(object p, double retain_prob, object noise_shape, object seed) { throw new NotImplementedException(); } - public TFDataType? dtype(Tensor input_tensor) + public DataType? dtype(Tensor tensor) { - return input_tensor.dtype; + return TF(TF(tensor).dtype); } public Tensor elu(Tensor x) @@ -294,9 +303,9 @@ public Tensor exp(object v) throw new NotImplementedException(); } - public TFDataType floatx() + public DataType floatx() { - return TFDataType.Float; + return DataType.Float; } public Function function(object inputs, List list, Func> updates, string name) @@ -336,14 +345,18 @@ public Function function(object inputs, List list, List updates, return int_shape(x); } - public List gradients(ILoss loss, object param) + public List gradients(Tensor loss, object param) { throw new NotImplementedException(); } - public List gradients(Tensor loss, object param) + public List gradients(Tensor loss, List param) { - throw new NotImplementedException(); + var y = new TFOutput[] { TF(loss).output }; + var x = param.Select(t => TF(t).output).ToArray(); + TFOutput[] grads = tf.AddGradients(x, y); + List r = grads.Select(o => tensor(o)).ToList(); + return r; } public Tensor greater_equal(Tensor w, double v) @@ -353,12 +366,12 @@ public Tensor greater_equal(Tensor w, double v) public Tensor not_equal(Tensor x, Tensor y) { - return tensor(tf.NotEqual(x, y)); + return tensor(tf.NotEqual(TF(x), TF(y))); } public Tensor not_equal(Tensor x, double y) { - return tensor(tf.NotEqual(x, tf.Const(y, x.dtype))); + return tensor(tf.NotEqual(TF(x), tf.Const(y, TF(x).dtype))); } public Tensor hard_sigmoid(Tensor x) @@ -388,8 +401,8 @@ public Tensor identity(Tensor x) try { - long[] shape = tf.GetTensorShape(x.output); - return shape.Apply(i => i == -1 ? null : (int?)i); + long[] shape = tf.GetTensorShape(TF(x).output).ToArray(); + return shape.Select(i => i == -1 ? null : (int?)i).ToArray(); } catch { @@ -462,9 +475,10 @@ public Tensor in_train_phase(Func x, Func alt, bool? training) /// public Tensor @switch(Tensor condition, Func then_expression, Func else_expression) { + var _condition = TF(condition); - if (condition.dtype != TFDataType.Bool) - condition = tensor(tf.Cast(condition, TFDataType.Bool)); + if (_condition.dtype != TFDataType.Bool) + condition = tensor(tf.Cast(_condition, TFDataType.Bool)); throw new NotImplementedException(); @@ -556,7 +570,7 @@ public TFOutput _normalize_axis(int[] axis, int? ndim) /// public Tensor mean(Tensor x, int[] axis, bool keepdims = false, string name = null) { - return tensor(tf.ReduceMean(x, _normalize_axis(axis, ndim(x)), keepdims, operName: name)); + return tensor(tf.ReduceMean(TF(x), _normalize_axis(axis, ndim(x)), keepdims, operName: name)); } /// @@ -573,7 +587,7 @@ public Tensor mean(Tensor x, int[] axis, bool keepdims = false, string name = nu /// public Tensor mean(Tensor x, int axis = -1, bool keepdims = false, string name = null) { - return tensor(tf.ReduceMean(x, axis: tf.Const(axis), keep_dims: keepdims, operName: name)); + return tensor(tf.ReduceMean(TF(x), axis: tf.Const(axis), keep_dims: keepdims, operName: name)); } @@ -584,23 +598,23 @@ public Tensor minus(Tensor tensor) public Tensor dot(Tensor a, Tensor b) { - return tensor(tf.MatMul(a.output, b.output)); + return tensor(tf.MatMul(TF(a).output, TF(b).output)); } public Tensor mul(T a, Tensor b) { - return mul(constant(a, dtype: b.dtype), b); + return mul(constant(a, dtype: dtype(b)), b); } public Tensor mul(Tensor a, Tensor b) { - return tensor(tf.Mul(a.output, b.output)); + return tensor(tf.Mul(TF(a).output, TF(b).output)); } public Tensor mul(Tensor a, T b) { - return mul(a, constant(b, dtype: a.dtype)); + return mul(a, constant(b, dtype: dtype(a))); } public Tensor mul(List batch_outs, int length) @@ -614,17 +628,17 @@ public Tensor mul(List batch_outs, int length) public Tensor div(T a, Tensor b) { - return div(constant(a, dtype: b.dtype), b); + return div(constant(a, dtype: dtype(b)), b); } public Tensor div(Tensor a, Tensor b) { - return tensor(tf.Mul(a.output, b.output)); + return tensor(tf.Mul(TF(a).output, TF(b).output)); } public Tensor div(Tensor a, T b) { - return div(a, constant(b, dtype: a.dtype)); + return div(a, constant(b, dtype: dtype(a))); } public Tensor div(List batch_outs, int length) @@ -636,7 +650,7 @@ public Tensor div(List batch_outs, int length) public Tensor add(Tensor a, Tensor b) { - return tensor(tf.Add(a.output, b.output)); + return tensor(tf.Add(TF(a).output, TF(b).output)); } public Tensor add(T a, Tensor b) @@ -653,7 +667,7 @@ public Tensor add(Tensor a, T b) public Tensor subtract(Tensor a, Tensor b) { - return tensor(tf.Sub(a.output, b.output)); + return tensor(tf.Sub(TF(a).output, TF(b).output)); } public Tensor subtract(T a, Tensor b) @@ -692,10 +706,10 @@ public IDisposable name_scope(string name) if (dims != null) return dims.Length; - return tf.GetTensorNumDims(x.output); + return tf.GetTensorNumDims(TF(x).output); } - public Tensor placeholder(int?[] shape = null, int? ndim = null, TFDataType? dtype = Utils.DEFAULT_DTYPE, bool sparse = false, string name = null) + public Tensor placeholder(int?[] shape = null, int? ndim = null, DataType? dtype = DataType.DEFAULT_DTYPE, bool sparse = false, string name = null) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L397 @@ -713,7 +727,7 @@ public Tensor placeholder(int?[] shape = null, int? ndim = null, TFDataType? dty var tfshape = this.shape(shape); - Tensor x = tensor(tf.Placeholder(dtype.Value, tfshape, operName: name)); + Tensor x = tensor(tf.Placeholder(TF(dtype.Value), tfshape, operName: name)); x._keras_shape = shape; x._uses_learning_phase = false; return x; @@ -730,33 +744,35 @@ public Tensor placeholder(int?[] shape = null, int? ndim = null, TFDataType? dty /// /// A tensor. /// - public Tensor random_uniform(int?[] shape, double minval = 0.0, double maxval = 1.0, TFDataType dtype = Utils.DEFAULT_DTYPE, int? seed = null, string name = null) + public Tensor random_uniform(int?[] shape, double minval = 0.0, double maxval = 1.0, DataType dtype = DataType.DEFAULT_DTYPE, int? seed = null, string name = null) { + var _dtype = TF(dtype); + if (seed == null) seed = Accord.Math.Random.Generator.Random.Next(1_000_000); - var tf_shape = tf.Const(shape.Apply(x => (long)x)); - TFOutput u = tf.RandomUniform(tf_shape, dtype: dtype, seed: seed, operName: name); + var tf_shape = tf.Const(shape.Select(x => (long)x).ToArray()); + TFOutput u = tf.RandomUniform(tf_shape, dtype: _dtype, seed: seed, operName: name); - return tensor(tf.Add(tf.Mul(u, tf.Const(new TFTensor(maxval - minval), dtype: dtype)), - tf.Const(new TFTensor(minval), dtype: dtype))); + return tensor(tf.Add(tf.Mul(u, tf.Const(new TFTensor(maxval - minval), dtype: _dtype)), + tf.Const(new TFTensor(minval), dtype: _dtype))); } public Tensor relu(Tensor x) { - return tensor(tf.Relu(x)); + return tensor(tf.Relu(TF(x))); } public Tensor sigmoid(Tensor x) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L2817 - return tensor(tf.Sigmoid(x)); + return tensor(tf.Sigmoid(TF(x))); } public Tensor softmax(Tensor x) { - return tensor(tf.Softmax(x.output)); + return tensor(tf.Softmax(TF(x).output)); } public Tensor softplus(Tensor x) @@ -776,17 +792,17 @@ public Tensor sqrt(object p) public Tensor square(Tensor w) { - return tensor(tf.Square(w)); + return tensor(tf.Square(TF(w))); } public Tensor sum(Tensor x, int[] axis, bool keepdims = false, string name = null) { - return tensor(tf.ReduceSum(x, tf.Const(axis), keepdims, name)); + return tensor(tf.ReduceSum(TF(x), tf.Const(axis), keepdims, name)); } public Tensor sum(Tensor x, int axis, bool keepdims = false, string name = null) { - return tensor(tf.ReduceSum(x, tf.Const(axis), keepdims, name)); + return tensor(tf.ReduceSum(TF(x), tf.Const(axis), keepdims, name)); } public object sum(object[] v) @@ -809,17 +825,12 @@ public Tensor tanh(Tensor x) throw new NotImplementedException(); } - public Tensor truncated_normal(TFShape shape, double v, double stddev, TFDataType dtype, int? seed) + public Tensor truncated_normal(int[] shape, double v, double stddev, DataType dtype, int? seed) { throw new NotImplementedException(); } - public Tensor truncated_normal(int[] shape, double v, double stddev, TFDataType dtype, int? seed) - { - throw new NotImplementedException(); - } - - public Tensor truncated_normal(int?[] shape, double v, double stddev, TFDataType dtype, int? seed) + public Tensor truncated_normal(int?[] shape, double v, double stddev, DataType dtype, int? seed) { throw new NotImplementedException(); } @@ -847,7 +858,8 @@ public Tensor variable(T value, string name = null) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L308 - Tensor t = new Tensor(this); + var t = new TensorFlowTensor(this); + // trick for being type safe and still allow all numeric types supported by TFTensor t.output = tf.Const(new TFTensor((dynamic)value), operName: name); t._keras_shape = new int?[] { }; @@ -867,7 +879,8 @@ public Tensor variable(Array array, string name = null) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L308 - Tensor t = new Tensor(this); + var t = new TensorFlowTensor(this); + t.output = tf.Const(new TFTensor(array), operName: name); t._keras_shape = array.GetLength().Apply(x => (int?)x); t._uses_learning_phase = false; @@ -882,15 +895,18 @@ public Tensor variable(Array array, string name = null) /// Tensor type. /// Optional name string for the tensor. /// - public Tensor variable(Tensor tensor, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null) + public Tensor variable(Tensor tensor, DataType dtype = DataType.DEFAULT_DTYPE, string name = null) { + var _tensor = TF(tensor); + // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L308 - Tensor t = new Tensor(this); - if (tensor.tensor == null) - t.output = tensor.output; + var t = new TensorFlowTensor(this); + + if (_tensor.tensor == null) + t.output = _tensor.output; else - t.output = tf.Const(tensor.tensor); + t.output = tf.Const(_tensor.tensor); t._keras_shape = tensor.shape; t._uses_learning_phase = false; return t; @@ -898,7 +914,9 @@ public Tensor variable(Tensor tensor, TFDataType dtype = Utils.DEFAULT_DTYPE, st public object eval(Tensor tensor) { - TFTensor[] result = _SESSION.Run(new TFOutput[] { }, new TFTensor[] { }, new[] { tensor.output }); + var _tensor = TF(tensor); + + TFTensor[] result = _SESSION.Run(new TFOutput[] { }, new TFTensor[] { }, new[] { _tensor.output }); if (result.Length == 1) return result[0].GetValue(); @@ -907,15 +925,6 @@ public object eval(Tensor tensor) } - public TFShape shape(int?[] shape) - { - return new TFShape(shape.Select(x => x.HasValue ? (long)x.Value : -1).ToArray()); - } - - public Tensor tensor(TFOutput output) - { - return new Tensor(this) { output = output }; - } /// @@ -925,7 +934,7 @@ public Tensor tensor(TFOutput output) /// Data type of returned Keras variable. /// String, name of returned Keras variable. /// A variable(including Keras metadata), filled with 0.0. - public Tensor zeros(int?[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null) + public Tensor zeros(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE, string name = null) { return zeros(shape.Select(i => i.Value).ToArray(), dtype, name); } @@ -937,7 +946,7 @@ public Tensor zeros(int?[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string /// Data type of returned Keras variable. /// String, name of returned Keras variable. /// A variable(including Keras metadata), filled with 0.0. - public Tensor zeros(int[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null) + public Tensor zeros(int[] shape, DataType dtype = DataType.DEFAULT_DTYPE, string name = null) { // The following is not necessary since C# is strongly typed: // if dtype is None: @@ -946,12 +955,100 @@ public Tensor zeros(int[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string // tf_dtype = _convert_string_dtype(dtype) // However, we might have to perform other conversions of our own: - Type type = Utils.GetSystemType(dtype); + Type type = TFTensor.TypeFromTensorType(TF(dtype)); Array zeros = Array.CreateInstance(type, shape); return this.variable(array: zeros, name: name); } + /// + /// Element-wise equality between two tensors. + /// + /// + /// Tensor or variable. + /// Tensor or variable. + /// + /// A bool tensor. + /// + public Tensor equal(Tensor x, Tensor y) + { + return tensor(tf.Equal(TF(x), TF(y))); + } + + /// + /// Returns the index of the maximum value along an axis. + /// + /// + /// Tensor or variable. + /// The axis along which to perform the reduction. + /// + /// A tensor. + /// + public Tensor argmax(Tensor x, int axis = -1) + { + // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L1332 + //axis = _normalize_axis(axis, ndim(x)); + return tensor(tf.ArgMax(TF(x), tf.Const(axis))); + } + + public Tensor round(Tensor x) + { + return tensor(tf.Round(TF(x))); + } + + + + + + + + #region convertion + + public TFShape shape(int?[] shape) + { + return new TFShape(shape.Select(x => x.HasValue ? (long)x.Value : -1).ToArray()); + } + + public Tensor tensor(TFOutput output) + { + return new TensorFlowTensor(this) { output = output }; + } + + public TensorFlowTensor TF(Tensor output) + { + return (TensorFlowTensor)output; + } + + public TensorFlowTensor TF(TFOutput output) + { + return new TensorFlowTensor(this) { output = output }; + } + + private static TFDataType TF(DataType dataType) + { + return (TFDataType)dataType; + } + + private static TFDataType? TF(DataType? dataType) + { + if (dataType == null) + return null; + return (TFDataType)dataType.Value; + } + + private static DataType? TF(TFDataType? dataType) + { + if (dataType == null) + return null; + return (DataType)dataType.Value; + } + + private static DataType TF(TFDataType dataType) + { + return (DataType)dataType; + } + + #endregion @@ -996,41 +1093,6 @@ public void Dispose() // TODO: uncomment the following line if the finalizer is overridden above. // GC.SuppressFinalize(this); } - - /// - /// Element-wise equality between two tensors. - /// - /// - /// Tensor or variable. - /// Tensor or variable. - /// - /// A bool tensor. - /// - public Tensor equal(Tensor x, Tensor y) - { - return tensor(tf.Equal(x, y)); - } - - /// - /// Returns the index of the maximum value along an axis. - /// - /// - /// Tensor or variable. - /// The axis along which to perform the reduction. - /// - /// A tensor. - /// - public Tensor argmax(Tensor x, int axis = -1) - { - // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L1332 - //axis = _normalize_axis(axis, ndim(x)); - return tensor(tf.ArgMax(x, tf.Const(axis))); - } - - public Tensor round(Tensor x) - { - return tensor(tf.Round(x)); - } #endregion } } diff --git a/Sources/TensorFlowSharpEx.cs b/Backends/TensorFlow/TensorFlowSharpEx.cs similarity index 99% rename from Sources/TensorFlowSharpEx.cs rename to Backends/TensorFlow/TensorFlowSharpEx.cs index 84b3ce0..3c078de 100644 --- a/Sources/TensorFlowSharpEx.cs +++ b/Backends/TensorFlow/TensorFlowSharpEx.cs @@ -32,7 +32,7 @@ static TFOutput ReduceDims(this TFGraph g, TFOutput input, TFOutput? axis = null return axis.Value; // Fast path: avoid creating Rank and Range ops if ndims is known. - var shape = g.GetTensorShape(input); + long[] shape = g.GetTensorShape(input).ToArray(); if (shape.Length >= 0) { // The python code distinguishes between tensor and sparsetensor diff --git a/Backends/TensorFlow/TensorFlowTensor.cs b/Backends/TensorFlow/TensorFlowTensor.cs new file mode 100644 index 0000000..3c5f654 --- /dev/null +++ b/Backends/TensorFlow/TensorFlowTensor.cs @@ -0,0 +1,91 @@ +// Keras-Sharp: C# port of the Keras library +// https://github.com/cesarsouza/keras-sharp +// +// Based under the Keras library for Python. See LICENSE text for more details. +// +// The MIT License(MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +namespace KerasSharp.Engine.Topology +{ + using KerasSharp.Backends; + using KerasSharp.Layers; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Runtime.Serialization; + using System.Text; + using System.Threading.Tasks; + using static KerasSharp.Python; + using TensorFlow; + using System.Diagnostics; + + [DataContract] + [DebuggerDisplay("{ToString()}")] + public class TensorFlowTensor : Tensor + { + public new TensorFlowBackend K + { + get { return base.K as TensorFlowBackend; } + } + + public TFTensor tensor; + public TFOutput output; + + public TFDataType dtype + { + get + { + if (tensor != null) + return tensor.TensorType; + return output.OutputType; + } + } + + public TensorFlowTensor(IBackend backend) + : base(backend) + { + } + + public long[] TF_Shape + { + get + { + var tf = (K as TensorFlowBackend).tf; + return tf.GetShape(output); + } + } + + public override string ToString() + { + string n = output.Operation.Name; + long i = output.Index; + string s = str(shape); + string r = $"KerasSharp.Engine.Topology.Tensor '{n}_{i}' shape={s} dtype={output.OutputType}"; + return r; + } + + public static implicit operator TFOutput(TensorFlowTensor t) + { + return t.output; + } + } +} diff --git a/Backends/TensorFlow/app.config b/Backends/TensorFlow/app.config new file mode 100644 index 0000000..4b89b20 --- /dev/null +++ b/Backends/TensorFlow/app.config @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Backends/TensorFlow/packages.config b/Backends/TensorFlow/packages.config new file mode 100644 index 0000000..d313e52 --- /dev/null +++ b/Backends/TensorFlow/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Keras Sharp.sln b/Keras Sharp.sln index c272064..dab00e0 100644 --- a/Keras Sharp.sln +++ b/Keras Sharp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26621.2 +VisualStudioVersion = 15.0.26906.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Keras Sharp", "sources\Keras Sharp.csproj", "{1478E4E5-F4D5-4066-886B-544D1E172374}" EndProject @@ -13,6 +13,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Backends", "Backends", "{0DAA7F67-8E93-4EC8-9CB5-C0D1924C34E1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlow (CPU)", "Backends\TensorFlow\TensorFlow (CPU).csproj", "{31E970FD-4952-4A3A-9CC9-F9B519C3B20D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CNTK (CPU)", "Backends\CNTK.CPU\CNTK (CPU).csproj", "{99BEFBBE-88C2-426A-B687-0D0928B54F05}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,10 +33,22 @@ Global {8EDF4429-251A-416D-BB68-93F227191BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU {8EDF4429-251A-416D-BB68-93F227191BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU {8EDF4429-251A-416D-BB68-93F227191BCF}.Release|Any CPU.Build.0 = Release|Any CPU + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D}.Release|Any CPU.Build.0 = Release|Any CPU + {99BEFBBE-88C2-426A-B687-0D0928B54F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99BEFBBE-88C2-426A-B687-0D0928B54F05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99BEFBBE-88C2-426A-B687-0D0928B54F05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99BEFBBE-88C2-426A-B687-0D0928B54F05}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {31E970FD-4952-4A3A-9CC9-F9B519C3B20D} = {0DAA7F67-8E93-4EC8-9CB5-C0D1924C34E1} + {99BEFBBE-88C2-426A-B687-0D0928B54F05} = {0DAA7F67-8E93-4EC8-9CB5-C0D1924C34E1} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {12390374-1462-4836-BC17-0986518A9F03} EndGlobalSection diff --git a/Sources/AccordEx.cs b/Sources/AccordEx.cs index b9c7dac..766d9e7 100644 --- a/Sources/AccordEx.cs +++ b/Sources/AccordEx.cs @@ -6,7 +6,6 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -using TensorFlow; namespace KerasSharp { diff --git a/Sources/Activations/ELU.cs b/Sources/Activations/ELU.cs index e17c76f..d2ed873 100644 --- a/Sources/Activations/ELU.cs +++ b/Sources/Activations/ELU.cs @@ -35,7 +35,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + /// /// Exponential Linear Unit activation function. diff --git a/Sources/Activations/HardSigmoid.cs b/Sources/Activations/HardSigmoid.cs index 693b967..1e4ef93 100644 --- a/Sources/Activations/HardSigmoid.cs +++ b/Sources/Activations/HardSigmoid.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Engine.Topology; diff --git a/Sources/Activations/ReLU.cs b/Sources/Activations/ReLU.cs index 108b2c2..ee318b8 100644 --- a/Sources/Activations/ReLU.cs +++ b/Sources/Activations/ReLU.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Engine.Topology; diff --git a/Sources/Activations/SeLU.cs b/Sources/Activations/SeLU.cs index 2d0d492..57940ef 100644 --- a/Sources/Activations/SeLU.cs +++ b/Sources/Activations/SeLU.cs @@ -35,7 +35,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + /// /// Scaled Exponential Linear Unit (SeLU) activation function. diff --git a/Sources/Activations/Sigmoid.cs b/Sources/Activations/Sigmoid.cs index 377aa83..97c8c4d 100644 --- a/Sources/Activations/Sigmoid.cs +++ b/Sources/Activations/Sigmoid.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; /// diff --git a/Sources/Activations/SoftPlus.cs b/Sources/Activations/SoftPlus.cs index 7cc6618..82d7c6f 100644 --- a/Sources/Activations/SoftPlus.cs +++ b/Sources/Activations/SoftPlus.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; diff --git a/Sources/Activations/SoftSign.cs b/Sources/Activations/SoftSign.cs index e0e6319..0a205ee 100644 --- a/Sources/Activations/SoftSign.cs +++ b/Sources/Activations/SoftSign.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; diff --git a/Sources/Activations/Softmax.cs b/Sources/Activations/Softmax.cs index 770ec0f..072333e 100644 --- a/Sources/Activations/Softmax.cs +++ b/Sources/Activations/Softmax.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Engine.Topology; diff --git a/Sources/Activations/TanH.cs b/Sources/Activations/TanH.cs index c4a1e6b..a8d5fd0 100644 --- a/Sources/Activations/TanH.cs +++ b/Sources/Activations/TanH.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; diff --git a/Sources/Backends/BackendBase.cs b/Sources/Backends/BackendBase.cs index ab2708a..5afb81d 100644 --- a/Sources/Backends/BackendBase.cs +++ b/Sources/Backends/BackendBase.cs @@ -34,7 +34,6 @@ namespace KerasSharp.Backends using KerasSharp.Engine.Topology; using KerasSharp.Losses; using KerasSharp.Models; - using TensorFlow; using Accord.Math; using static KerasSharp.Python; diff --git a/Sources/Backends/Base/IBackend.cs b/Sources/Backends/Base/IBackend.cs index 8062119..12a0423 100644 --- a/Sources/Backends/Base/IBackend.cs +++ b/Sources/Backends/Base/IBackend.cs @@ -33,7 +33,6 @@ namespace KerasSharp.Backends using System.Threading.Tasks; using KerasSharp.Engine.Topology; using KerasSharp.Losses; - using TensorFlow; using KerasSharp.Models; public interface IBackend : IDisposable @@ -53,18 +52,18 @@ public interface IBackend : IDisposable Tensor clip(Tensor norms, int v, int maxValue); - Tensor zeros(int[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null); + Tensor zeros(int[] shape, DataType dtype = DataType.DEFAULT_DTYPE, string name = null); - Tensor zeros(int?[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null); + Tensor zeros(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE, string name = null); float epsilon(); - TFDataType floatx(); + DataType floatx(); Tensor greater_equal(Tensor w, double v); void clear_session(); - Tensor cast(Tensor x, TFDataType dataType); + Tensor cast(Tensor x, DataType dataType); Tensor dropout(object p, double retain_prob, object noise_shape, object seed); @@ -146,9 +145,7 @@ public interface IBackend : IDisposable - Tensor random_uniform(int?[] shape, double minvalue = 0.0, double maxvalue = 1.0, TFDataType dtype = Utils.DEFAULT_DTYPE, int? seed = null, string name = null); - - Tensor truncated_normal(TFShape shape, double v, double stddev, TFDataType dtype, int? seed); + Tensor random_uniform(int?[] shape, double minvalue = 0.0, double maxvalue = 1.0, DataType dtype = DataType.DEFAULT_DTYPE, int? seed = null, string name = null); Tensor l2_normalize(Tensor expected, int axis); @@ -177,13 +174,13 @@ public interface IBackend : IDisposable Tensor variable(T value, string name = null) where T : struct; - Tensor variable(Tensor tensor, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null); + Tensor variable(Tensor tensor, DataType dtype = DataType.DEFAULT_DTYPE, string name = null); Tensor in_train_phase(Func x, Func alt, bool? training); - TFDataType? dtype(Tensor input_tensor); + DataType? dtype(Tensor input_tensor); - Tensor constant(T value, int?[] shape = null, TFDataType? dtype = null, string name = null); + Tensor constant(T value, int?[] shape = null, DataType? dtype = null, string name = null); int get_uid(string prefix); @@ -204,13 +201,10 @@ public interface IBackend : IDisposable void batch_set_value(List> weight_value_tuples); - Tensor placeholder(int?[] shape = null, int? ndim = null, TFDataType? dtype = Utils.DEFAULT_DTYPE, bool sparse = false, string name = null); + Tensor placeholder(int?[] shape = null, int? ndim = null, DataType? dtype = DataType.DEFAULT_DTYPE, bool sparse = false, string name = null); // Tensor placeholder(int ndim, TFDataType? dtype = Utils.DEFAULT_DTYPE, bool sparse = false, string name = null); - - int?[] int_shape(TFTensor input_tensor); - List batch_get_value(List> weights); void batch_set_value(List<(Tensor, Array)> tuples); @@ -238,9 +232,9 @@ public interface IBackend : IDisposable Tensor update(object m, object v); - Tensor truncated_normal(int[] shape, double v, double stddev, TFDataType dtype, int? seed); + Tensor truncated_normal(int[] shape, double v, double stddev, DataType dtype, int? seed); - Tensor truncated_normal(int?[] shape, double v, double stddev, TFDataType dtype, int? seed); + Tensor truncated_normal(int?[] shape, double v, double stddev, DataType dtype, int? seed); Tensor not_equal(Tensor weights, double v); } diff --git a/Sources/Backends/Base/ITensor.cs b/Sources/Backends/Base/ITensor.cs new file mode 100644 index 0000000..e77d156 --- /dev/null +++ b/Sources/Backends/Base/ITensor.cs @@ -0,0 +1,43 @@ +// Keras-Sharp: C# port of the Keras library +// https://github.com/cesarsouza/keras-sharp +// +// Based under the Keras library for Python. See LICENSE text for more details. +// +// The MIT License(MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +namespace KerasSharp.Backends +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using KerasSharp.Engine.Topology; + using KerasSharp.Losses; + using TensorFlow; + using KerasSharp.Models; + + public interface ITensor + { + + } +} diff --git a/Sources/Backends/CNTK/CNTKBackend.cs b/Sources/Backends/CNTK/CNTKBackend.cs new file mode 100644 index 0000000..c61688d --- /dev/null +++ b/Sources/Backends/CNTK/CNTKBackend.cs @@ -0,0 +1,502 @@ +// Keras-Sharp: C# port of the Keras library +// https://github.com/cesarsouza/keras-sharp +// +// Based under the Keras library for Python. See LICENSE text for more details. +// +// The MIT License(MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +namespace KerasSharp.Backends +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using KerasSharp.Engine.Topology; + using KerasSharp.Losses; + using KerasSharp.Models; + using TensorFlow; + using Accord.Math; + using static KerasSharp.Python; + using C = CNTK.CNTKLib; + using CNTK; + + public class CNTKBackend : BackendBase, IBackend + { + + // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py + + public CNTKBackend() + { + } + + private CNTKTensor tensor(CNTK.Function function) + { + return new CNTKTensor(function); + } + + public CNTKTensor abs(CNTKTensor input) + { + return tensor(C.Abs(input.variable)); + } + + public CNTKTensor add(CNTKTensor a, CNTKTensor b) + { + return tensor(a.variable + b.variable); + } + + public CNTKTensor add(CNTKTensor a, T b) + { + return add(a, constant(b)); + } + + public CNTKTensor add(T a, CNTKTensor b) + { + return add(constant(a), b); + } + + public CNTKTensor argmax(CNTKTensor x, int axis = -1) + { + return tensor(C.Argmax(x.variable, new Axis(axis))); + } + + public List batch_get_value(List weights) + { + throw new NotImplementedException(); + } + + public List batch_get_value(List> weights) + { + throw new NotImplementedException(); + } + + public void batch_set_value(List> weight_value_tuples) + { + throw new NotImplementedException(); + } + + public void batch_set_value(List<(CNTKTensor, Array)> tuples) + { + throw new NotImplementedException(); + } + + public CNTKTensor binary_crossentropy(CNTKTensor output, CNTKTensor target, bool from_logits = false) + { + throw new NotImplementedException(); + } + + public CNTKTensor cast(CNTKTensor x, TFDataType dataType) + { + throw new NotImplementedException(); + } + + public CNTKTensor categorical_crossentropy(CNTKTensor target, CNTKTensor output, bool from_logits = false) + { + throw new NotImplementedException(); + } + + public void clear_session() + { + throw new NotImplementedException(); + } + + public CNTKTensor clip(CNTKTensor norms, int v, int maxValue) + { + throw new NotImplementedException(); + } + + public CNTKTensor clip(CNTKTensor norms, double min_value, double max_value) + { + throw new NotImplementedException(); + } + + public CNTKTensor clip_norm(CNTKTensor g, double clipnorm, CNTKTensor norm) + { + throw new NotImplementedException(); + } + + public CNTKTensor constant(T value, int?[] shape = null, TFDataType? dtype = null, string name = null) + { + throw new NotImplementedException(); + } + + public void Dispose() + { + throw new NotImplementedException(); + } + + public CNTKTensor div(CNTKTensor a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor div(T a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor div(CNTKTensor a, T b) + { + throw new NotImplementedException(); + } + + public CNTKTensor dot(CNTKTensor a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor dropout(object p, double retain_prob, object noise_shape, object seed) + { + throw new NotImplementedException(); + } + + public TFDataType? dtype(CNTKTensor input_tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor elu(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor elu(CNTKTensor x, double alpha) + { + throw new NotImplementedException(); + } + + public CNTKTensor elu(object x) + { + throw new NotImplementedException(); + } + + public CNTKTensor equal(CNTKTensor x, CNTKTensor y) + { + throw new NotImplementedException(); + } + + public object eval(CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor exp(object v) + { + throw new NotImplementedException(); + } + + public TFDataType floatx() + { + throw new NotImplementedException(); + } + + public Models.Function function(object inputs, List list, Func> updates, string name) + { + throw new NotImplementedException(); + } + + public Models.Function function(object inputs, List list, List updates, string name) + { + throw new NotImplementedException(); + } + + public Models.Function function(List inputs, List list, List updates, string name) + { + throw new NotImplementedException(); + } + + public Models.Function function(List inputs, List list, Func> updates, string name) + { + throw new NotImplementedException(); + } + + public int get_uid(string prefix) + { + throw new NotImplementedException(); + } + + public int?[] get_variable_shape(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public List gradients(CNTKTensor loss, List param) + { + throw new NotImplementedException(); + } + + public CNTKTensor greater_equal(CNTKTensor w, double v) + { + throw new NotImplementedException(); + } + + public CNTKTensor hard_sigmoid(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor identity(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public int?[] int_shape(CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public int?[] int_shape(TFTensor input_tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor in_train_phase(Func x, Func alt, bool? training) + { + throw new NotImplementedException(); + } + + public bool is_sparse(CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor l2_normalize(CNTKTensor expected, int axis) + { + throw new NotImplementedException(); + } + + public object learning_phase() + { + throw new NotImplementedException(); + } + + public CNTKTensor max(CNTKTensor x, int v, object p) + { + throw new NotImplementedException(); + } + + public CNTKTensor max(CNTKTensor x, int axis, bool keepdims) + { + throw new NotImplementedException(); + } + + public CNTKTensor max(CNTKTensor tensor, int axis) + { + throw new NotImplementedException(); + } + + public CNTKTensor maximum(double v, CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor mean(CNTKTensor tensor, int axis = -1, bool keepdims = false, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor mean(CNTKTensor tensor, int[] axis, bool keepdims = false, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor minus(CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor mul(CNTKTensor a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor mul(T a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor mul(CNTKTensor a, T b) + { + throw new NotImplementedException(); + } + + public CNTKTensor mul(List batch_outs, int length) + { + throw new NotImplementedException(); + } + + public IDisposable name_scope(string name) + { + throw new NotImplementedException(); + } + + public int? ndim(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor not_equal(CNTKTensor weights, double v) + { + throw new NotImplementedException(); + } + + public CNTKTensor placeholder(int?[] shape = null, int? ndim = null, TFDataType? dtype = TFDataType.Float, bool sparse = false, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor random_uniform(int?[] shape, double minvalue = 0, double maxvalue = 1, TFDataType dtype = TFDataType.Float, int? seed = null, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor relu(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor round(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor sigmoid(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor softmax(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor softplus(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor softsign(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor sqrt(object p) + { + throw new NotImplementedException(); + } + + public CNTKTensor square(CNTKTensor w) + { + throw new NotImplementedException(); + } + + public CNTKTensor subtract(CNTKTensor a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor subtract(CNTKTensor a, T b) + { + throw new NotImplementedException(); + } + + public CNTKTensor subtract(T a, CNTKTensor b) + { + throw new NotImplementedException(); + } + + public CNTKTensor sum(CNTKTensor x, int[] axis = null, bool keepdims = false, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor sum(CNTKTensor x, int axis, bool keepdims = false, string name = null) + { + throw new NotImplementedException(); + } + + public object sum(object[] v) + { + throw new NotImplementedException(); + } + + public CNTKTensor sum(double v, CNTKTensor tensor) + { + throw new NotImplementedException(); + } + + public CNTKTensor tanh(CNTKTensor x) + { + throw new NotImplementedException(); + } + + public CNTKTensor truncated_normal(TFShape shape, double v, double stddev, TFDataType dtype, int? seed) + { + throw new NotImplementedException(); + } + + public CNTKTensor truncated_normal(int[] shape, double v, double stddev, TFDataType dtype, int? seed) + { + throw new NotImplementedException(); + } + + public CNTKTensor truncated_normal(int?[] shape, double v, double stddev, TFDataType dtype, int? seed) + { + throw new NotImplementedException(); + } + + public CNTKTensor update(object m, object v) + { + throw new NotImplementedException(); + } + + public CNTKTensor update_add(CNTKTensor iterations, int v) + { + throw new NotImplementedException(); + } + + public CNTKTensor variable(Array array, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor variable(T value, string name = null) where T : struct + { + throw new NotImplementedException(); + } + + public CNTKTensor variable(CNTKTensor tensor, TFDataType dtype = TFDataType.Float, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor zeros(int[] shape, TFDataType dtype = TFDataType.Float, string name = null) + { + throw new NotImplementedException(); + } + + public CNTKTensor zeros(int?[] shape, TFDataType dtype = TFDataType.Float, string name = null) + { + throw new NotImplementedException(); + } + } +} diff --git a/Sources/Backends/CNTK/CNTKTensor.cs b/Sources/Backends/CNTK/CNTKTensor.cs new file mode 100644 index 0000000..4bb509b --- /dev/null +++ b/Sources/Backends/CNTK/CNTKTensor.cs @@ -0,0 +1,51 @@ +// Keras-Sharp: C# port of the Keras library +// https://github.com/cesarsouza/keras-sharp +// +// Based under the Keras library for Python. See LICENSE text for more details. +// +// The MIT License(MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +namespace KerasSharp.Backends +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using KerasSharp.Engine.Topology; + using KerasSharp.Losses; + using KerasSharp.Models; + using TensorFlow; + using Accord.Math; + using CNTK; + + public class CNTKTensor : ITensor + { + public Variable variable; + private CNTK.Function function; + + public CNTKTensor(CNTK.Function function) + { + this.function = function; + } + } +} diff --git a/Sources/Backends/Current.cs b/Sources/Backends/Current.cs index 5e03fa8..bf33eaa 100644 --- a/Sources/Backends/Current.cs +++ b/Sources/Backends/Current.cs @@ -33,16 +33,60 @@ namespace KerasSharp.Backends using System.Threading.Tasks; using KerasSharp.Engine.Topology; using System.Threading; + using System.Reflection; public static class Current { + private static ThreadLocal backend; - private static ThreadLocal backend = new ThreadLocal(() => new TensorFlowBackend()); + private static string[] assemblyNames = + { + "KerasSharp.Backends.TensorFlow", + "KerasSharp.Backends.CNTK.CPUOnly", + }; public static IBackend K { get { return backend.Value; } set { backend.Value = value; } } + + static Current() + { + backend = new ThreadLocal(() => load("KerasSharp.Backends.TensorFlowBackend")); + } + + + + + + private static IBackend load(string typeName) + { + Type type = find(typeName); + + IBackend obj = (IBackend)Activator.CreateInstance(type); + + return obj; + } + + private static Type find(string typeName) + { + foreach (string assemblyName in assemblyNames) + { + Assembly assembly = Assembly.Load(assemblyName); + + var types = assembly.GetExportedTypes(); + + foreach (var type in types) + { + string currentTypeName = type.FullName; + if (currentTypeName == typeName) + return type; + } + } + + throw new ArgumentException("typeName"); + } + } } diff --git a/Sources/Constraints/Base/IWeightConstraint.cs b/Sources/Constraints/Base/IWeightConstraint.cs index c06cb35..0d61ee5 100644 --- a/Sources/Constraints/Base/IWeightConstraint.cs +++ b/Sources/Constraints/Base/IWeightConstraint.cs @@ -27,7 +27,7 @@ namespace KerasSharp.Constraints { using KerasSharp.Engine.Topology; - using TensorFlow; + /// /// Common interface for weight constraints. diff --git a/Sources/Constraints/MaxNorm.cs b/Sources/Constraints/MaxNorm.cs index 608426d..3b4a267 100644 --- a/Sources/Constraints/MaxNorm.cs +++ b/Sources/Constraints/MaxNorm.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Constraints { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; + using static KerasSharp.Backends.Current; /// diff --git a/Sources/Constraints/MinMaxNorm.cs b/Sources/Constraints/MinMaxNorm.cs index 80a6bfa..2a55c2d 100644 --- a/Sources/Constraints/MinMaxNorm.cs +++ b/Sources/Constraints/MinMaxNorm.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Constraints { using System.Runtime.Serialization; using static KerasSharp.Backends.Current; - using TensorFlow; + using KerasSharp.Engine.Topology; /// diff --git a/Sources/Constraints/NonNeg.cs b/Sources/Constraints/NonNeg.cs index 9609eea..1eb3aa8 100644 --- a/Sources/Constraints/NonNeg.cs +++ b/Sources/Constraints/NonNeg.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Constraints { using System.Runtime.Serialization; using static KerasSharp.Backends.Current; - using TensorFlow; + using KerasSharp.Engine.Topology; /// diff --git a/Sources/Constraints/UnitNorm.cs b/Sources/Constraints/UnitNorm.cs index c61d147..b6bb835 100644 --- a/Sources/Constraints/UnitNorm.cs +++ b/Sources/Constraints/UnitNorm.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Constraints { using System.Runtime.Serialization; using static KerasSharp.Backends.Current; - using TensorFlow; + using KerasSharp.Engine.Topology; /// diff --git a/Sources/DataType.cs b/Sources/DataType.cs new file mode 100644 index 0000000..6611560 --- /dev/null +++ b/Sources/DataType.cs @@ -0,0 +1,38 @@ +using Accord.Math; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace KerasSharp +{ + public enum DataType : uint + { + DEFAULT_DTYPE = Float, + + Float = 1, + Double = 2, + Int32 = 3, + UInt8 = 4, + Int16 = 5, + Int8 = 6, + String = 7, + Complex64 = 8, + Complex = 8, + Int64 = 9, + Bool = 10, + QInt8 = 11, + QUInt8 = 12, + QInt32 = 13, + BFloat16 = 14, + QInt16 = 15, + QUInt16 = 16, + UInt16 = 17, + Complex128 = 18, + Half = 19, + Resource = 20 + } +} diff --git a/Sources/Engine/Topology/Container.cs b/Sources/Engine/Topology/Container.cs index 0ba1118..83f219c 100644 --- a/Sources/Engine/Topology/Container.cs +++ b/Sources/Engine/Topology/Container.cs @@ -35,7 +35,7 @@ namespace KerasSharp.Engine.Topology using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; using static KerasSharp.Python; diff --git a/Sources/Engine/Topology/Input.cs b/Sources/Engine/Topology/Input.cs index d3a20ec..79d599d 100644 --- a/Sources/Engine/Topology/Input.cs +++ b/Sources/Engine/Topology/Input.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Engine.Topology using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + public static class TensorFlowSharp { @@ -61,7 +61,7 @@ public static class TensorFlowSharp /// If set, the layer will not create a placeholder tensor. /// public static List Input(int?[] shape = null, int?[] batch_shape = null, string name = null, - TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE, bool sparse = false, Tensor tensor = null) + DataType dtype = DataType.DEFAULT_DTYPE, bool sparse = false, Tensor tensor = null) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/engine/topology.py#L1416 diff --git a/Sources/Engine/Topology/InputLayer.cs b/Sources/Engine/Topology/InputLayer.cs index f773348..60c1ac9 100644 --- a/Sources/Engine/Topology/InputLayer.cs +++ b/Sources/Engine/Topology/InputLayer.cs @@ -33,7 +33,7 @@ namespace KerasSharp.Layers using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; @@ -69,7 +69,7 @@ public class InputLayer : Layer /// The optional tensor to use as layer input. /// public InputLayer(int?[] input_shape = null, int? batch_size = null, int?[] batch_input_shape = null, string name = null, - TFDataType? dtype = null, bool sparse = false, Tensor input_tensor = null) + DataType? dtype = null, bool sparse = false, Tensor input_tensor = null) : base(dtype: GetType(dtype, input_tensor), name: GetName(name)) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/engine/topology.py#L1291 @@ -148,12 +148,7 @@ public InputLayer(int?[] input_shape = null, int? batch_size = null, int?[] batc output_shapes: new List { batch_input_shape }); } - private static TFDataType? GetType(TFDataType? dtype, TFTensor input_tensor) - { - throw new NotImplementedException(); - } - - private static TFDataType GetType(TFDataType? dtype, Tensor input_tensor) + private static DataType? GetType(DataType? dtype, Tensor input_tensor) { if (dtype == null) { diff --git a/Sources/Engine/Topology/InputSpec.cs b/Sources/Engine/Topology/InputSpec.cs index f5f9fd3..c76c047 100644 --- a/Sources/Engine/Topology/InputSpec.cs +++ b/Sources/Engine/Topology/InputSpec.cs @@ -32,14 +32,14 @@ namespace KerasSharp using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Python; [DataContract] public class InputSpec { - public TFDataType? dtype; + public DataType? dtype; public int[] shape; public int? ndim; public int? max_ndim; @@ -65,7 +65,7 @@ public class InputSpec /// The minimum rank of the input. /// Dictionary mapping integer axes to /// a specific dimension value. - public InputSpec(TFDataType dtype = Utils.DEFAULT_DTYPE, int[] shape = null, int? ndim = null, + public InputSpec(DataType dtype = DataType.DEFAULT_DTYPE, int[] shape = null, int? ndim = null, int? max_ndim = null, int? min_ndim = null, Dictionary axes = null) { this.dtype = dtype; diff --git a/Sources/Engine/Topology/Layer.cs b/Sources/Engine/Topology/Layer.cs index 5a726d6..75f6ace 100644 --- a/Sources/Engine/Topology/Layer.cs +++ b/Sources/Engine/Topology/Layer.cs @@ -36,7 +36,7 @@ namespace KerasSharp.Engine.Topology using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; using static KerasSharp.Python; @@ -76,11 +76,12 @@ public virtual bool trainable public List outbound_nodes; public List inbound_nodes; protected internal int?[] batch_input_shape; - protected internal TFDataType? input_dtype; + protected internal DataType? input_dtype; public bool is_placeholder; - protected internal TFDataType dtype; + protected internal DataType dtype; + public virtual List _flattened_layers { get; set; } public virtual bool stateful { get; set; } @@ -121,7 +122,7 @@ public Layer(string name = null, List input_spec = null, bool trainab bool uses_learning_phase = true, int?[] input_shape = null, int[] output_shape = null, List inbound_nodes = null, List outbound_nodes = null, Tensor input = null, Tensor output = null, Tensor input_mask = null, Tensor output_mask = null, List trainable_weights = null, - List non_trainable_weights = null, Tensor weights = null, TFDataType? dtype = null, + List non_trainable_weights = null, Tensor weights = null, DataType? dtype = null, Dictionary constraints = null, int?[] batch_input_shape = null, int? batch_size = null, int? input_dim = null) { @@ -278,7 +279,7 @@ public virtual List non_trainable_weights /// /// The created weight variable. /// - public Tensor add_weight(string name, int?[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, + public Tensor add_weight(string name, int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE, IWeightInitializer initializer = null, IWeightRegularizer regularizer = null, bool trainable = true, IWeightConstraint constraint = null) { diff --git a/Sources/Engine/Topology/Node.cs b/Sources/Engine/Topology/Node.cs index f2390a2..102899b 100644 --- a/Sources/Engine/Topology/Node.cs +++ b/Sources/Engine/Topology/Node.cs @@ -32,7 +32,7 @@ namespace KerasSharp.Engine.Topology using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; using static KerasSharp.Python; diff --git a/Sources/Engine/Topology/Tensor.cs b/Sources/Engine/Topology/Tensor.cs index 72af3a1..708c0aa 100644 --- a/Sources/Engine/Topology/Tensor.cs +++ b/Sources/Engine/Topology/Tensor.cs @@ -36,16 +36,13 @@ namespace KerasSharp.Engine.Topology using System.Text; using System.Threading.Tasks; using static KerasSharp.Python; - using TensorFlow; using System.Diagnostics; [DataContract] [DebuggerDisplay("{ToString()}")] - public class Tensor + public abstract class Tensor { public IBackend K; - public TFTensor tensor; - public TFOutput output; public int?[] _keras_shape; public bool _uses_learning_phase; public int?[] int_shape; @@ -57,16 +54,6 @@ public class Tensor public string name; - public TFDataType dtype - { - get - { - if (tensor != null) - return tensor.TensorType; - return output.OutputType; - } - } - public Tensor(IBackend backend) { this.K = backend; @@ -85,28 +72,12 @@ public int?[] shape get { return K.int_shape(this); } } - internal long[] TF_Shape - { - get - { - var tf = (K as TensorFlowBackend).tf; - return tf.GetShape(output); - } - } - - public object eval() { return K.eval(this); } - public static implicit operator TFOutput(Tensor t) - { - return t.output; - } - - // TODO: Generate these operators using T4 templates public static Tensor operator *(double a, Tensor b) @@ -154,17 +125,5 @@ public static implicit operator TFOutput(Tensor t) return b.K.subtract(a, b); } - - - - public override string ToString() - { - string n = output.Operation.Name; - long i = output.Index; - string s = str(shape); - string r = $"KerasSharp.Engine.Topology.Tensor '{n}_{i}' shape={s} dtype={output.OutputType}"; - return r; - } } - } diff --git a/Sources/Engine/Training/Model.cs b/Sources/Engine/Training/Model.cs index 7fdac96..e10e615 100644 --- a/Sources/Engine/Training/Model.cs +++ b/Sources/Engine/Training/Model.cs @@ -43,7 +43,7 @@ namespace KerasSharp.Models using Accord.Math; using System.Collections; using KerasSharp.Optimizers; - using TensorFlow; + public enum Shuffle { diff --git a/Sources/Initializers/Base/IWeightInitializer.cs b/Sources/Initializers/Base/IWeightInitializer.cs index 7c0a847..63a8ee4 100644 --- a/Sources/Initializers/Base/IWeightInitializer.cs +++ b/Sources/Initializers/Base/IWeightInitializer.cs @@ -27,7 +27,6 @@ namespace KerasSharp.Initializers { using KerasSharp.Engine.Topology; - using TensorFlow; /// /// Common interface for weight initialization strategies. @@ -46,6 +45,6 @@ public interface IWeightInitializer /// and element data type that has been initialized using this /// strategy. /// - Tensor Call(int?[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE); + Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE); } } \ No newline at end of file diff --git a/Sources/Initializers/Constant.cs b/Sources/Initializers/Constant.cs index 4ce5aec..b2b3ce3 100644 --- a/Sources/Initializers/Constant.cs +++ b/Sources/Initializers/Constant.cs @@ -35,7 +35,6 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; @@ -68,7 +67,7 @@ public Constant(double value) /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { return K.constant(1, shape: shape, dtype: dtype); } diff --git a/Sources/Initializers/GlorotUniform.cs b/Sources/Initializers/GlorotUniform.cs index af7c369..4555ff6 100644 --- a/Sources/Initializers/GlorotUniform.cs +++ b/Sources/Initializers/GlorotUniform.cs @@ -35,7 +35,6 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; @@ -75,7 +74,7 @@ public GlorotUniform(int? seed = null) /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { return new VarianceScaling(scale: 1.0, mode: "fan_avg", distribution: "uniform", seed: seed).Call(shape, dtype); } diff --git a/Sources/Initializers/HeUniform.cs b/Sources/Initializers/HeUniform.cs index 1be6650..31c1cba 100644 --- a/Sources/Initializers/HeUniform.cs +++ b/Sources/Initializers/HeUniform.cs @@ -35,7 +35,7 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; @@ -63,7 +63,7 @@ public class HeUniform : IWeightInitializer /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { return new VarianceScaling(scale: 2.0, mode: "fan_in", distribution: "uniform", seed: seed).Call(shape, dtype); } diff --git a/Sources/Initializers/Ones.cs b/Sources/Initializers/Ones.cs index d899784..730fe16 100644 --- a/Sources/Initializers/Ones.cs +++ b/Sources/Initializers/Ones.cs @@ -35,7 +35,7 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using static KerasSharp.Backends.Current; @@ -58,7 +58,7 @@ public class Ones : IWeightInitializer /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { return K.constant(1, shape: shape, dtype: dtype); } diff --git a/Sources/Initializers/VarianceScaling.cs b/Sources/Initializers/VarianceScaling.cs index c9816e4..c02ddf9 100644 --- a/Sources/Initializers/VarianceScaling.cs +++ b/Sources/Initializers/VarianceScaling.cs @@ -35,7 +35,6 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; @@ -114,7 +113,7 @@ public VarianceScaling(double scale = 1.0, string mode = "fan_in", string distri /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { var (fan_in, fan_out) = _compute_fans(shape.Select(x => x.Value).ToArray()); diff --git a/Sources/Initializers/Zeros.cs b/Sources/Initializers/Zeros.cs index 530e9dc..4f51361 100644 --- a/Sources/Initializers/Zeros.cs +++ b/Sources/Initializers/Zeros.cs @@ -35,7 +35,6 @@ namespace KerasSharp.Initializers using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; @@ -58,7 +57,7 @@ public class Zeros : IWeightInitializer /// and element data type that has been initialized using this /// strategy. /// - public Tensor Call(int?[] shape, TFDataType dtype = KerasSharp.Utils.DEFAULT_DTYPE) + public Tensor Call(int?[] shape, DataType dtype = DataType.DEFAULT_DTYPE) { return K.constant(0, shape: shape, dtype: dtype); } diff --git a/Sources/Keras Sharp.csproj b/Sources/Keras Sharp.csproj index f4e949b..8154883 100644 --- a/Sources/Keras Sharp.csproj +++ b/Sources/Keras Sharp.csproj @@ -34,14 +34,14 @@ 4 - - ..\packages\Accord.3.6.4-alpha\lib\net462\Accord.dll + + ..\packages\Accord.3.7.0\lib\net462\Accord.dll - - ..\packages\Accord.Math.3.6.4-alpha\lib\net462\Accord.Math.dll + + ..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.dll - - ..\packages\Accord.Math.3.6.4-alpha\lib\net462\Accord.Math.Core.dll + + ..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.Core.dll ..\packages\Accord.Statistics.3.6.4-alpha\lib\net462\Accord.Statistics.dll @@ -63,10 +63,11 @@ - ..\packages\TensorFlowSharp.1.2.2\lib\net461\TensorFlowSharp.dll + ..\packages\TensorFlowSharp.1.3.1-pre1\lib\net461\TensorFlowSharp.dll + @@ -81,7 +82,6 @@ - @@ -163,10 +163,9 @@ - - + @@ -175,13 +174,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + + - + \ No newline at end of file diff --git a/Sources/Layers/Convolutional/Conv2D.cs b/Sources/Layers/Convolutional/Conv2D.cs index 896e0c8..b9dcfd6 100644 --- a/Sources/Layers/Convolutional/Conv2D.cs +++ b/Sources/Layers/Convolutional/Conv2D.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Core/Activation.cs b/Sources/Layers/Core/Activation.cs index 83e313e..f3c0a90 100644 --- a/Sources/Layers/Core/Activation.cs +++ b/Sources/Layers/Core/Activation.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Engine.Topology; diff --git a/Sources/Layers/Core/Dense.cs b/Sources/Layers/Core/Dense.cs index 2c485d5..37debfd 100644 --- a/Sources/Layers/Core/Dense.cs +++ b/Sources/Layers/Core/Dense.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Core/Dropout.cs b/Sources/Layers/Core/Dropout.cs index c717553..8e5e741 100644 --- a/Sources/Layers/Core/Dropout.cs +++ b/Sources/Layers/Core/Dropout.cs @@ -34,7 +34,6 @@ namespace KerasSharp.Activations using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; /// diff --git a/Sources/Layers/Core/Flatten.cs b/Sources/Layers/Core/Flatten.cs index b6bc505..f05ce54 100644 --- a/Sources/Layers/Core/Flatten.cs +++ b/Sources/Layers/Core/Flatten.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Core/Merge.cs b/Sources/Layers/Core/Merge.cs index 3c2ac9e..89bc5ee 100644 --- a/Sources/Layers/Core/Merge.cs +++ b/Sources/Layers/Core/Merge.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Embeddings/Embedding.cs b/Sources/Layers/Embeddings/Embedding.cs index 830fba9..eff13c4 100644 --- a/Sources/Layers/Embeddings/Embedding.cs +++ b/Sources/Layers/Embeddings/Embedding.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/MaxPooling2D.cs b/Sources/Layers/MaxPooling2D.cs index 7b53ba2..d465210 100644 --- a/Sources/Layers/MaxPooling2D.cs +++ b/Sources/Layers/MaxPooling2D.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Recurrent/GRU.cs b/Sources/Layers/Recurrent/GRU.cs index cc75d0c..271dab3 100644 --- a/Sources/Layers/Recurrent/GRU.cs +++ b/Sources/Layers/Recurrent/GRU.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Layers/Recurrent/LSTM.cs b/Sources/Layers/Recurrent/LSTM.cs index 5e4a3b5..caa50d5 100644 --- a/Sources/Layers/Recurrent/LSTM.cs +++ b/Sources/Layers/Recurrent/LSTM.cs @@ -31,7 +31,7 @@ namespace KerasSharp using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; + using System.Runtime.Serialization; using KerasSharp.Constraints; using KerasSharp.Regularizers; diff --git a/Sources/Losses/Base/ILoss.cs b/Sources/Losses/Base/ILoss.cs index 7b64b03..e3c3106 100644 --- a/Sources/Losses/Base/ILoss.cs +++ b/Sources/Losses/Base/ILoss.cs @@ -27,7 +27,7 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; - using TensorFlow; + /// /// Common interface for loss functions. diff --git a/Sources/Losses/BinaryCrossEntropy.cs b/Sources/Losses/BinaryCrossEntropy.cs index 9bb83d1..d58a27d 100644 --- a/Sources/Losses/BinaryCrossEntropy.cs +++ b/Sources/Losses/BinaryCrossEntropy.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; + using static KerasSharp.Backends.Current; /// diff --git a/Sources/Losses/CategoricalCrossEntropy.cs b/Sources/Losses/CategoricalCrossEntropy.cs index e7e7158..f3138aa 100644 --- a/Sources/Losses/CategoricalCrossEntropy.cs +++ b/Sources/Losses/CategoricalCrossEntropy.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; + using static KerasSharp.Backends.Current; diff --git a/Sources/Losses/CategoricalHinge.cs b/Sources/Losses/CategoricalHinge.cs index 746dd2b..4c4d2fe 100644 --- a/Sources/Losses/CategoricalHinge.cs +++ b/Sources/Losses/CategoricalHinge.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; + using static KerasSharp.Backends.Current; diff --git a/Sources/Losses/CosineProximity.cs b/Sources/Losses/CosineProximity.cs index d8156e9..e14ffc7 100644 --- a/Sources/Losses/CosineProximity.cs +++ b/Sources/Losses/CosineProximity.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Losses/CustomLoss.cs b/Sources/Losses/CustomLoss.cs index 8fdb1ee..6f7dc15 100644 --- a/Sources/Losses/CustomLoss.cs +++ b/Sources/Losses/CustomLoss.cs @@ -29,7 +29,7 @@ namespace KerasSharp.Losses using KerasSharp.Engine.Topology; using System; using System.Runtime.Serialization; - using TensorFlow; + [DataContract] public class CustomLoss : ILoss diff --git a/Sources/Losses/Hinge.cs b/Sources/Losses/Hinge.cs index 41185ed..f5ca13b 100644 --- a/Sources/Losses/Hinge.cs +++ b/Sources/Losses/Hinge.cs @@ -28,7 +28,7 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; + using static KerasSharp.Backends.Current; [DataContract] diff --git a/Sources/Losses/KullbackLeiberDivergence.cs b/Sources/Losses/KullbackLeiberDivergence.cs index de83c27..c591775 100644 --- a/Sources/Losses/KullbackLeiberDivergence.cs +++ b/Sources/Losses/KullbackLeiberDivergence.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class KullbackLeiberDivergence : ILoss diff --git a/Sources/Losses/LogCosh.cs b/Sources/Losses/LogCosh.cs index 85d6e45..2cfeb02 100644 --- a/Sources/Losses/LogCosh.cs +++ b/Sources/Losses/LogCosh.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class LogCosh : ILoss diff --git a/Sources/Losses/MeanAbsoluteError.cs b/Sources/Losses/MeanAbsoluteError.cs index 66c6bde..3bc5d5e 100644 --- a/Sources/Losses/MeanAbsoluteError.cs +++ b/Sources/Losses/MeanAbsoluteError.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class MeanAbsoluteError : ILoss diff --git a/Sources/Losses/MeanAbsolutePercentageError.cs b/Sources/Losses/MeanAbsolutePercentageError.cs index 46f22a9..e08d4e1 100644 --- a/Sources/Losses/MeanAbsolutePercentageError.cs +++ b/Sources/Losses/MeanAbsolutePercentageError.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class MeanAbsolutePercentageError : ILoss diff --git a/Sources/Losses/MeanSquareError.cs b/Sources/Losses/MeanSquareError.cs index 1bf501a..4d46d97 100644 --- a/Sources/Losses/MeanSquareError.cs +++ b/Sources/Losses/MeanSquareError.cs @@ -27,7 +27,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Losses/MeanSquareLogarithmicError.cs b/Sources/Losses/MeanSquareLogarithmicError.cs index 054a2f9..95051b3 100644 --- a/Sources/Losses/MeanSquareLogarithmicError.cs +++ b/Sources/Losses/MeanSquareLogarithmicError.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class MeanSquareLogarithmicError : ILoss diff --git a/Sources/Losses/Poisson.cs b/Sources/Losses/Poisson.cs index e1ba3a5..0d8d521 100644 --- a/Sources/Losses/Poisson.cs +++ b/Sources/Losses/Poisson.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class Poisson : ILoss diff --git a/Sources/Losses/SparseCategoricalCrossEntropy.cs b/Sources/Losses/SparseCategoricalCrossEntropy.cs index 8151e84..e513fe1 100644 --- a/Sources/Losses/SparseCategoricalCrossEntropy.cs +++ b/Sources/Losses/SparseCategoricalCrossEntropy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class SparseCategoricalCrossEntropy : ILoss diff --git a/Sources/Losses/SquaredHinge.cs b/Sources/Losses/SquaredHinge.cs index b3831c5..f743954 100644 --- a/Sources/Losses/SquaredHinge.cs +++ b/Sources/Losses/SquaredHinge.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Losses { using KerasSharp.Engine.Topology; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class SquaredHinge : ILoss diff --git a/Sources/Metrics/Accuracy.cs b/Sources/Metrics/Accuracy.cs index a5c829a..46930f2 100644 --- a/Sources/Metrics/Accuracy.cs +++ b/Sources/Metrics/Accuracy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; public class Accuracy : IMetric { diff --git a/Sources/Metrics/Base/IMetric.cs b/Sources/Metrics/Base/IMetric.cs index 98cf3d1..685c1b8 100644 --- a/Sources/Metrics/Base/IMetric.cs +++ b/Sources/Metrics/Base/IMetric.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; /// /// Common interface for performance metric functions. diff --git a/Sources/Metrics/BinaryAccuracy.cs b/Sources/Metrics/BinaryAccuracy.cs index 229ef17..2e6d004 100644 --- a/Sources/Metrics/BinaryAccuracy.cs +++ b/Sources/Metrics/BinaryAccuracy.cs @@ -29,7 +29,6 @@ namespace KerasSharp.Metrics using KerasSharp.Engine.Topology; using System; using System.Collections.Generic; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Metrics/CategoricalAccuracy.cs b/Sources/Metrics/CategoricalAccuracy.cs index e31556f..ea3b3af 100644 --- a/Sources/Metrics/CategoricalAccuracy.cs +++ b/Sources/Metrics/CategoricalAccuracy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Metrics/CustomMetric.cs b/Sources/Metrics/CustomMetric.cs index 36a8055..2057faa 100644 --- a/Sources/Metrics/CustomMetric.cs +++ b/Sources/Metrics/CustomMetric.cs @@ -29,7 +29,6 @@ namespace KerasSharp.Metrics using KerasSharp.Engine.Topology; using System; using System.Collections.Generic; - using TensorFlow; public class CustomMetric : IMetric { diff --git a/Sources/Metrics/SparseCategoricalAccuracy.cs b/Sources/Metrics/SparseCategoricalAccuracy.cs index 121be7f..16210dd 100644 --- a/Sources/Metrics/SparseCategoricalAccuracy.cs +++ b/Sources/Metrics/SparseCategoricalAccuracy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; public class SparseCategoricalAccuracy : IMetric { diff --git a/Sources/Metrics/SparseTopKCategoricalAccuracy.cs b/Sources/Metrics/SparseTopKCategoricalAccuracy.cs index e616e28..a5b620e 100644 --- a/Sources/Metrics/SparseTopKCategoricalAccuracy.cs +++ b/Sources/Metrics/SparseTopKCategoricalAccuracy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; public class SparseTopKCategoricalAccuracy : IMetric { diff --git a/Sources/Metrics/TopKCategoricalAccuracy.cs b/Sources/Metrics/TopKCategoricalAccuracy.cs index 732b734..bad25b9 100644 --- a/Sources/Metrics/TopKCategoricalAccuracy.cs +++ b/Sources/Metrics/TopKCategoricalAccuracy.cs @@ -28,7 +28,6 @@ namespace KerasSharp.Metrics { using KerasSharp.Engine.Topology; using System.Collections.Generic; - using TensorFlow; public class TopKCategoricalAccuracy : IMetric { diff --git a/Sources/Models/Sequential.cs b/Sources/Models/Sequential.cs index 621db7b..a9bfce8 100644 --- a/Sources/Models/Sequential.cs +++ b/Sources/Models/Sequential.cs @@ -39,7 +39,6 @@ namespace KerasSharp.Models using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; using static KerasSharp.Python; diff --git a/Sources/Optimizers/Adagrad.cs b/Sources/Optimizers/Adagrad.cs index b3deeb3..908de67 100644 --- a/Sources/Optimizers/Adagrad.cs +++ b/Sources/Optimizers/Adagrad.cs @@ -31,7 +31,6 @@ namespace KerasSharp.Optimizers using System; using System.Collections.Generic; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class Adagrad : OptimizerBase diff --git a/Sources/Optimizers/Adam.cs b/Sources/Optimizers/Adam.cs index ead6b77..174d15e 100644 --- a/Sources/Optimizers/Adam.cs +++ b/Sources/Optimizers/Adam.cs @@ -31,7 +31,6 @@ namespace KerasSharp.Optimizers using System; using System.Collections.Generic; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class Adam : OptimizerBase diff --git a/Sources/Optimizers/Adamax.cs b/Sources/Optimizers/Adamax.cs index 37848ec..490a9c4 100644 --- a/Sources/Optimizers/Adamax.cs +++ b/Sources/Optimizers/Adamax.cs @@ -31,7 +31,6 @@ namespace KerasSharp.Optimizers using System; using System.Collections.Generic; using System.Runtime.Serialization; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Optimizers/Base/Optimizer.cs b/Sources/Optimizers/Base/Optimizer.cs index 1004421..cc920b6 100644 --- a/Sources/Optimizers/Base/Optimizer.cs +++ b/Sources/Optimizers/Base/Optimizer.cs @@ -36,7 +36,6 @@ namespace KerasSharp.Optimizers using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; - using TensorFlow; using static KerasSharp.Backends.Current; @@ -73,7 +72,7 @@ public virtual void get_updates(object param, IWeightConstraint constraints, ILo throw new NotImplementedException(); } - public List get_gradients(Tensor loss, object param) + public List get_gradients(Tensor loss, List param) { List grads = K.gradients(loss, param); if (this.clipnorm > 0 && this.clipnorm > 0) diff --git a/Sources/Optimizers/Nadam.cs b/Sources/Optimizers/Nadam.cs index 793a364..5b79342 100644 --- a/Sources/Optimizers/Nadam.cs +++ b/Sources/Optimizers/Nadam.cs @@ -31,7 +31,6 @@ namespace KerasSharp.Optimizers using System; using System.Collections.Generic; using System.Runtime.Serialization; - using TensorFlow; [DataContract] public class Nadam : OptimizerBase diff --git a/Sources/Optimizers/RMSProp.cs b/Sources/Optimizers/RMSProp.cs index d618cfa..0b2009c 100644 --- a/Sources/Optimizers/RMSProp.cs +++ b/Sources/Optimizers/RMSProp.cs @@ -31,7 +31,6 @@ namespace KerasSharp.Optimizers using System; using System.Collections.Generic; using System.Runtime.Serialization; - using TensorFlow; using static KerasSharp.Backends.Current; using KerasSharp.Engine.Topology; diff --git a/Sources/Optimizers/SGD.cs b/Sources/Optimizers/SGD.cs index f1aefdd..c4e6a6b 100644 --- a/Sources/Optimizers/SGD.cs +++ b/Sources/Optimizers/SGD.cs @@ -34,7 +34,6 @@ namespace KerasSharp.Optimizers using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Python.cs b/Sources/Python.cs index 039e40b..8704e2c 100644 --- a/Sources/Python.cs +++ b/Sources/Python.cs @@ -36,7 +36,6 @@ namespace KerasSharp using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; - using TensorFlow; public static class Python { diff --git a/Sources/Regularizers/Base/IWeightRegularizer.cs b/Sources/Regularizers/Base/IWeightRegularizer.cs index 1a9ee0b..fb45f39 100644 --- a/Sources/Regularizers/Base/IWeightRegularizer.cs +++ b/Sources/Regularizers/Base/IWeightRegularizer.cs @@ -29,7 +29,6 @@ namespace KerasSharp.Regularizers using KerasSharp.Engine.Topology; using System.Collections.Generic; using System.Linq; - using TensorFlow; /// /// Common interface for weight regularizers. diff --git a/Sources/Regularizers/L1L2Regularizer.cs b/Sources/Regularizers/L1L2Regularizer.cs index 21ac070..be9c8e8 100644 --- a/Sources/Regularizers/L1L2Regularizer.cs +++ b/Sources/Regularizers/L1L2Regularizer.cs @@ -32,7 +32,6 @@ namespace KerasSharp.Regularizers using System.Linq; using System.Text; using System.Threading.Tasks; - using TensorFlow; using static KerasSharp.Backends.Current; diff --git a/Sources/Utils.cs b/Sources/Utils.cs deleted file mode 100644 index ca6823b..0000000 --- a/Sources/Utils.cs +++ /dev/null @@ -1,101 +0,0 @@ -// Keras-Sharp: C# port of the Keras library -// https://github.com/cesarsouza/keras-sharp -// -// Based under the Keras library for Python. See LICENSE text for more details. -// -// The MIT License(MIT) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// - -namespace KerasSharp -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text; - using System.Threading.Tasks; - using TensorFlow; - - public static class Utils - { - public const TFDataType DEFAULT_DTYPE = TFDataType.Float; - - public static long GetLastDimension(TFShape shape) - { - return shape[shape.NumDimensions - 1]; - } - - public static Type GetSystemType(TFDataType dtype) - { - switch (dtype) - { - case TFDataType.Float: - return typeof(float); - case TFDataType.Double: - return typeof(double); - case TFDataType.Int32: - return typeof(Int32); - case TFDataType.UInt8: - return typeof(Byte); - case TFDataType.Int16: - break; - case TFDataType.Int8: - break; - case TFDataType.String: - break; - case TFDataType.Complex64: - break; - //case TFDataType.Complex: - // break; - case TFDataType.Int64: - break; - case TFDataType.Bool: - break; - case TFDataType.QInt8: - break; - case TFDataType.QUInt8: - break; - case TFDataType.QInt32: - break; - case TFDataType.BFloat16: - break; - case TFDataType.QInt16: - break; - case TFDataType.QUInt16: - break; - case TFDataType.UInt16: - break; - case TFDataType.Complex128: - break; - case TFDataType.Half: - break; - case TFDataType.Resource: - break; - } - - throw new NotSupportedException(); - } - - internal static object ToNetType(object dtype) - { - throw new NotImplementedException(); - } - } -} diff --git a/Sources/app.config b/Sources/app.config new file mode 100644 index 0000000..96344c3 --- /dev/null +++ b/Sources/app.config @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sources/packages.config b/Sources/packages.config index 0162fdc..bffa5f4 100644 --- a/Sources/packages.config +++ b/Sources/packages.config @@ -1,9 +1,9 @@  - - + + - + \ No newline at end of file diff --git a/Tests/SequentialTest.cs b/Tests/SequentialTest.cs index 0838ba5..7c776da 100644 --- a/Tests/SequentialTest.cs +++ b/Tests/SequentialTest.cs @@ -99,7 +99,7 @@ import numpy as np Assert.AreEqual(new int?[] { null, 500 }, model.input_layers[0].batch_input_shape); Assert.AreEqual(true, model.input_layers[0].built); //Assert.AreEqual(1, model.input_layers[0].constraints); - Assert.AreEqual(TFDataType.Float, model.input_layers[0].dtype); + Assert.AreEqual(DataType.Float, model.input_layers[0].dtype); Assert.AreEqual(1, model.input_layers[0].inbound_nodes.Count); Assert.AreEqual(1, model.input_layers[0].inbound_nodes.Count); var layer_node = model.input_layers[0].inbound_nodes[0]; @@ -249,7 +249,7 @@ import numpy as np //Assert.AreEqual(0, outputLayer.batch_input_shape); Assert.AreEqual(true, outputLayer.built); Assert.AreEqual(0, outputLayer.constraints.Count); - Assert.AreEqual(TFDataType.Float, outputLayer.dtype); + Assert.AreEqual(DataType.Float, outputLayer.dtype); Assert.AreEqual(1, outputLayer.inbound_nodes.Count); Assert.AreEqual("{ dense_1 ([[null, 500]] -> [[null, 32]]) } => dense_2 ([[null, 32]] -> [[null, 10]])", outputLayer.inbound_nodes[0].ToString()); Assert.AreEqual(1, outputLayer.input.Count); diff --git a/Tests/TensorFlowBackendTest.cs b/Tests/TensorFlowBackendTest.cs index 100ddd7..4d6c601 100644 --- a/Tests/TensorFlowBackendTest.cs +++ b/Tests/TensorFlowBackendTest.cs @@ -96,9 +96,9 @@ import numpy as np int?[] shape_b = K.int_shape(b); int?[] shape_ab = K.int_shape(ab); - long[] tf_shape_a = a.TF_Shape; - long[] tf_shape_b = b.TF_Shape; - long[] tf_shape_ab = ab.TF_Shape; + long[] tf_shape_a = K.TF(a).TF_Shape; + long[] tf_shape_b = K.TF(b).TF_Shape; + long[] tf_shape_ab = K.TF(ab).TF_Shape; AssertEx.AreEqual(new int?[] { null, 2 }, shape_a); AssertEx.AreEqual(new int?[] { 2, 3 }, shape_b); @@ -142,7 +142,7 @@ public void variable_test() var b = kvar.eval(); // { { 1, 2 }, { 3, 4 } } #endregion - Assert.AreEqual(TFDataType.Double, a); + Assert.AreEqual(DataType.Double, a); Assert.AreEqual(new double[,] { { 1, 2 }, { 3, 4 } }, b); } } @@ -153,7 +153,7 @@ public void random_uniform() using (var K = new TensorFlowBackend()) { #region doc_random_uniform - var kvar = K.random_uniform(new int?[] { 100, 2000 }, minval: -4, maxval: 2, dtype: TFDataType.Double, seed: 1337, name: "uni"); + var kvar = K.random_uniform(new int?[] { 100, 2000 }, minval: -4, maxval: 2, dtype: DataType.Double, seed: 1337, name: "uni"); var a = K.dtype(kvar); // float64 (Double) var b = kvar.eval(); #endregion diff --git a/Tests/Unit Tests.csproj b/Tests/Unit Tests.csproj index f832fd1..0ccd0c3 100644 --- a/Tests/Unit Tests.csproj +++ b/Tests/Unit Tests.csproj @@ -34,18 +34,21 @@ 4 - - ..\packages\Accord.3.6.4-alpha\lib\net462\Accord.dll + + ..\packages\Accord.3.7.0\lib\net462\Accord.dll - - ..\packages\Accord.Math.3.6.4-alpha\lib\net462\Accord.Math.dll + + ..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.dll - - ..\packages\Accord.Math.3.6.4-alpha\lib\net462\Accord.Math.Core.dll + + ..\packages\Accord.Math.3.7.0\lib\net462\Accord.Math.Core.dll ..\packages\Accord.Statistics.3.6.4-alpha\lib\net462\Accord.Statistics.dll + + ..\packages\CNTK.CPUOnly.2.2.0\lib\net45\x64\Cntk.Core.Managed-2.2.dll + ..\packages\NUnit.3.6.1\lib\net45\nunit.framework.dll @@ -56,7 +59,7 @@ ..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll - ..\packages\TensorFlowSharp.1.2.2\lib\net461\TensorFlowSharp.dll + ..\packages\TensorFlowSharp.1.3.1-pre1\lib\net461\TensorFlowSharp.dll @@ -67,27 +70,40 @@ - + + + Designer + + + {99befbbe-88c2-426a-b687-0d0928b54f05} + CNTK %28CPU%29 + + + {31e970fd-4952-4a3a-9cc9-f9b519c3b20d} + TensorFlow %28CPU%29 + {1478e4e5-f4d5-4066-886b-544d1e172374} Keras Sharp - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + + + - + + +