Skip to content

Commit a8a0731

Browse files
committed
Change RefVariable to IVariableV1.
1 parent 468cb8e commit a8a0731

26 files changed

Lines changed: 111 additions & 133 deletions

src/TensorFlowNET.Core/APIs/tf.compat.v1.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System;
18+
using System.Collections.Generic;
1819
using Tensorflow.Eager;
1920
using static Tensorflow.Binding;
2021

@@ -26,5 +27,29 @@ public void disable_eager_execution()
2627
{
2728
tf.context.default_execution_mode = Context.GRAPH_MODE;
2829
}
30+
31+
public IVariableV1 get_variable(string name,
32+
TensorShape shape = null,
33+
TF_DataType dtype = TF_DataType.DtInvalid,
34+
object initializer = null, // IInitializer or Tensor
35+
bool? trainable = null,
36+
List<string> collections = null,
37+
bool? use_resource = null,
38+
bool validate_shape = true,
39+
VariableSynchronization synchronization = VariableSynchronization.Auto,
40+
VariableAggregation aggregation = VariableAggregation.None)
41+
{
42+
var scope = Tensorflow.variable_scope.get_variable_scope();
43+
var store = Tensorflow.variable_scope._get_default_variable_store();
44+
return scope.get_variable(store,
45+
name,
46+
shape: shape,
47+
dtype: dtype,
48+
use_resource: use_resource,
49+
validate_shape: validate_shape,
50+
initializer: initializer,
51+
trainable: trainable,
52+
collections: collections);
53+
}
2954
}
3055
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public partial class tensorflow
2727

