You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Reshape.cs 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Tensorflow.Keras.ArgsDefinition;
  2. using Tensorflow.Keras.Engine;
  3. using static Tensorflow.Binding;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Linq;
  7. namespace Tensorflow.Keras.Layers
  8. {
  9. /// <summary>
  10. /// Layer that reshapes inputs into the given shape.
  11. /// </summary>
  12. public class Reshape : Layer
  13. {
  14. ReshapeArgs args;
  15. public Reshape(ReshapeArgs args)
  16. : base(args)
  17. {
  18. this.args = args;
  19. }
  20. protected override Tensors Call(Tensors inputs, Tensor state = null, bool is_training = false)
  21. {
  22. var shapes = new List<object>();
  23. shapes.Add(array_ops.shape(inputs)[0]);
  24. if (args.TargetShapeObjects != null)
  25. shapes.AddRange(args.TargetShapeObjects);
  26. if (args.TargetShape != null)
  27. args.TargetShape.dims.ToList().ForEach(x => shapes.Add(x));
  28. var shape = ops.convert_to_tensor(shapes);
  29. var result = array_ops.reshape(inputs, shape);
  30. if (!tf.Context.executing_eagerly())
  31. result.set_shape(ComputeOutputShape(inputs.shape));
  32. return result;
  33. }
  34. public override TensorShape ComputeOutputShape(TensorShape input_shape)
  35. {
  36. if (input_shape.dims[1..].Contains(-1))
  37. {
  38. throw new NotImplementedException("");
  39. }
  40. else
  41. {
  42. input_shape = input_shape.dims[0];
  43. var output_shape = input_shape.concatenate(args.TargetShape.dims);
  44. return output_shape;
  45. }
  46. }
  47. }
  48. }