Skip to content

Commit 649623b

Browse files
committed
fix name_scope default_name.
1 parent 8dfc3b7 commit 649623b

7 files changed

Lines changed: 43 additions & 31 deletions

File tree

data/linear_regression.zip

3.34 KB
Binary file not shown.

src/TensorFlowNET.Core/Operations/OpDefLibrary.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
193193
}
194194

195195
// Add Op to graph
196-
var op = g.create_op(op_type_name, inputs.ToArray(), output_types.ToArray(),
196+
var op = g.create_op(op_type_name,
197+
inputs.ToArray(),
198+
output_types.ToArray(),
197199
name: scope,
198200
input_types: input_types.ToArray(),
199201
attrs: attr_protos,

src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
6161
bool sharded = false,
6262
int max_to_keep = 5,
6363
float keep_checkpoint_every_n_hours = 10000,
64-
string name = "",
64+
string name = null,
6565
bool restore_sequentially = false,
6666
string filename = "model",
6767
bool build_save = true,

src/TensorFlowNET.Core/Train/Saving/Saver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Saver(RefVariable[] var_list = null,
3737
bool sharded = false,
3838
int max_to_keep = 5,
3939
float keep_checkpoint_every_n_hours = 10000,
40-
string name = "",
40+
string name = null,
4141
bool restore_sequentially = false,
4242
SaverDef saver_def = null,
4343
ISaverBuilder builder = null,

src/TensorFlowNET.Core/ops.name_scope.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public name_scope(string name, string default_name = "", object values = null)
2727

2828
public void __enter__()
2929
{
30-
if (String.IsNullOrEmpty(_name))
31-
{
32-
_name = _default_name;
33-
}
30+
_name = _name == null ? _default_name : _name;
3431

3532
Graph g = null;
3633
if (_values is List<Tensor> values)

test/TensorFlowNET.Examples/LinearRegression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Run()
5757
var grad = tf.train.GradientDescentOptimizer(learning_rate);
5858
var optimizer = grad.minimize(cost);*/
5959

60-
var new_saver = tf.train.import_meta_graph("save_model.meta", import_scope: "import");
60+
var new_saver = tf.train.import_meta_graph("linear_regression.meta");
6161

6262
var X = graph.OperationByName("Placeholder");
6363
var Y = graph.OperationByName("Placeholder_1");

test/TensorFlowNET.Examples/python/linear_regression.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Parameters
1515
learning_rate = 0.01
1616
training_epochs = 1000
17-
display_step = 50
17+
display_step = 10
1818

1919
# Training Data
2020
train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
@@ -23,28 +23,41 @@
2323
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
2424
n_samples = train_X.shape[0]
2525

26-
# tf Graph Input
27-
X = tf.placeholder("float")
28-
Y = tf.placeholder("float")
29-
30-
# Set model weights
31-
W = tf.Variable(rng.randn(), name="weight")
32-
b = tf.Variable(rng.randn(), name="bias")
33-
34-
# Construct a linear model
35-
mul = tf.multiply(X, W)
36-
pred = tf.add(mul, b)
37-
38-
# Mean squared error
39-
sub = pred-Y
40-
pow = tf.pow(sub, 2)
41-
42-
reduce = tf.reduce_sum(pow)
43-
cost = reduce/(2*n_samples)
44-
# Gradient descent
45-
# Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
46-
grad = tf.train.GradientDescentOptimizer(learning_rate)
47-
optimizer = grad.minimize(cost)
26+
if False:
27+
# tf Graph Input
28+
X = tf.placeholder("float")
29+
Y = tf.placeholder("float")
30+
31+
# Set model weights
32+
W = tf.Variable(-0.06, name="weight")
33+
b = tf.Variable(-0.73, name="bias")
34+
35+
# Construct a linear model
36+
mul = tf.multiply(X, W)
37+
pred = tf.add(mul, b)
38+
39+
# Mean squared error
40+
sub = pred-Y
41+
pow = tf.pow(sub, 2)
42+
43+
reduce = tf.reduce_sum(pow)
44+
cost = reduce/(2*n_samples)
45+
# Gradient descent
46+
# Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
47+
grad = tf.train.GradientDescentOptimizer(learning_rate)
48+
optimizer = grad.minimize(cost)
49+
# tf.train.export_meta_graph(filename='save_model.meta');
50+
else:
51+
# tf Graph Input
52+
new_saver = tf.train.import_meta_graph("save_model.meta")
53+
nodes = tf.get_default_graph()._nodes_by_name;
54+
optimizer = nodes["GradientDescent"]
55+
cost = nodes["truediv"].outputs[0]
56+
X = nodes["Placeholder"].outputs[0]
57+
Y = nodes["Placeholder_1"].outputs[0]
58+
W = nodes["weight"].outputs[0]
59+
b = nodes["bias"].outputs[0]
60+
pred = nodes["Add"].outputs[0]
4861

4962
# Initialize the variables (i.e. assign their default value)
5063
init = tf.global_variables_initializer()

0 commit comments

Comments
 (0)