2828
public class nn_internal
2929
{
30-
public Tensor conv2d(Tensor input, RefVariable filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
30+
public Tensor conv2d(Tensor input, IVariableV1 filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
3131
string data_format= "NHWC", int[] dilations= null, string name = null)
3232
{
3333
var parameters = new Conv2dParams
3434
{
3535
Input = input,
36-
Filter = filter,
36+
Filter = filter.AsTensor(),
3737
Strides = strides,
3838
Padding = padding,
3939
UseCudnnOnGpu = use_cudnn_on_gpu,
@@ -98,7 +98,7 @@ public Tensor elu(Tensor features, string name = null)
9898
name: name,
9999
keep_dims: keep_dims);
100100

101-
public Tensor embedding_lookup(RefVariable @params,
101+
public Tensor embedding_lookup(IVariableV1 @params,
102102
Tensor ids,
103103
string partition_strategy = "mod",
104104
string name = null) => embedding_ops._embedding_lookup_and_transform(@params,
@@ -150,12 +150,12 @@ public Tensor in_top_k(Tensor predictions, Tensor targets, int k, string name =
150150
public Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name = null)
151151
=> gen_nn_ops.top_kv2(input, k: k, sorted: sorted, name: name);
152152

153-
public Tensor bias_add(Tensor value, RefVariable bias, string data_format = null, string name = null)
153+
public Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null, string name = null)
154154
{
155155
return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
156156
{
157157
name = scope;
158-
return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name);
158+
return gen_nn_ops.bias_add(value, bias.AsTensor(), data_format: data_format, name: name);
159159
});
160160
}
161161

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Tensorflow
1818
{
1919
public partial class tensorflow
2020
{
21-
public Tensor assign_add<T>(IVariableV1 @ref, T value,
21+
public ITensorOrOperation assign_add<T>(IVariableV1 @ref, T value,
2222
bool use_locking = false, string name = null)
2323
=> state_ops.assign_add(@ref, value, use_locking: use_locking, name: name);
2424
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public partial class tensorflow
2626

2727
public class train_internal
2828
{
29-
public RefVariable create_global_step(Graph graph)
29+
public IVariableV1 create_global_step(Graph graph)
3030
=> TrainingUtil.create_global_step(graph);
3131

32-
public RefVariable get_global_step(Graph graph)
32+
public IVariableV1 get_global_step(Graph graph)
3333
=> TrainingUtil.get_global_step(graph);
3434

3535
public Optimizer GradientDescentOptimizer(float learning_rate)

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,6 @@ public Operation global_variables_initializer()
5050
public IVariableV1[] trainable_variables(string scope = null)
5151
=> (variables.trainable_variables() as List<IVariableV1>).ToArray();
5252

53-
public RefVariable get_variable(string name,
54-
TensorShape shape = null,
55-
TF_DataType dtype = TF_DataType.DtInvalid,
56-
object initializer = null, // IInitializer or Tensor
57-
bool? trainable = null,
58-
List<string> collections = null,
59-
bool? use_resource = null,
60-
bool validate_shape = true,
61-
VariableSynchronization synchronization = VariableSynchronization.Auto,
62-
VariableAggregation aggregation = VariableAggregation.None)
63-
{
64-
var scope = Tensorflow.variable_scope.get_variable_scope();
65-
var store = Tensorflow.variable_scope._get_default_variable_store();
66-
return scope.get_variable(store,
67-
name,
68-
shape: shape,
69-
dtype: dtype,
70-
use_resource: use_resource,
71-
validate_shape: validate_shape,
72-
initializer: initializer,
73-
trainable: trainable,
74-
collections: collections);
75-
}
76-
7753
public VariableScope get_variable_scope()
7854
=> Tensorflow.variable_scope.get_variable_scope();
7955
}

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public TensorShape GetTensorShape(TF_Output output)
535535
string debugString = string.Empty;
536536
public override string ToString()
537537
{
538-
return $"{graph_key}, ({_handle})";
538+
return $"{graph_key}, 0x{_handle.ToString("x16")}";
539539
/*if (string.IsNullOrEmpty(debugString))
540540
{
541541
int len = 0;

src/TensorFlowNET.Core/Keras/Optimizers/PolynomialDecay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public PolynomialDecay(float initial_learning_rate,
3434
this.name = name;
3535
}
3636

37-
public Tensor __call__(RefVariable step)
37+
public Tensor __call__(IVariableV1 step)
3838
{
3939
return tf_with(ops.name_scope(name ?? "PolynomialDecay"), scope =>
4040
{

src/TensorFlowNET.Core/Layers/Layer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected virtual IVariableV1 add_weight(string name,
161161
initializer: initializer,
162162
trainable: trainable,
163163
getter: (name1, shape1, dtype1, initializer1, trainable1) =>
164-
tf.get_variable(name1,
164+
tf.compat.v1.get_variable(name1,
165165
shape: new TensorShape(shape1),
166166
dtype: dtype1,
167167
initializer: initializer1,

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static Tensor add_n(Tensor[] inputs, string name = null)
6868
return gen_math_ops.add_n(inputs, name: name);
6969
}
7070

71-
public static Tensor cast(RefVariable x, TF_DataType dtype = TF_DataType.DtInvalid, string name = null)
71+
public static Tensor cast(IVariableV1 x, TF_DataType dtype = TF_DataType.DtInvalid, string name = null)
7272
{
7373
var base_type = dtype.as_base_dtype();
7474
if (base_type == x.dtype)
75-
return x;
75+
return x.AsTensor();
7676

7777
return tf_with(ops.name_scope(name, "Cast", new { x }), scope =>
7878
{
@@ -81,7 +81,7 @@ public static Tensor cast(RefVariable x, TF_DataType dtype = TF_DataType.DtInval
8181
if (t_x.dtype.as_base_dtype() != base_type)
8282
t_x = gen_math_ops.cast(t_x, base_type, name: name);
8383

84-
return x;
84+
return x.AsTensor();
8585
});
8686
}
8787

src/TensorFlowNET.Core/Training/AdamOptimizer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override Operation _apply_dense(Tensor grad, RefVariable var)
7979
use_locking: _use_locking).op;
8080
}
8181

82-
private Operation _apply_sparse_shared(Tensor grad, RefVariable var, Tensor indices, Func<RefVariable, Tensor, Tensor, Tensor> scatter_add)
82+
private Operation _apply_sparse_shared(Tensor grad, IVariableV1 var, Tensor indices, Func<IVariableV1, Tensor, Tensor, Tensor> scatter_add)
8383
{
8484
var (beta1_power_v, beta2_power_v) = _get_beta_accumulators();
8585
Tensor beta1_power = math_ops.cast(beta1_power_v, var.dtype.as_base_dtype());
@@ -91,15 +91,15 @@ private Operation _apply_sparse_shared(Tensor grad, RefVariable var, Tensor indi
9191
var lr = (lr_t * math_ops.sqrt(1 - beta2_power) / (1 - beta1_power));
9292
var m = get_slot(var, "m");
9393
var m_scaled_g_values = grad * (1 - beta1_t);
94-
var m_t = state_ops.assign(m, m * beta1_t, use_locking: _use_locking);
94+
var m_t = state_ops.assign(m.AsTensor(), m.AsTensor() * beta1_t, use_locking: _use_locking);
9595
tf_with(ops.control_dependencies(new[] { m_t }), delegate
9696
{
9797
m_t = scatter_add(m, indices, m_scaled_g_values);
9898
});
9999

100100
var v = get_slot(var, "v");
101101
var v_scaled_g_values = (grad * grad) * (1 - beta2_t);
102-
var v_t = state_ops.assign(v, v * beta2_t, use_locking: _use_locking);
102+
var v_t = state_ops.assign(v.AsTensor(), v.AsTensor() * beta2_t, use_locking: _use_locking);
103103
tf_with(ops.control_dependencies(new[] { v_t }), delegate
104104
{
105105
v_t = scatter_add(v, indices, v_scaled_g_values);
@@ -132,8 +132,8 @@ public override Operation _finish(Operation[] update_ops, string name_scope)
132132
{
133133
var (beta1_power, beta2_power) = _get_beta_accumulators();
134134
ops.colocate_with(beta1_power);
135-
var update_beta1 = beta1_power.assign(beta1_power * _beta1_t, use_locking: _use_locking);
136-
var update_beta2 = beta2_power.assign(beta2_power * _beta2_t, use_locking: _use_locking);
135+
var update_beta1 = beta1_power.assign(beta1_power.AsTensor() * _beta1_t, use_locking: _use_locking);
136+
var update_beta2 = beta2_power.assign(beta2_power.AsTensor() * _beta2_t, use_locking: _use_locking);
137137

138138
operations.Add(update_beta1);
139139
operations.Add(update_beta2);
@@ -142,12 +142,12 @@ public override Operation _finish(Operation[] update_ops, string name_scope)
142142
return control_flow_ops.group(operations.ToArray(), name: name_scope);
143143
}
144144

145-
private (RefVariable, RefVariable) _get_beta_accumulators()
145+
private (IVariableV1, IVariableV1) _get_beta_accumulators()
146146
{
147147
ops.init_scope();
148148
var graph = ops.get_default_graph();
149-
return (_get_non_slot_variable("beta1_power", graph: graph) as RefVariable,
150-
_get_non_slot_variable("beta2_power", graph: graph) as RefVariable);
149+
return (_get_non_slot_variable("beta1_power", graph: graph),
150+
_get_non_slot_variable("beta2_power", graph: graph));
151151
}
152152

153153
public override void _prepare()

0 commit comments

Comments
 (0)