Skip to content

Commit 5720dfd

Browse files
committed
fix _constant_if_small for zeros.
1 parent 11572bf commit 5720dfd

9 files changed

Lines changed: 38 additions & 49 deletions

File tree

‎src/TensorFlowNET.Core/Operations/OpDefLibrary.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ private AttrValue SetAttrValue(OpDef op_def, AttrDef attr_def, object value)
322322
attr_value.Shape = val1.as_proto();
323323
else if(value is long[] val2)
324324
attr_value.Shape = tensor_util.as_shape(val2);
325+
else if (value is int[] val3)
326+
attr_value.Shape = tensor_util.as_shape(val3);
325327

326328
break;
327329
default:

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT
3434
private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dtype, string name)
3535
{
3636
Tensor tShape = null;
37-
var nd = np.zeros<T>(shape);
3837
if (shape.Size < 1000)
3938
{
40-
return constant_op.constant(nd, name: name);
39+
return constant_op.constant(value, shape: shape, dtype: dtype, name: name);
4140
}
4241
else
4342
{

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TensorFlow 1.13 RC.</PackageReleaseNotes>
4343

4444
<ItemGroup>
4545
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
46-
<PackageReference Include="NumSharp" Version="0.7.0" />
46+
<PackageReference Include="NumSharp" Version="0.7.1" />
4747
</ItemGroup>
4848

4949
<ItemGroup>

‎src/TensorFlowNET.Core/Tensors/tensor_util.cs‎

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
5656

5757
switch (values)
5858
{
59-
/*case bool boolVal:
59+
case bool boolVal:
6060
nparray = boolVal;
61-
break;*/
61+
break;
6262
case int intVal:
6363
nparray = intVal;
6464
break;
@@ -74,6 +74,9 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
7474
case string strVal:
7575
nparray = strVal;
7676
break;
77+
case string[] strVals:
78+
nparray = strVals;
79+
break;
7780
default:
7881
throw new Exception("make_tensor_proto Not Implemented");
7982
}
@@ -100,7 +103,8 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
100103
}
101104
else
102105
{
103-
throw new NotImplementedException("make_tensor_proto shape not implemented");
106+
shape_size = new TensorShape(shape).Size;
107+
is_same_size = shape_size == nparray.size;
104108
}
105109

106110
var tensor_proto = new tensor_pb2.TensorProto
@@ -111,41 +115,17 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
111115

112116
if (is_same_size && _TENSOR_CONTENT_TYPES.Contains(numpy_dtype) && shape_size > 1)
113117
{
114-
var bytes = new List<byte>();
115-
var nd2 = nparray.ravel();
116-
switch (nparray.dtype.Name)
117-
{
118-
case "Int32":
119-
nd2.Data<int>().Select(x =>
120-
{
121-
bytes.AddRange(BitConverter.GetBytes(x));
122-
return x;
123-
}).ToArray();
124-
break;
125-
case "Single":
126-
nd2.Data<float>().Select(x =>
127-
{
128-
bytes.AddRange(BitConverter.GetBytes(x));
129-
return x;
130-
}).ToArray();
131-
break;
132-
case "Double":
133-
nd2.Data<double>().Select(x =>
134-
{
135-
bytes.AddRange(BitConverter.GetBytes(x));
136-
return x;
137-
}).ToArray();
138-
break;
139-
default:
140-
throw new Exception("make_tensor_proto Not Implemented");
141-
}
118+
byte[] bytes = nparray.ToByteArray();
142119
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes.ToArray());
143120
return tensor_proto;
144121
}
145122

146-
if (numpy_dtype == TF_DataType.TF_STRING && !(values is NDArray) && values is string str)
123+
if (numpy_dtype == TF_DataType.TF_STRING && !(values is NDArray))
147124
{
148-
tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFromUtf8(str));
125+
if (values is string str)
126+
tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFromUtf8(str));
127+
else if (values is string[] str_values)
128+
tensor_proto.StringVal.AddRange(str_values.Select(x => Google.Protobuf.ByteString.CopyFromUtf8(x)));
149129
return tensor_proto;
150130
}
151131

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
123123
Version = _write_version
124124
};
125125
});
126-
127-
128126
}
129127

