/// <summary>コンストラクタ</summary> public Broadcast(Field infield, Field outfield, Shape shape) : base(new Field[] { infield }, outfield) { if (shape.Ndim < infield.Shape.Ndim) { throw new ArgumentException(ExceptionMessage.Broadcast(infield.Shape, shape)); } List <int> axes_keepdims = new List <int>(); List <int> axes_abandondims = new List <int>(); for (int i = 0; i < infield.Shape.Ndim; i++) { if (infield.Shape[i] == 1 && shape[i] > 1) { axes_keepdims.Add(i); } } for (int i = infield.Shape.Ndim; i < shape.Ndim; i++) { axes_abandondims.Add(i); } this.axes_keepdims = axes_keepdims.ToArray(); this.axes_abandondims = axes_abandondims.ToArray(); this.InShape = infield.Shape; this.OutShape = shape; }
/// <summary>コンストラクタ</summary> public Broadcast(Shape inshape, Shape outshape) { if (BroadcastShape(inshape, outshape) != outshape) { throw new ArgumentException(ExceptionMessage.Broadcast(inshape, outshape)); } this.arguments = new List <(ArgumentType type, Shape shape)> { (ArgumentType.In, inshape), (ArgumentType.Out, outshape), }; }
/// <summary>コンストラクタ</summary> public Broadcast(Shape inshape, Shape targetshape) : base(inputs: 1, outputs: 1, allow_resubstitution: false) { if (inshape.Ndim > targetshape.Ndim) { throw new ArgumentException(ExceptionMessage.Broadcast(inshape, targetshape)); } for (int i = 0; i < inshape.Ndim; i++) { if (inshape[i] != 1 && inshape[i] != targetshape[i]) { throw new ArgumentException(ExceptionMessage.Broadcast(inshape, targetshape)); } } this.InShape = inshape; this.OutShape = Operators.ArrayManipulation.Broadcast.BroadcastShape(inshape, targetshape); this.TargetShape = targetshape; }
/// <summary>形状を調整する</summary> public static VariableNode AdjectShape(VariableNode node, Shape shape) { if (shape != node.Shape) { if (shape.Ndim == 0 && node.Shape.Ndim > 0) { node = Sum(node, keepdims: false); } else if (shape.Ndim == 1 && node.Shape.Ndim > 1 && shape[0] == node.Shape[0]) { int[] axes = (new int[node.Shape.Ndim - 1]).Select((_, idx) => idx + 1).ToArray(); node = Sum(node, axes, keepdims: false); } else { throw new ArgumentException(ExceptionMessage.Broadcast(node.Shape, shape)); } } return(node); }