Skip to content

Commit 01f5ab9

Browse files
committed
tf.zeros
1 parent 3f3a805 commit 01f5ab9

18 files changed

Lines changed: 169 additions & 24 deletions

File tree

‎docs/source/Constant.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,14 @@ var tensor = tf.constant(nd);
4747

4848
Now let's explore how constant works.
4949

50-
现在让我探究一下`tf.constant`是怎么工作的。
50+
现在让我探究一下`tf.constant`是怎么工作的。
51+
52+
53+
54+
##### Other functions to create a Constant
55+
56+
* tf.zeros
57+
* tf.zeros_like
58+
* tf.ones
59+
* tf.ones_like
60+
* tf.fill

‎src/TensorFlowNET.Core/APIs/tf.math.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public static Tensor reduce_sum(Tensor input, int[] axis = null)
3636
{
3737
Tensor rank;
3838
string name;
39-
using (var namescop = new ops.name_scope<Tensor>("", "Rank", new List<Tensor> { input }))
39+
using (var namescop = new ops.name_scope("", "Rank", new List<Tensor> { input }))
4040
{
4141
name = namescop;
4242
rank = gen_array_ops.rank(input, namescop);
4343
}
4444

45-
using (var namescope = new ops.name_scope<Tensor>("range", "Range", new List<Tensor> { 0D, input, 1D }))
45+
using (var namescope = new ops.name_scope("range", "Range", new List<Tensor> { 0D, input, 1D }))
4646
{
4747
name = namescope;
4848
var start = ops.convert_to_tensor(0D);

‎src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void _GradientsHelper(object ys,
5050
all.AddRange(grad_ys1);
5151

5252
string grad_scope = "";
53-
using (var namescope = new ops.name_scope<Tensor>(name, "gradients", values: all))
53+
using (var namescope = new ops.name_scope(name, "gradients", values: all))
5454
{
5555
grad_scope = namescope;
5656
// Get a uid for this call to gradients that can be used to help

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Operation _apply_op_helper(string op_type_name, string name = "", Diction
4141
var input_types = new List<TF_DataType>();
4242

4343
string scope = "";
44-
using (var namescope = new ops.name_scope<object>(name))
44+
using (var namescope = new ops.name_scope(name))
4545
{
4646
scope = namescope;
4747

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Tensorflow
7+
{
8+
public class array_ops
9+
{
10+
public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = "")
11+
{
12+
Tensor output = null;
13+
14+
dtype = dtype.as_base_dtype();
15+
Python.with(new ops.name_scope(name, "zeros", shape), self =>
16+
{
17+
name = self as ops.name_scope;
18+
switch (dtype)
19+
{
20+
case TF_DataType.TF_BOOL:
21+
output = _constant_if_small(false, shape, dtype, name);
22+
break;
23+
case TF_DataType.TF_DOUBLE:
24+
output = _constant_if_small(0.0D, shape, dtype, name);
25+
break;
26+
case TF_DataType.TF_FLOAT:
27+
output = _constant_if_small(0.0F, shape, dtype, name);
28+
break;
29+
case TF_DataType.TF_INT32:
30+
output = _constant_if_small(0, shape, dtype, name);
31+
break;
32+
default:
33+
break;
34+
}
35+
});
36+
37+
return output;
38+
}
39+
40+
private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dtype, string name)
41+
{
42+
Tensor tShape = null;
43+
var nd = np.zeros<T>(shape);
44+
if (shape.Size < 1000)
45+
{
46+
return constant_op.Constant(nd, name);
47+
}
48+
else
49+
{
50+
tShape = constant_op._tensor_shape_tensor_conversion_function(shape.as_shape(), dtype, name);
51+
var c = constant_op.Constant(nd, name);
52+
return gen_array_ops.fill<T>(shape, value, name);
53+
}
54+
}
55+
}
56+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class control_flow_ops
99
{
1010
public static Operation group(List<Operation> inputs, string name = "")
1111
{
12-
using(var namescope = new ops.name_scope<Operation>(name, "group_deps", inputs))
12+
using(var namescope = new ops.name_scope(name, "group_deps", inputs))
1313
{
1414
name = namescope;
1515

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,24 @@ public static Tensor rank(Tensor input, string name = "")
5555

5656
return _op.outputs[0];
5757
}
58+
59+
/// <summary>
60+
/// Creates a tensor filled with a scalar value.
61+
/// </summary>
62+
/// <typeparam name="T"></typeparam>
63+
/// <param name="dims"></param>
64+
/// <param name="value"></param>
65+
/// <param name="name"></param>
66+
/// <returns></returns>
67+
public static Tensor fill<T>(int[] dims, T value, string name = "")
68+
{
69+
var keywords = new Dictionary<string, object>();
70+
keywords.Add("dims", dims);
71+
keywords.Add("value", value);
72+
73+
var _op = _op_def_lib._apply_op_helper("Fill", name);
74+
75+
return _op.outputs[0];
76+
}
5877
}
5978
}

‎src/TensorFlowNET.Core/Python.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ protected void print(object obj)
1414
Console.WriteLine(obj.ToString());
1515
}
1616

17-
public static void with(IPython py, Action action)
17+
public static void with(IPython py, Action<IPython> action)
1818
{
1919
try
2020
{
2121
py.__enter__();
22-
action();
22+
action(py);
2323
}
2424
catch (Exception ex)
2525
{

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

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

3838
<ItemGroup>
3939
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
40-
<PackageReference Include="NumSharp" Version="0.6.6" />
40+
<PackageReference Include="NumSharp" Version="0.7.0" />
4141
</ItemGroup>
4242

4343
<ItemGroup>

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,37 @@ public static Tensor Constant(NDArray nd, string name = "Const", bool verify_sha
4646

4747
return op.outputs[0];
4848
}
49+
50+
/// <summary>
51+
/// Function to convert TensorShape to Tensor.
52+
/// </summary>
53+
/// <param name="s"></param>
54+
/// <param name="dtype"></param>
55+
/// <param name="name"></param>
56+
/// <param name="as_ref"></param>
57+
/// <returns></returns>
58+
public static Tensor _tensor_shape_tensor_conversion_function(TensorShape s, TF_DataType dtype = TF_DataType.DtInvalid, string name = "", bool as_ref = false)
59+
{
60+
var s_list = s.Dimensions;
61+
var int64_value = 0;
62+
foreach(var dim in s_list)
63+
{
64+
if (dim > Math.Pow(2, 31))
65+
{
66+
int64_value = dim;
67+
break;
68+
}
69+
}
70+
71+
if(int64_value > 0)
72+
{
73+
dtype = TF_DataType.TF_INT32;
74+
}
75+
76+
if (string.IsNullOrEmpty(name))
77+
name = "shape_as_tensor";
78+
79+
return constant_op.Constant(s_list, name);
80+
}
4981
}
5082
}

0 commit comments

Comments
 (0)