Skip to content

Commit d3e2124

Browse files
committed
fix ndarray index.
1 parent 083c8e8 commit d3e2124

9 files changed

Lines changed: 97 additions & 80 deletions

File tree

src/TensorFlowNET.Core/NumPy/NDArray.Index.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public NDArray this[params int[] index]
2525
{
2626
get
2727
{
28-
return _tensor[index.Select(x => new Slice(x, x + 1)).ToArray()];
28+
return _tensor[index.Select(x => new Slice(x, x + 1)).ToArray()];
2929
}
3030

3131
set

src/TensorFlowNET.Core/Operations/Initializers/Constant.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ public Tensor Apply(InitializerArgs args)
3434
if (args.DType == TF_DataType.DtInvalid)
3535
args.DType = this.dtype;
3636

37-
if (!args.VerifyShape.HasValue)
38-
args.VerifyShape = _verify_shape;
37+
args.VerifyShape = _verify_shape;
3938

40-
return constant_op._constant_impl(value, args.DType, args.Shape,
39+
return constant_op.constant(value, args.DType, args.Shape,
4140
name: "Const",
42-
verify_shape: args.VerifyShape.Value,
41+
verify_shape: args.VerifyShape,
4342
allow_broadcast: false);
4443
}
4544
}

src/TensorFlowNET.Core/Operations/Initializers/InitializerArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ public class InitializerArgs
55
public string Name { get; set; }
66
public TensorShape Shape { get; set; }
77
public TF_DataType DType { get; set; }
8-
public bool? VerifyShape { get; set; } = null;
8+
public bool VerifyShape { get; set; }
99

1010
public InitializerArgs(TensorShape shape,
1111
TF_DataType dtype = TF_DataType.DtInvalid,
12-
bool? verify_shape = null,
12+
bool verify_shape = false,
1313
string name = null)
1414
{
1515
Shape = shape;

src/TensorFlowNET.Core/Operations/array_ops.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ internal static Tensor constant(object value,
6464
TF_DataType dtype = TF_DataType.DtInvalid,
6565
int[] shape = null,
6666
string name = "Const",
67-
bool verify_shape = false) => constant_op._constant_impl(value,
68-
dtype,
69-
shape,
70-
name,
67+
bool verify_shape = false) => constant_op.constant(value,
68+
dtype: dtype,
69+
shape: shape,
70+
name: name,
7171
verify_shape: verify_shape,
7272
allow_broadcast: false);
7373

src/TensorFlowNET.Core/Tensors/constant_op.cs

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,68 +37,14 @@ public class constant_op
3737
/// <param name="shape">Optional dimensions of resulting tensor.</param>
3838
/// <param name="name">Optional name for the tensor.</param>
3939
/// <returns></returns>
40-
public static Tensor constant(object value, TF_DataType dtype = TF_DataType.DtInvalid, int[] shape = null, string name = "Const")
40+
public static Tensor constant(object value, TF_DataType dtype = TF_DataType.DtInvalid,
41+
int[] shape = null, bool verify_shape = false,
42+
bool allow_broadcast = true, string name = "Const")
4143
{
42-
return _constant_impl(value, dtype, shape, name, verify_shape: false, allow_broadcast: true);
43-
}
44-
45-
/// <param name="verify_shape">Boolean that enables verification of a shape of values.</param>
46-
public static Tensor _constant_impl(object value,
47-
TF_DataType dtype,
48-
TensorShape shape,
49-
string name,
50-
bool verify_shape,
51-
bool allow_broadcast)
52-
{
53-
if (tf.Context.executing_eagerly())
54-
{
55-
var t = convert_to_eager_tensor(value, tf.Context, dtype: dtype);
56-
if (shape == null)
57-
return t;
58-
59-
if (t.shape.Select(x => Convert.ToInt64(x)).SequenceEqual(shape.dims))
60-
return t;
61-
62-
if (verify_shape)
63-
throw new TypeError($"Expected Tensor's shape: {shape}, got {t.shape}.");
64-
65-
var num_t = t.TensorShape.num_elements();
66-
if (num_t == shape.num_elements())
67-
return _eager_reshape(t, shape, tf.Context);
68-
if (num_t == 1)
69-
{
70-
if (t.dtype == dtypes.@bool)
71-
throw new NotImplementedException("");
72-
else
73-
return _eager_fill(shape, t, tf.Context);
74-
}
75-
}
76-
77-
// graph mode
78-
Graph g = ops.get_default_graph();
79-
var tensor_value = new AttrValue();
80-
tensor_value.Tensor = tensor_util.make_tensor_proto(value,
81-
dtype: dtype,
82-
shape: shape,
83-
verify_shape: verify_shape,
84-
allow_broadcast: allow_broadcast);
85-
86-
var dtype_value = new AttrValue
87-
{
88-
Type = tensor_value.Tensor.Dtype,
89-
};
90-
91-
var attrs = new Dictionary<string, AttrValue>();
92-
attrs["value"] = tensor_value;
93-
attrs["dtype"] = dtype_value;
94-
95-
var op = g.create_op("Const",
96-
new Tensor[0],
97-
new TF_DataType[] { dtype_value.Type.as_tf_dtype() },
98-
attrs: attrs,
99-
name: name);
100-
101-
return op.outputs[0];
44+
if(tf.executing_eagerly())
45+
return convert_to_eager_tensor(value, dtype, shape, name, verify_shape: verify_shape, allow_broadcast: allow_broadcast);
46+
else
47+
return convert_to_graph_tensor(value, dtype, shape, name, verify_shape: verify_shape, allow_broadcast: allow_broadcast);
10248
}
10349

10450
private static Tensor _eager_reshape(Tensor tensor, int[] shape, Context ctx)
@@ -189,6 +135,70 @@ value is NDArray nd &&
189135
}
190136
}
191137

