Skip to content

Commit 11572bf

Browse files
committed
bunch of updates.
1 parent fe06a29 commit 11572bf

18 files changed

Lines changed: 405 additions & 93 deletions

File tree

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 static partial class tf
8+
{
9+
/// <summary>
10+
/// Outputs random values from a normal distribution.
11+
/// </summary>
12+
/// <param name="shape"></param>
13+
/// <param name="mean"></param>
14+
/// <param name="stddev"></param>
15+
/// <param name="dtype"></param>
16+
/// <param name="seed"></param>
17+
/// <param name="name"></param>
18+
/// <returns></returns>
19+
public static Tensor random_normal(int[] shape,
20+
float mean = 0.0f,
21+
float stddev = 1.0f,
22+
TF_DataType dtype = TF_DataType.TF_FLOAT,
23+
int? seed = null,
24+
string name = "") => random_ops.random_normal(shape, mean, stddev, dtype, seed, name);
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public class random_seed
8+
{
9+
public static (int?, int?) get_seed(int? op_seed = null)
10+
{
11+
return (null, null);
12+
}
13+
}
14+
}

‎src/TensorFlowNET.Core/Graphs/Graph.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[]
141141
}
142142

143143
if (String.IsNullOrEmpty(name))
144-
{
145144
name = op_type;
146-
}
147-
145+
// If a names ends with a '/' it is a "name scope" and we use it as-is,
146+
// after removing the trailing '/'.
148147
name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name);
149148
var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);
150149

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OpDefLibrary
1414
{
1515
public Operation _apply_op_helper(string op_type_name, string name = "", dynamic args = null)
1616
{
17-
var keywords = ConvertToDict(args);
17+
Dictionary<string, object> keywords = ConvertToDict(args);
1818
var g = ops.get_default_graph();
1919
var op_def = g.GetOpDef(op_type_name);
2020

@@ -42,7 +42,8 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
4242
var attrs = new Dictionary<string, object>();
4343
var inputs = new List<Tensor>();
4444
var input_types = new List<TF_DataType>();
45-
45+
dynamic values = null;
46+
4647
return Python.with<ops.name_scope, Operation>(new ops.name_scope(name), scope =>
4748
{
4849
var inferred_from = new Dictionary<string, object>();
@@ -53,7 +54,17 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
5354
foreach (var input_arg in op_def.InputArg)
5455
{
5556
var input_name = input_arg.Name;
56-
var values = keywords[input_name];
57+
58+
if (keywords.ContainsKey(input_name))
59+
values = keywords[input_name];
60+
else if (keywords.ContainsKey(input_name + "_"))
61+
{
62+
input_name += "_";
63+
values = keywords[input_name];
64+
}
65+
else
66+
throw new TypeError("No argument for input " + input_name);
67+
5768
// Goals:
5869
// * Convert values to Tensors if it contains constants.
5970
// * Verify that values is a list if that matches the input_arg's
@@ -92,8 +103,8 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
92103

93104
values = ops.internal_convert_n_to_tensor(values,
94105
name: input_arg.Name,
95-
dtype: dtype,
96-
preferred_dtype: default_dtype,
106+
dtype: dtype.as_tf_dtype(),
107+
preferred_dtype: default_dtype.as_tf_dtype(),
97108
as_ref: input_arg.IsRef);
98109
}
99110
else
@@ -107,9 +118,9 @@ public Operation _apply_op_helper(string op_type_name, string name = "", dynamic
107118

108119
values = ops.internal_convert_to_tensor(values,
109120
name: input_name,
110-
dtype: dtype,
121+
dtype: dtype.as_tf_dtype(),
111122
as_ref: input_arg.IsRef,
112-
preferred_dtype: default_dtype);
123+
preferred_dtype: default_dtype.as_tf_dtype());
113124

114125
//if (!String.IsNullOrEmpty(input_arg.TypeAttr))
115126
//attrs[input_arg.TypeAttr] = values.dtype;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Tensorflow
77
{
88
public class array_ops
99
{
10+
public static Tensor placeholder_with_default<T>(T input, int[] shape, string name = "") => gen_array_ops.placeholder_with_default(input, shape, name);
11+
1012
public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = "")
1113
{
1214
dtype = dtype.as_base_dtype();
@@ -35,13 +37,13 @@ private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dt
3537
var nd = np.zeros<T>(shape);
3638
if (shape.Size < 1000)
3739
{
38-
return constant_op.constant(nd, name);
40+
return constant_op.constant(nd, name: name);
3941
}
4042
else
4143
{
4244
tShape = constant_op._tensor_shape_tensor_conversion_function(shape.as_shape());
4345
var c = constant_op.constant(0);
44-
return gen_array_ops.fill(tShape, c, name);
46+
return gen_array_ops.fill(tShape, c, name: name);
4547
}
4648
}
4749

@@ -99,7 +101,7 @@ private static Tensor shape_internal(Tensor input, string name = "", bool optimi
99101
if (optimize && input_shape.is_fully_defined())
100102
{
101103
var nd = np.array(input_tensor.shape, out_type.as_numpy_datatype());
102-
return constant_op.constant(nd, name);
104+
return constant_op.constant(nd, name: name);
103105
}
104106
}
105107

@@ -122,7 +124,7 @@ private static Tensor size_internal(Tensor input, string name = "", bool optimiz
122124
if (input_shape.is_fully_defined())
123125
{
124126
var nd = np.array(input_tensor.shape, out_type.as_numpy_datatype());
125-
return constant_op.constant(nd, name);
127+
return constant_op.constant(nd, name: name);
126128
}
127129
}
128130

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static Tensor where()
113113
/// <param name="shape"></param>
114114
/// <param name="name"></param>
115115
/// <returns></returns>
116-
public static Tensor placeholder_with_default<T>(T input, TensorShape shape, string name = "")
116+
public static Tensor placeholder_with_default<T>(T input, int[] shape, string name = "")
117117
{
118118
var _op = _op_def_lib._apply_op_helper("PlaceholderWithDefault", name, new { input, shape, name });
119119
return _op.outputs[0];
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public class gen_random_ops
8+
{
9+
public static OpDefLibrary _op_def_lib = new OpDefLibrary();
10+
11+
/// <summary>
12+
/// Outputs random values from a normal distribution.
13+
/// </summary>
14+
/// <param name="shape"></param>
15+
/// <param name="dtype"></param>
16+
/// <param name="seed"></param>
17+
/// <param name="seed2"></param>
18+
/// <param name="name"></param>
19+
/// <returns></returns>
20+
public static Tensor random_standard_normal(Tensor shape, TF_DataType dtype = TF_DataType.DtInvalid, int? seed = null, int? seed2 = null, string name = "")
21+
{
22+
if (!seed.HasValue)
23+
seed = 0;
24+
if (!seed2.HasValue)
25+
seed2 = 0;
26+
27+
var _op = _op_def_lib._apply_op_helper("RandomStandardNormal", name: name,
28+
args: new { shape, dtype, seed, seed2 });
29+
30+
return _op.outputs[0];
31+
}
32+
}
33+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Tensorflow
66
{
77
public class math_ops
88
{
9+
public static Tensor add(Tensor x, Tensor y, string name = "") => gen_math_ops.add(x, y, name);
10+
911
public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, string name = "")
1012
{
1113
var base_type = dtype.as_base_dtype();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public class random_ops
8+
{
9+
public static Tensor random_normal(int[] shape,
10+
float mean = 0.0f,
11+
float stddev = 1.0f,
12+
TF_DataType dtype = TF_DataType.TF_FLOAT,
13+
int? seed = null,
14+
string name = "")
15+
{
16+
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "random_normal", new object[] { shape, mean, stddev }), scope =>
17+
{
18+
var shape_tensor = _ShapeTensor(shape);
19+
var mean_tensor = ops.convert_to_tensor(mean, dtype: dtype, name: "mean");
20+
var stddev_tensor = ops.convert_to_tensor(stddev, dtype: dtype, name = "stddev");
21+
var (seed1, seed2) = random_seed.get_seed(seed);
22+
var rnd = gen_random_ops.random_standard_normal(shape_tensor, dtype: dtype, seed: seed1, seed2: seed2);
23+
var mul = rnd * stddev_tensor;
24+
var value = math_ops.add(mul, mean_tensor, name: name);
25+
return value;
26+
});
27+
}
28+
29+
private static Tensor _ShapeTensor(int[] shape)
30+
{
31+
return ops.convert_to_tensor(shape, name: "shape");
32+
}
33+
}
34+
}
35+

‎src/TensorFlowNET.Core/Sessions/BaseSession.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,19 @@ private NDArray _run(object fetches, FeedItem[] feed_dict = null)
5555
var subfeed_dtype = subfeed_t.dtype.as_numpy_datatype();
5656
switch(subfeed.Value)
5757
{
58+
case float floatVal:
59+
feed_dict_tensor[subfeed_t] = (NDArray)floatVal;
60+
break;
61+
case int intVal:
62+
feed_dict_tensor[subfeed_t] = (NDArray)intVal;
63+
break;
5864
case string str:
59-
feed_dict_tensor[subfeed_t] = np.array(str);
60-
feed_map[subfeed_t.name] = new Tuple<object, object>(subfeed_t, subfeed.Value);
65+
feed_dict_tensor[subfeed_t] = (NDArray)str;
6166
break;
67+
default:
68+
throw new NotImplementedException("_run subfeed");
6269
}
70+
feed_map[subfeed_t.name] = new Tuple<object, object>(subfeed_t, subfeed.Value);
6371
}
6472
}
6573

0 commit comments

Comments
 (0)