130128
public Tensor _AddSaveOps(Tensor filename_tensor, SaveableObject[] saveables)

‎test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="NumSharp" Version="0.7.0" />
9+
<PackageReference Include="NumSharp" Version="0.7.1" />
1010
<PackageReference Include="TensorFlow.NET" Version="0.1.0" />
1111
</ItemGroup>
1212

‎test/TensorFlowNET.UnitTest/ConstantTest.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void ZerosConst()
5858

5959
var data = result.Data<int>();
6060
Assert.AreEqual(0, data[0]);
61+
Assert.AreEqual(0, data[500]);
6162
Assert.AreEqual(0, data[result.size - 1]);
6263
});
6364
}
@@ -109,5 +110,15 @@ public void StringEncode()
109110

110111
//c_api.TF_StringDecode(str, (ulong)str.Length, IntPtr.Zero, ref dst_len, status);
111112
}
113+
114+
/// <summary>
115+
/// tensorflow\c\c_api_test.cc
116+
/// TestEncodeDecode
117+
/// </summary>
118+
[TestMethod]
119+
public void EncodeDecode()
120+
{
121+
122+
}
112123
}
113124
}

‎test/TensorFlowNET.UnitTest/TensorFlowNET.UnitTest.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
2020
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
2121
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
22-
<PackageReference Include="NumSharp" Version="0.7.0" />
22+
<PackageReference Include="NumSharp" Version="0.7.1" />
2323
<PackageReference Include="TensorFlow.NET" Version="0.1.0" />
2424
</ItemGroup>
2525

‎test/TensorFlowNET.UnitTest/TrainSaverTest.cs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,25 @@ namespace TensorFlowNET.UnitTest
1010
public class TrainSaverTest : Python
1111
{
1212
[TestMethod]
13-
public void WriteGraph()
13+
public void ExportGraph()
1414
{
1515
var v = tf.Variable(0, name: "my_variable");
1616
var sess = tf.Session();
17-
tf.train.write_graph(sess.graph, "/tmp/my-model", "train.pbtxt");
17+
tf.train.write_graph(sess.graph, "/tmp/my-model", "train1.pbtxt");
1818
}
1919

2020
[TestMethod]
2121
public void ImportGraph()
2222
{
2323
var v = tf.Variable(0, name: "my_variable");
2424
var sess = tf.Session();
25-
tf.train.write_graph(sess.graph, "/tmp/my-model", "train.pbtxt");
25+
tf.train.write_graph(sess.graph, "/tmp/my-model", "train2.pbtxt");
2626
}
2727

2828
[TestMethod]
29-
public void SaveSimple()
29+
public void Save1()
3030
{
31-
var w1 = tf.Variable(tf.random_normal(new int[] { 2 }), name: "w1");
32-
var w2 = tf.Variable(tf.random_normal(new int[] { 5 }), name: "w2");
31+
var w1 = tf.Variable(0, name: "save1");
3332

3433
var init_op = tf.global_variables_initializer();
3534

@@ -41,13 +40,13 @@ public void SaveSimple()
4140
sess.run(init_op);
4241

4342
// Save the variables to disk.
44-
var save_path = saver.save(sess, "/tmp/model.ckpt");
43+
var save_path = saver.save(sess, "/tmp/model1.ckpt");
4544
Console.WriteLine($"Model saved in path: {save_path}");
4645
});
4746
}
4847

4948
[TestMethod]
50-
public void Save()
49+
public void Save2()
5150
{
5251
var v1 = tf.get_variable("v1", shape: new TensorShape(3), initializer: tf.zeros_initializer);
5352
var v2 = tf.get_variable("v2", shape: new TensorShape(5), initializer: tf.zeros_initializer);
@@ -69,7 +68,7 @@ public void Save()
6968
dec_v2.op.run();
7069

7170
// Save the variables to disk.
72-
var save_path = saver.save(sess, "/tmp/model.ckpt");
71+
var save_path = saver.save(sess, "/tmp/model2.ckpt");
7372
Console.WriteLine($"Model saved in path: {save_path}");
7473
});
7574
}

0 commit comments

Comments
 (0)