Skip to content

Commit 5f1f598

Browse files
committed
Partial implementation of tf.keras. SciSharp#355
1 parent 34efa15 commit 5f1f598

30 files changed

Lines changed: 329 additions & 196 deletions

TensorFlow.NET.sln

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "src
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.UnitTest", "test\TensorFlowNET.UnitTest\Tensorflow.UnitTest.csproj", "{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras", "src\TensorFlowNET.Keras\Tensorflow.Keras.csproj", "{6268B461-486A-460B-9B3C-86493CBBAAF7}"
13-
EndProject
14-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Keras.UnitTest", "test\Tensorflow.Keras.UnitTest\Tensorflow.Keras.UnitTest.csproj", "{EB92DD90-6346-41FB-B967-2B33A860AD98}"
15-
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNET.Console", "src\TensorFlowNET.Console\TensorFlowNET.Console.csproj", "{03F06299-3F4B-4449-A709-3A647657BC0C}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Console", "src\TensorFlowNET.Console\TensorFlowNET.Console.csproj", "{03F06299-3F4B-4449-A709-3A647657BC0C}"
1713
EndProject
1814
Global
1915
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -103,54 +99,6 @@ Global
10399
{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x64.Build.0 = Release|x64
104100
{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x86.ActiveCfg = Release|Any CPU
105101
{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x86.Build.0 = Release|Any CPU
106-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
108-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|x64.ActiveCfg = Debug|x64
109-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|x64.Build.0 = Debug|x64
110-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|x86.ActiveCfg = Debug|Any CPU
111-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug|x86.Build.0 = Debug|Any CPU
112-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
113-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
114-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|x64.ActiveCfg = Debug|x64
115-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|x64.Build.0 = Debug|x64
116-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|x86.ActiveCfg = Debug|Any CPU
117-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Debug-Minimal|x86.Build.0 = Debug|Any CPU
118-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|Any CPU.ActiveCfg = Release|Any CPU
119-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|Any CPU.Build.0 = Release|Any CPU
120-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|x64.ActiveCfg = Release|x64
121-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|x64.Build.0 = Release|x64
122-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|x86.ActiveCfg = Release|Any CPU
123-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Publish|x86.Build.0 = Release|Any CPU
124-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
125-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|Any CPU.Build.0 = Release|Any CPU
126-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x64.ActiveCfg = Release|x64
127-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x64.Build.0 = Release|x64
128-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x86.ActiveCfg = Release|Any CPU
129-
{6268B461-486A-460B-9B3C-86493CBBAAF7}.Release|x86.Build.0 = Release|Any CPU
130-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|Any CPU.Build.0 = Debug|Any CPU
132-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x64.ActiveCfg = Debug|x64
133-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x64.Build.0 = Debug|x64
134-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x86.ActiveCfg = Debug|Any CPU
135-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug|x86.Build.0 = Debug|Any CPU
136-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
137-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
138-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|x64.ActiveCfg = Debug|x64
139-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|x64.Build.0 = Debug|x64
140-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|x86.ActiveCfg = Debug|Any CPU
141-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Debug-Minimal|x86.Build.0 = Debug|Any CPU
142-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|Any CPU.ActiveCfg = Release|Any CPU
143-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|Any CPU.Build.0 = Release|Any CPU
144-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|x64.ActiveCfg = Release|x64
145-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|x64.Build.0 = Release|x64
146-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|x86.ActiveCfg = Release|Any CPU
147-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Publish|x86.Build.0 = Release|Any CPU
148-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|Any CPU.ActiveCfg = Release|Any CPU
149-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|Any CPU.Build.0 = Release|Any CPU
150-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.ActiveCfg = Release|x64
151-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x64.Build.0 = Release|x64
152-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x86.ActiveCfg = Release|Any CPU
153-
{EB92DD90-6346-41FB-B967-2B33A860AD98}.Release|x86.Build.0 = Release|Any CPU
154102
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
155103
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
156104
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.ActiveCfg = Debug|Any CPU

src/TensorFlowNET.Core/APIs/tf.layers.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using System;
1718
using System.Collections.Generic;
1819
using System.Linq;
1920
using NumSharp;
21+
using Tensorflow.Keras.ArgsDefinition;
2022
using Tensorflow.Keras.Layers;
2123
using Tensorflow.Operations.Activation;
2224
using static Tensorflow.Binding;
@@ -173,14 +175,19 @@ public Tensor dense(Tensor inputs,
173175
if (bias_initializer == null)
174176
bias_initializer = tf.zeros_initializer;
175177

176-
var layer = new Dense(units, activation,
177-
use_bias: use_bias,
178-
bias_initializer: bias_initializer,
179-
kernel_initializer: kernel_initializer,
180-
trainable: trainable,
181-
name: name);
182-
183-
return layer.apply(inputs).Item1;
178+
var layer = new Dense(new DenseArgs
179+
{
180+
Units = units,
181+
Activation = activation,
182+
UseBias = use_bias,
183+
BiasInitializer = bias_initializer,
184+
KernelInitializer = kernel_initializer,
185+
Trainable = trainable,
186+
Name = name
187+
});
188+
189+
throw new NotImplementedException("");
190+
//return layer.apply(inputs).Item1;
184191
}
185192

186193
/// <summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ public Tensor sigmoid<T>(T x, string name = null)
515515
public Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null)
516516
=> gen_math_ops._sum(input, axis, keep_dims: keep_dims, name: name);
517517

518+
public Tensor reduce_mean(Tensor input_tensors, int axis, bool keepdims = false, string name = null)
519+
=> math_ops.reduce_mean(input_tensors, axis: new[] { axis }, keepdims: keepdims, name: name);
520+
518521
public Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
519522
=> math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices);
520523

src/TensorFlowNET.Core/Data/DatasetManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Tensorflow
77
{
88
public class DatasetManager
99
{
10-
public IDatasetV2 from_tensor_slices(NDArray features, NDArray labels)
10+
public IDatasetV2 from_tensor_slices(Tensor features, Tensor labels)
1111
=> new TensorSliceDataset(features, labels);
1212
}
1313
}

src/TensorFlowNET.Core/Data/TensorSliceDataset.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace Tensorflow
1111
{
1212
public class TensorSliceDataset : DatasetSource
1313
{
14-
public TensorSliceDataset(NDArray features, NDArray labels)
14+
public TensorSliceDataset(Tensor features, Tensor labels)
1515
{
16-
_tensors = new[] { tf.convert_to_tensor(features), tf.convert_to_tensor(labels) };
16+
_tensors = new[] { features, labels };
1717
var batched_spec = _tensors.Select(x => x.ToTensorSpec()).ToArray();
1818
structure = batched_spec.Select(x => x._unbatch()).ToArray();
1919

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Tensorflow
4+
{
5+
public class InvalidArgumentError : TensorflowException
6+
{
7+
public InvalidArgumentError() : base()
8+
{
9+
10+
}
11+
12+
public InvalidArgumentError(string message) : base(message)
13+
{
14+
15+
}
16+
}
17+
}

src/TensorFlowNET.Core/Gradients/GradientTape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Tensor gradient(Tensor target, ResourceVariable source)
119119
return (results[0], results[1]);
120120
}
121121

122-
public Tensor[] gradient(Tensor target, ResourceVariable[] sources)
122+
public Tensor[] gradient(Tensor target, IEnumerable<IVariableV1> sources)
123123
{
124124
if (_recording)
125125
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Operations.Activation;
5+
using static Tensorflow.Binding;
6+
7+
namespace Tensorflow.Keras.ArgsDefinition
8+
{
9+
public class DenseArgs : LayerArgs
10+
{
11+
/// <summary>
12+
/// Positive integer, dimensionality of the output space.
13+
/// </summary>
14+
public int Units { get; set; }
15+
16+
/// <summary>
17+
/// Activation function to use.
18+
/// </summary>
19+
public IActivation Activation { get; set; }
20+
21+
/// <summary>
22+
/// Whether the layer uses a bias vector.
23+
/// </summary>
24+
public bool UseBias { get; set; } = true;
25+
26+
/// <summary>
27+
/// Initializer for the `kernel` weights matrix.
28+
/// </summary>
29+
public IInitializer KernelInitializer { get; set; } = tf.glorot_uniform_initializer;
30+
31+
/// <summary>
32+
/// Initializer for the bias vector.
33+
/// </summary>
34+
public IInitializer BiasInitializer { get; set; } = tf.zeros_initializer;
35+
36+
/// <summary>
37+
/// Regularizer function applied to the `kernel` weights matrix.
38+
/// </summary>
39+
public IInitializer KernelRegularizer { get; set; }
40+
41+
/// <summary>
42+
/// Regularizer function applied to the bias vector.
43+
/// </summary>
44+
public IInitializer BiasRegularizer { get; set; }
45+
46+
/// <summary>
47+
/// Constraint function applied to the `kernel` weights matrix.
48+
/// </summary>
49+
public Action KernelConstraint { get; set; }
50+
51+
/// <summary>
52+
/// Constraint function applied to the bias vector.
53+
/// </summary>
54+
public Action BiasConstraint { get; set; }
55+
}
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.ArgsDefinition
6+
{
7+
public class LayerArgs
8+
{
9+
/// <summary>
10+
/// Indicates whether the layer's weights are updated during training
11+
/// and whether the layer's updates are run during training.
12+
/// </summary>
13+
public bool Trainable { get; set; } = true;
14+
15+
public string Name { get; set; }
16+
17+
/// <summary>
18+
/// Only applicable to input layers.
19+
/// </summary>
20+
public TF_DataType DType { get; set; }
21+
22+
/// <summary>
23+
/// Whether the `call` method can be used to build a TF graph without issues.
24+
/// This attribute has no effect if the model is created using the Functional
25+
/// API. Instead, `model.dynamic` is determined based on the internal layers.
26+
/// </summary>
27+
public bool Dynamic { get; set; } = false;
28+
29+
/// <summary>
30+
/// Only applicable to input layers.
31+
/// </summary>
32+
public TensorShape InputShape { get; set; }
33+
34+
/// <summary>
35+
/// Only applicable to input layers.
36+
/// </summary>
37+
public TensorShape BatchInputShape { get; set; }
38+
39+
/// <summary>
40+
/// Initial weight values.
41+
/// </summary>
42+
public float[] Weights { get; set; }
43+
44+
/// <summary>
45+
/// Regularizer function applied to the output of the layer(its "activation").
46+
/// </summary>
47+
public IInitializer ActivityRegularizer { get; set; }
48+
49+
public bool Autocast { get; set; }
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.ArgsDefinition
6+
{
7+
public class ModelArgs : LayerArgs
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)