138+
static Tensor convert_to_eager_tensor(object value,
139+
TF_DataType dtype,
140+
TensorShape shape,
141+
string name,
142+
bool verify_shape,
143+
bool allow_broadcast)
144+
{
145+
var t = convert_to_eager_tensor(value, tf.Context, dtype: dtype);
146+
if (shape == null)
147+
return t;
148+
149+
if (t.shape.Select(x => Convert.ToInt64(x)).SequenceEqual(shape.dims))
150+
return t;
151+
152+
if (verify_shape)
153+
throw new TypeError($"Expected Tensor's shape: {shape}, got {t.shape}.");
154+
155+
var num_t = t.TensorShape.num_elements();
156+
if (num_t == shape.num_elements())
157+
return _eager_reshape(t, shape, tf.Context);
158+
if (num_t == 1)
159+
{
160+
if (t.dtype == dtypes.@bool)
161+
throw new NotImplementedException("");
162+
else
163+
return _eager_fill(shape, t, tf.Context);
164+
}
165+
166+
throw new NotImplementedException("");
167+
}
168+
169+
static Tensor convert_to_graph_tensor(object value,
170+
TF_DataType dtype,
171+
TensorShape shape,
172+
string name,
173+
bool verify_shape,
174+
bool allow_broadcast)
175+
{
176+
Graph g = ops.get_default_graph();
177+
var tensor_value = new AttrValue();
178+
tensor_value.Tensor = tensor_util.make_tensor_proto(value,
179+
dtype: dtype,
180+
shape: shape,
181+
verify_shape: verify_shape,
182+
allow_broadcast: allow_broadcast);
183+
184+
var dtype_value = new AttrValue
185+
{
186+
Type = tensor_value.Tensor.Dtype,
187+
};
188+
189+
var attrs = new Dictionary<string, AttrValue>();
190+
attrs["value"] = tensor_value;
191+
attrs["dtype"] = dtype_value;
192+
193+
var op = g.create_op("Const",
194+
new Tensor[0],
195+
new TF_DataType[] { dtype_value.Type.as_tf_dtype() },
196+
attrs: attrs,
197+
name: name);
198+
199+
return op.outputs[0];
200+
}
201+
192202
/// <summary>
193203
/// Function to convert TensorShape to Tensor.
194204
/// </summary>

src/TensorFlowNET.Core/Tensors/tensor_util.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ public static TensorProto make_tensor_proto(object values, TF_DataType dtype = T
125125
byte[] bytes = nd.ToByteArray();
126126
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
127127
}
128+
else if (values is Tensor tensor && tensor.IsReferencedByNDArray)
129+
{
130+
var len = tensor.itemsize * tensor.size;
131+
byte[] bytes = tensor.BufferToArray();
132+
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(bytes);
133+
}
128134
else if (!values.GetType().IsArray)
129135
{
130136
switch (values)

src/TensorFlowNET.Core/Tensors/tf.constant.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public Tensor constant(object value,
3030
TF_DataType dtype = TF_DataType.DtInvalid,
3131
TensorShape shape = null,
3232
string name = "Const")
33-
=> constant_op._constant_impl(value,
34-
dtype,
35-
shape,
36-
name,
33+
=> constant_op.constant(value,
34+
dtype: dtype,
35+
shape: shape,
36+
name: name,
3737
verify_shape: false,
3838
allow_broadcast: true);
3939

src/TensorFlowNET.Core/ops.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ public static Tensor convert_to_tensor(object value,
145145
}
146146
else if (value is Tensor tensor && tensor.IsReferencedByNDArray)
147147
{
148-
return tensor;
148+
if (tf.executing_eagerly())
149+
return tensor;
150+
else
151+
return constant_op.constant(tensor);
149152
}
150153

151154
// graph mode

test/TensorFlowNET.Graph.UnitTest/ImageTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ public void TestCropAndResize()
8282

8383
var result = sess.run(cropped);
8484
// check if cropped to 1x1 center was succesfull
85-
Assert.AreEqual(result.size, 1);
85+
Assert.AreEqual(result.size, 1ul);
8686
Assert.AreEqual(result[0, 0, 0, 0], 4f);
8787

8888
cropped = tf.image.crop_and_resize(image2, box, boxInd, cropSize2_2);
8989
result = sess.run(cropped);
9090
// check if flipped and no cropping occured
91-
Assert.AreEqual(result.size, 16);
91+
Assert.AreEqual(result.size, 16ul);
9292
Assert.AreEqual(result[0, 0, 0, 0], 12f);
93-
9493
}
9594
}
9695
}

0 commit comments

Comments
 (0)