Skip to content

Commit 79aaae2

Browse files
committed
normalize_tuple, InputSpec
1 parent a466159 commit 79aaae2

13 files changed

Lines changed: 191 additions & 18 deletions

File tree

TensorFlow.NET.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "t
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "src\TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{0254BFF9-453C-4FE0-9609-3644559A79CE}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -29,10 +29,10 @@ Global
2929
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
3030
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU
3131
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.Build.0 = Release|Any CPU
3636
EndGlobalSection
3737
GlobalSection(SolutionProperties) = preSolution
3838
HideSolutionNode = FALSE

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,30 @@ public static Tensor conv2d(Tensor inputs,
1919
int[] dilation_rate = null,
2020
bool use_bias = true,
2121
IActivation activation = null,
22-
IInitializer kernel_initializer = null)
22+
IInitializer kernel_initializer = null,
23+
IInitializer bias_initializer = null,
24+
bool trainable = true,
25+
string name = null)
2326
{
2427
if (strides == null)
2528
strides = new int[] { 1, 1 };
2629
if (dilation_rate == null)
2730
dilation_rate = new int[] { 1, 1 };
31+
if (bias_initializer == null)
32+
bias_initializer = tf.zeros_initializer;
2833

29-
var layer = new Conv2D(filters, kernel_size);
34+
var layer = new Conv2D(filters,
35+
kernel_size: kernel_size,
36+
strides: strides,
37+
padding: padding,
38+
data_format: data_format,
39+
dilation_rate: dilation_rate,
40+
activation: activation,
41+
use_bias: use_bias,
42+
kernel_initializer: kernel_initializer,
43+
bias_initializer: bias_initializer,
44+
trainable: trainable,
45+
name: name);
3046

3147
return layer.apply(inputs);
3248
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.Engine
6+
{
7+
/// <summary>
8+
/// Specifies the ndim, dtype and shape of every input to a layer.
9+
/// </summary>
10+
public class InputSpec
11+
{
12+
public InputSpec(TF_DataType dtype = TF_DataType.DtInvalid)
13+
{
14+
15+
}
16+
}
17+
}

src/TensorFlowNET.Core/Keras/Engine/Layer.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,77 @@ namespace Tensorflow.Keras.Engine
1212
/// </summary>
1313
public class Layer : CheckpointableBase
1414
{
15+
protected bool trainable;
16+
protected string _name;
17+
protected TF_DataType _dtype;
18+
protected Graph _graph;
19+
protected string _base_name;
20+
protected VariableScope _scope;
21+
/// <summary>
22+
/// A stateful layer is a layer whose updates are run during inference too,
23+
/// for instance stateful RNNs.
24+
/// </summary>
25+
protected bool stateful;
26+
/// <summary>
27+
/// Indicates whether `build` needs to be called upon layer call, to create
28+
/// the layer's weights.
29+
/// </summary>
30+
protected bool built;
31+
/// <summary>
32+
/// Provides information about which inputs are compatible with the layer.
33+
/// </summary>
34+
protected InputSpec input_spec;
35+
protected bool supports_masking;
1536

37+
public Layer(bool trainable = true,
38+
string name = null,
39+
TF_DataType dtype = TF_DataType.DtInvalid)
40+
{
41+
this.trainable = trainable;
42+
this.stateful = false;
43+
this.built = false;
44+
this.supports_masking = false;
45+
_init_set_name(name);
46+
}
47+
48+
public Tensor apply(Tensor inputs)
49+
{
50+
return __call__(inputs);
51+
}
52+
53+
public Tensor __call__(Tensor inputs,
54+
VariableScope scope = null)
55+
{
56+
_set_scope(scope);
57+
_graph = ops._get_graph_from_inputs(new List<Tensor> { inputs }, graph: _graph);
58+
var scope_context_manager = tf.variable_scope(_scope);
59+
60+
throw new NotImplementedException("");
61+
}
62+
63+
private void _init_set_name(string name)
64+
{
65+
if (string.IsNullOrEmpty(name))
66+
(_name, _base_name) = _make_unique_name();
67+
}
68+
69+
private (string, string) _make_unique_name()
70+
{
71+
string base_name = "conv2d";
72+
string name = base_layer_utils.unique_layer_name(base_name);
73+
return (name, base_name);
74+
}
75+
76+
private void _set_scope(VariableScope scope = null)
77+
{
78+
if (_scope == null)
79+
{
80+
Python.with(tf.variable_scope(scope, default_name: _base_name), captured_scope =>
81+
{
82+
_scope = captured_scope;
83+
});
84+
}
85+
86+
}
1687
}
1788
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.Engine
6+
{
7+
public class base_layer_utils
8+
{
9+
/// <summary>
10+
/// Makes a layer name (or arbitrary string) unique within a TensorFlow graph.
11+
/// </summary>
12+
/// <param name="name"></param>
13+
/// <returns></returns>
14+
public static string unique_layer_name(string name)
15+
{
16+
int number = get_default_graph_uid_map();
17+
return $"{name}_{number}";
18+
}
19+
20+
public static int get_default_graph_uid_map()
21+
{
22+
var graph = ops.get_default_graph();
23+
return graph._next_id();
24+
}
25+
}
26+
}
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using Tensorflow.Keras.Engine;
5+
using Tensorflow.Operations.Activation;
46

57
namespace Tensorflow.Keras.Layers
68
{
7-
public class Conv
9+
public class Conv : Layer
810
{
11+
protected int rank;
12+
protected int filters;
13+
protected int[] kernel_size;
14+
protected int[] strides;
15+
protected string padding;
16+
protected string data_format;
17+
protected int[] dilation_rate;
18+
protected IActivation activation;
19+
protected bool use_bias;
20+
protected IInitializer kernel_initializer;
21+
protected IInitializer bias_initializer;
22+
23+
public Conv(int rank,
24+
int filters,
25+
int[] kernel_size,
26+
int[] strides = null,
27+
string padding = "valid",
28+
string data_format = null,
29+
int[] dilation_rate = null,
30+
IActivation activation = null,
31+
bool use_bias = true,
32+
IInitializer kernel_initializer = null,
33+
IInitializer bias_initializer = null,
34+
bool trainable = true,
35+
string name = null) : base(trainable: trainable, name: name)
36+
{
37+
this.rank = rank;
38+
this.filters = filters;
39+
this.kernel_size = kernel_size;
40+
this.strides = strides;
41+
this.padding = padding;
42+
this.data_format = data_format;
43+
this.dilation_rate = dilation_rate;
44+
this.activation = activation;
45+
this.use_bias = use_bias;
46+
this.kernel_initializer = kernel_initializer;
47+
this.bias_initializer = bias_initializer;
48+
}
949
}
1050
}

src/TensorFlowNET.Core/Keras/Layers/Conv2D.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ namespace Tensorflow.Keras.Layers
77
{
88
public class Conv2D : Conv
99
{
10-
private int filters;
11-
private int[] kernel_size;
12-
private int[] strides;
13-
1410
public Conv2D(int filters,
1511
int[] kernel_size,
1612
int[] strides = null,
@@ -22,14 +18,21 @@ public Conv2D(int filters,
2218
IInitializer kernel_initializer = null,
2319
IInitializer bias_initializer = null,
2420
bool trainable = true,
25-
string name = null)
21+
string name = null) : base(2,
22+
filters,
23+
kernel_size,
24+
strides: strides,
25+
padding: padding,
26+
data_format: data_format,
27+
dilation_rate: dilation_rate,
28+
activation: activation,
29+
use_bias: use_bias,
30+
kernel_initializer: kernel_initializer,
31+
bias_initializer: bias_initializer,
32+
trainable: trainable,
33+
name: name)
2634
{
2735

2836
}
29-
30-
public Tensor apply(Tensor inputs)
31-
{
32-
throw new NotImplementedException("apply");
33-
}
3437
}
3538
}

TensorFlowNET.Visualization/Controllers/ValuesController.cs renamed to src/TensorFlowNET.Visualization/Controllers/ValuesController.cs

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)