Skip to content

Commit bcb28f3

Browse files
committed
fix keras reshape layer.
1 parent cd2f0c0 commit bcb28f3

11 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,10 @@ public static TF_DataType GetDataType(this object data)
540540
return nd.dtype;
541541
case Tensor tensor:
542542
return tensor.dtype;
543-
case Tensor[] tensor:
544-
return tensor[0].dtype;
543+
case Tensors tensors:
544+
return tensors.dtype;
545+
case IEnumerable<Tensor> tensors:
546+
return tensors.First().dtype;
545547
case ResourceVariable variable:
546548
return variable.dtype;
547549
default:

src/TensorFlowNET.Core/Eager/EagerRunner.TFE_Execute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public Tensor[] TFE_ExecuteCancelable(Context ctx,
5151
SafeTensorHandleHandle tensor_handle = inputs[i] switch
5252
{
5353
EagerTensor et => et.EagerTensorHandle,
54-
_ => throw new NotImplementedException("")
54+
Tensor nd => nd.EagerTensorHandle,
55+
_ => throw new NotImplementedException("Eager tensor handle has not been allocated.")
5556
};
5657
c_api.TFE_OpAddInput(op, tensor_handle, status.Handle);
5758
status.Check(true);

src/TensorFlowNET.Core/Operations/array_ops.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ public static Tensor zeros(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOA
186186

187187
private static Tensor _constant_if_small(int value, Tensor shape)
188188
{
189-
return shape < 1000L;
189+
if (shape.dtype == TF_DataType.TF_INT64)
190+
return shape < 1000L;
191+
else
192+
return shape < 1000;
190193
}
191194

192195
private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dtype, string name)

src/TensorFlowNET.Core/Tensors/Tensor.Value.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public unsafe T[] ToArray<T>() where T : unmanaged
1616
{
1717
//Are the types matching?
1818
if (typeof(T).as_tf_dtype() != dtype)
19-
throw new ArrayTypeMismatchException($"dtype {dtype} mismatch.");
19+
throw new ArrayTypeMismatchException($"Required dtype {dtype} mismatch with {typeof(T).as_tf_dtype()}.");
2020

2121
if (ndim == 0 && size == 1) //is it a scalar?
2222
{

src/TensorFlowNET.Core/Tensors/constant_op.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static Tensor convert_to_eager_tensor(object value, Context ctx, TF_Data
7474
if (dtype != TF_DataType.DtInvalid &&
7575
value.GetType().Name != "NDArray" &&
7676
value.GetType().BaseType.Name != "Array" &&
77-
dtypes.as_base_dtype(dtype) != dtypes.as_tf_dtype(value.GetType()))
77+
dtype != value.GetDataType())
7878
{
7979
switch (dtype)
8080
{
@@ -87,6 +87,9 @@ private static Tensor convert_to_eager_tensor(object value, Context ctx, TF_Data
8787
case TF_DataType.TF_INT64:
8888
value = Convert.ToInt64(value);
8989
break;
90+
case TF_DataType.TF_INT32:
91+
value = Convert.ToInt32(value);
92+
break;
9093
default:
9194
break;
9295
}

src/TensorFlowNET.Core/ops.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static Tensor convert_to_tensor(object value,
162162
? tensor.AsPlaceholder(name: name)
163163
: tensor.AsConstant(name: name),
164164
Tensor tensor => tensor,
165-
Tensor[] tensors => array_ops._autopacking_helper(tensors, dtype, name == null ? "packed" : name),
165+
IEnumerable<Tensor> tensors => array_ops._autopacking_helper(tensors, dtype, name == null ? "packed" : name),
166166
RefVariable varVal => varVal._TensorConversionFunction(dtype: dtype, name: name, as_ref: as_ref),
167167
ResourceVariable varVal => varVal._TensorConversionFunction(dtype: dtype, name: name, as_ref: as_ref),
168168
Axis ts => constant_op.constant(ts.axis, dtype: dtype, name: name),

src/TensorFlowNET.Keras/Layers/Reshaping/Reshape.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ public Reshape(ReshapeArgs args)
2121

2222
protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
2323
{
24-
var shapes = new List<object>();
24+
var shapes = new List<Tensor>();
2525
shapes.Add(array_ops.shape(inputs)[0]);
26+
var dtype = shapes[0].dtype;
2627
if (args.TargetShapeObjects != null)
27-
shapes.AddRange(args.TargetShapeObjects);
28+
// shapes.AddRange(args.TargetShapeObjects);
29+
throw new NotImplementedException("");
2830
if (args.TargetShape != null)
29-
args.TargetShape.dims.ToList().ForEach(x => shapes.Add(x));
31+
shapes.AddRange(args.TargetShape.dims.Select(x => constant_op.constant(x, dtype)));
3032
var shape = ops.convert_to_tensor(shapes);
3133

3234
var result = array_ops.reshape(inputs, shape);

src/TensorFlowNET.Keras/Utils/layer_utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void print_layer_summary_with_connections(ILayer layer, int[] positions,
184184
public static int count_params(Layer layer, List<IVariableV1> weights)
185185
{
186186
var weight_shapes = weights.Select(x => x.shape).ToArray();
187-
var total = weight_shapes.Select(p => (int)np.prod(p.dims)).Sum();
187+
var total = weight_shapes.Select(p => (int)p.size).Sum();
188188
return total;
189189
}
190190

test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Tensorflow;
23
using Tensorflow.NumPy;
34
using static Tensorflow.Binding;
45
using static Tensorflow.KerasApi;
@@ -11,8 +12,8 @@ public class LayersReshapingTest : EagerModeTestBase
1112
[TestMethod]
1213
public void ZeroPadding2D()
1314
{
14-
var input_shape = new[] { 1, 1, 2, 2 };
15-
var x = np.arange((int)np.prod(input_shape)).reshape(input_shape);
15+
Shape input_shape = (1, 1, 2, 2);
16+
var x = np.arange(input_shape.size).reshape(input_shape);
1617
var zero_padding_2d = keras.layers.ZeroPadding2D(new[,] { { 1, 0 }, { 1, 0 } });
1718
var y = zero_padding_2d.Apply(x);
1819
Assert.AreEqual((1, 2, 3, 2), y.shape);
@@ -21,8 +22,8 @@ public void ZeroPadding2D()
2122
[TestMethod]
2223
public void UpSampling2D()
2324
{
24-
var input_shape = new[] { 2, 2, 1, 3 };
25-
var x = np.arange((int)np.prod(input_shape)).reshape(input_shape);
25+
Shape input_shape = (2, 2, 1, 3);
26+
var x = np.arange(input_shape.size).reshape(input_shape);
2627
var y = keras.layers.UpSampling2D(size: (1, 2)).Apply(x);
2728
Assert.AreEqual((2, 2, 2, 3), y.shape);
2829
}

test/TensorFlowNET.Keras.UnitTest/Layers/MeanSquaredError.Test.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void Mse_Double()
2121
{
2222
var mse = keras.losses.MeanSquaredError();
2323
var call = mse.Call(y_true, y_pred);
24-
Assert.AreEqual((NDArray)0.5, call.numpy()) ;
24+
Assert.AreEqual(call.numpy(), 0.5);
2525
}
2626

2727
[TestMethod]
@@ -33,7 +33,7 @@ public void Mse_Float()
3333

3434
var mse = keras.losses.MeanSquaredError();
3535
var call = mse.Call(y_true_float, y_pred_float);
36-
Assert.AreEqual((NDArray)0.5, call.numpy());
36+
Assert.AreEqual(call.numpy(), 0.5f);
3737
}
3838

3939
[TestMethod]
@@ -42,15 +42,15 @@ public void Mse_Sample_Weight()
4242
{
4343
var mse = keras.losses.MeanSquaredError();
4444
var call = mse.Call(y_true, y_pred, sample_weight: (NDArray)new double[] { 0.7, 0.3 });
45-
Assert.AreEqual((NDArray)0.25, call.numpy());
45+
Assert.AreEqual(call.numpy(), 0.25);
4646
}
4747

4848
[TestMethod]
4949
public void Mse_Reduction_SUM()
5050
{
5151
var mse = keras.losses.MeanSquaredError(reduction: Reduction.SUM);
5252
var call = mse.Call(y_true, y_pred);
53-
Assert.AreEqual((NDArray)1.0, call.numpy());
53+
Assert.AreEqual(call.numpy(), 1.0);
5454
}
5555

5656
[TestMethod]
@@ -59,7 +59,7 @@ public void Mse_Reduction_NONE()
5959
{
6060
var mse = keras.losses.MeanSquaredError(reduction: Reduction.NONE);
6161
var call = mse.Call(y_true, y_pred);
62-
Assert.AreEqual((NDArray)new double[] { 0.5, 0.5 }, call.numpy());
62+
Assert.AreEqual(call.numpy(), new double[] { 0.5, 0.5 });
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)