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