Skip to content

Commit 29042df

Browse files
committed
add VariableArgs.
1 parent ef82af7 commit 29042df

7 files changed

Lines changed: 77 additions & 57 deletions

File tree

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected virtual IVariableV1 add_weight(string name,
250250
TF_DataType dtype = TF_DataType.DtInvalid,
251251
IInitializer initializer = null,
252252
bool? trainable = null,
253-
Func<string, int[], TF_DataType, IInitializer, bool, IVariableV1> getter = null)
253+
Func<VariableArgs, IVariableV1> getter = null)
254254
{
255255
if (dtype == TF_DataType.DtInvalid)
256256
dtype = TF_DataType.TF_FLOAT;
@@ -259,7 +259,7 @@ protected virtual IVariableV1 add_weight(string name,
259259
trainable = true;
260260

261261
// Initialize variable when no initializer provided
262-
if(initializer == null)
262+
if (initializer == null)
263263
{
264264
// If dtype is DT_FLOAT, provide a uniform unit scaling initializer
265265
if (dtype.is_floating())
@@ -269,13 +269,18 @@ protected virtual IVariableV1 add_weight(string name,
269269
else
270270
throw new ValueError($"An initializer for variable {name} of type {dtype.as_base_dtype()} is required for layer {this.name}");
271271
}
272-
var variable = _add_variable_with_custom_getter(name,
273-
shape,
274-
dtype: dtype,
275-
getter: (getter == null) ? base_layer_utils.make_variable : getter,
276-
overwrite: true,
277-
initializer: initializer,
278-
trainable: trainable.Value);
272+
273+
var variable = _add_variable_with_custom_getter(new VariableArgs
274+
{
275+
Name = name,
276+
Shape = shape,
277+
DType = dtype,
278+
Getter = getter ?? base_layer_utils.make_variable,
279+
Overwrite = true,
280+
Initializer = initializer,
281+
Trainable = trainable.Value
282+
});
283+
279284
//backend.track_variable(variable);
280285
if (trainable == true)
281286
_trainable_weights.Add(variable);

‎src/TensorFlowNET.Core/Keras/Optimizers/OptimizerV2.cs‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ void _create_slots(ResourceVariable[] var_list)
199199
}
200200
}
201201

202-
ResourceVariable add_weight(string name,
203-
TensorShape shape,
202+
ResourceVariable add_weight(string name,
203+
TensorShape shape,
204204
TF_DataType dtype = TF_DataType.TF_FLOAT,
205205
IInitializer initializer = null,
206206
bool trainable = false,
@@ -213,16 +213,19 @@ ResourceVariable add_weight(string name,
213213
if (dtype == TF_DataType.DtInvalid)
214214
dtype = TF_DataType.TF_FLOAT;
215215

216-
var variable = _add_variable_with_custom_getter(name: name,
217-
shape: shape,
218-
getter: base_layer_utils.make_variable,
219-
dtype: dtype,
220-
overwrite: true,
221-
initializer: initializer,
222-
trainable: trainable,
223-
use_resource: true,
224-
synchronization: synchronization,
225-
aggregation: aggregation);
216+
var variable = _add_variable_with_custom_getter(new VariableArgs
217+
{
218+
Name = name,
219+
Shape = shape,
220+
Getter = base_layer_utils.make_variable,
221+
DType = dtype,
222+
Overwrite = true,
223+
Initializer = initializer,
224+
Trainable = trainable,
225+
UseResource = true,
226+
Synchronization = synchronization,
227+
Aggregation = aggregation
228+
});
226229

227230
return variable as ResourceVariable;
228231
}

‎src/TensorFlowNET.Core/Keras/Utils/base_layer_utils.cs‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,26 @@ public class base_layer_utils
2626
/// <summary>
2727
/// Adds a new variable to the layer.
2828
/// </summary>
29-
/// <param name="name"></param>
30-
/// <param name="shape"></param>
31-
/// <param name="dtype"></param>
32-
/// <param name="initializer"></param>
33-
/// <param name="trainable"></param>
29+
/// <param name="args"></param>
3430
/// <returns></returns>
35-
public static IVariableV1 make_variable(string name,
36-
int[] shape,
37-
TF_DataType dtype = TF_DataType.TF_FLOAT,
38-
IInitializer initializer = null,
39-
bool trainable = true)
31+
public static IVariableV1 make_variable(VariableArgs args)
4032
{
4133
#pragma warning disable CS0219 // Variable is assigned but its value is never used
4234
var initializing_from_value = false;
43-
bool use_resource = true;
4435
#pragma warning restore CS0219 // Variable is assigned but its value is never used
4536

4637
ops.init_scope();
4738

48-
Func<Tensor> init_val = () => initializer.call(new TensorShape(shape), dtype: dtype);
39+
Func<Tensor> init_val = () => args.Initializer.call(args.Shape, dtype: args.DType);
4940

50-
var variable_dtype = dtype.as_base_dtype();
41+
var variable_dtype = args.DType.as_base_dtype();
5142
var v = tf.Variable(init_val,
52-
dtype: dtype,
53-
shape: shape,
54-
name: name);
43+
dtype: args.DType,
44+
shape: args.Shape,
45+
name: args.Name,
46+
trainable: args.Trainable,
47+
validate_shape: args.ValidateShape,
48+
use_resource: args.UseResource);
5549

5650
return v;
5751
}

‎src/TensorFlowNET.Core/Layers/Layer.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ protected virtual IVariableV1 add_weight(string name,
167167
dtype: dtype,
168168
initializer: initializer,
169169
trainable: trainable,
170-
getter: (name1, shape1, dtype1, initializer1, trainable1) =>
171-
tf.compat.v1.get_variable(name1,
172-
shape: new TensorShape(shape1),
173-
dtype: dtype1,
174-
initializer: initializer1,
175-
trainable: trainable1)
170+
getter: (args) =>
171+
tf.compat.v1.get_variable(args.Name,
172+
shape: args.Shape,
173+
dtype: args.DType,
174+
initializer: args.Initializer,
175+
trainable: args.Trainable)
176176
);
177177

178178
//if (init_graph != null)

‎src/TensorFlowNET.Core/Training/Trackable.cs‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ public abstract class Trackable
2727
/// Restore-on-create for a variable be saved with this `Checkpointable`.
2828
/// </summary>
2929
/// <returns></returns>
30-
protected virtual IVariableV1 _add_variable_with_custom_getter(string name,
31-
int[] shape,
32-
TF_DataType dtype = TF_DataType.TF_FLOAT,
33-
IInitializer initializer = null,
34-
Func<string, int[], TF_DataType, IInitializer, bool, IVariableV1> getter = null,
35-
bool overwrite = false,
36-
bool trainable = false,
37-
bool use_resource = false,
38-
VariableSynchronization synchronization = VariableSynchronization.Auto,
39-
VariableAggregation aggregation = VariableAggregation.None)
30+
protected virtual IVariableV1 _add_variable_with_custom_getter(VariableArgs args)
4031
{
4132
ops.init_scope();
4233
#pragma warning disable CS0219 // Variable is assigned but its value is never used
@@ -50,15 +41,15 @@ protected virtual IVariableV1 _add_variable_with_custom_getter(string name,
5041
checkpoint_initializer = null;
5142

5243
IVariableV1 new_variable;
53-
new_variable = getter(name, shape, dtype, initializer, trainable);
44+
new_variable = args.Getter(args);
5445

5546
// If we set an initializer and the variable processed it, tracking will not
5647
// assign again. It will add this variable to our dependencies, and if there
5748
// is a non-trivial restoration queued, it will handle that. This also
5849
// handles slot variables.
59-
if (!overwrite || new_variable is RefVariable)
60-
return _track_checkpointable(new_variable, name: name,
61-
overwrite: overwrite);
50+
if (!args.Overwrite || new_variable is RefVariable)
51+
return _track_checkpointable(new_variable, name: args.Name,
52+
overwrite: args.Overwrite);
6253
else
6354
return new_variable;
6455
}
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
6+
{
7+
public class VariableArgs
8+
{
9+
public object InitialValue { get; set; }
10+
public Func<VariableArgs, IVariableV1> Getter { get; set; }
11+
public string Name { get; set; }
12+
public TensorShape Shape { get; set; }
13+
public TF_DataType DType { get; set; } = TF_DataType.DtInvalid;
14+
public IInitializer Initializer { get; set; }
15+
public bool Trainable { get; set; }
16+
public bool ValidateShape { get; set; } = true;
17+
public bool UseResource { get; set; } = true;
18+
public bool Overwrite { get; set; }
19+
public List<string> Collections { get; set; }
20+
public string CachingDevice { get; set; } = "";
21+
public VariableDef VariableDef { get; set; }
22+
public string ImportScope { get; set; } = "";
23+
public VariableSynchronization Synchronization { get; set; } = VariableSynchronization.Auto;
24+
public VariableAggregation Aggregation { get; set; } = VariableAggregation.None;
25+
}
26+
}

‎src/TensorFlowNET.Core/tensorflow.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private void InitGradientEnvironment()
6262
public ResourceVariable Variable<T>(T data,
6363
bool trainable = true,
6464
bool validate_shape = true,
65+
bool use_resource = true,
6566
string name = null,
6667
TF_DataType dtype = TF_DataType.DtInvalid,
6768
int[] shape = null)

0 commit comments

Comments
 (0)