public override void Connect(Layer inLayer) { if (inLayer.Shape != InputShape) { throw new ShapeMismatchException(nameof(inLayer)); } ShapedArray <Neuron> padded = Padder.PadArray(inLayer.Neurons.ToShape(Shape), Paddings, () => new Neuron() { OutVal = PadVal }); var inConnections = new ShapedArray <List <Synapse> >(PaddedShape, () => new List <Synapse>()); IndexGen.ByStrides(PaddedShape, Strides, KernelShape).ForEach((idxKernel, i) => { Neuron outN = base.Neurons[i]; outN.InSynapses = IndexGen.ByStart(KernelShape, Array <int> .FromRef(idxKernel)).Select((idx, j) => { var S = new Synapse(padded[idx], outN); inConnections[idx].Add(S); return(S); }); }); padded.ForEach((N, i) => N.OutSynapses = Array <Synapse> .FromRef(inConnections[i].ToArray())); }
/// <inheritdoc/> protected override Measurement Measure(RenderContext context, int maxWidth) { var child = new Padder(_child, Padding); var childWidth = ((IRenderable)child).Measure(context, maxWidth); return(new Measurement( childWidth.Min + EdgeWidth, childWidth.Max + EdgeWidth)); }
public Pooling(Shape inputShape, Shape outputShape, Shape kernelShape, Array <int> strides, IOperation poolOp, double padVal = 0.0) : base(outputShape) { InputShape = inputShape; KernelShape = kernelShape; Strides = strides; PoolOp = poolOp; PadVal = padVal; Paddings = Padder.CalcPadding(inputShape, outputShape, kernelShape, strides, true); PaddedShape = inputShape.Pad(Paddings); }
public Convolutional(Shape inputShape, Shape outputShape, Shape kernelShape, Array <int> strides, IActivation activation, IInitializer?weightInit = null, IInitializer?biasInit = null, double padVal = 0.0) : base(outputShape, activation, weightInit, biasInit) { ValidateChannels(inputShape, outputShape, kernelShape, strides); InputShape = inputShape; KernelShape = kernelShape; Strides = strides; PadVal = padVal; Paddings = Padder.CalcPadding(inputShape, outputShape, kernelShape, strides, false); PaddedShape = inputShape.Pad(Paddings); KernelsNum = outputShape.Dims[0] / inputShape.Dims[0]; Kernels = new Array <Kernel>(KernelsNum, () => new Kernel(kernelShape)); Neurons = new Array <Neuron>(outputShape.Volume, () => new CNeuron()); }
public override void Connect(Layer inLayer) { if (inLayer.Shape != InputShape) { throw new ShapeMismatchException(nameof(inLayer)); } ShapedArray <Neuron> padded = Padder.PadArray(inLayer.Neurons.ToShape(Shape), Paddings, () => new Neuron() { OutVal = PadVal }); var inConnections = new ShapedArray <List <Synapse> >(PaddedShape, () => new List <Synapse>()); Kernels.ForEach((kernel, i) => { int offset = i * Shape.Volume / KernelsNum; IndexGen.ByStrides(PaddedShape, Strides, KernelShape).ForEach((idxKernel, j) => { var outN = (CNeuron)Neurons[offset + j]; outN.KernelBias = kernel.Bias; outN.InSynapses = IndexGen.ByStart(KernelShape, Array <int> .FromRef(idxKernel)).Select((idx, k) => { var S = new CSynapse(padded[idx], outN) { KernelWeight = kernel.Weights[k] }; inConnections[idx].Add(S); return((Synapse)S); }); }); }); padded.ForEach((N, i) => N.OutSynapses = Array <Synapse> .FromRef(inConnections[i].ToArray())); }
/// <inheritdoc/> protected override IEnumerable <Segment> Render(RenderContext context, int maxWidth) { var edgeWidth = EdgeWidth; var border = BoxExtensions.GetSafeBorder(Border, !context.Unicode && UseSafeBorder); var borderStyle = BorderStyle ?? Style.Plain; var showBorder = true; if (border is NoBoxBorder) { showBorder = false; edgeWidth = 0; } var child = new Padder(_child, Padding); var childWidth = maxWidth - edgeWidth; if (!Expand) { var measurement = ((IRenderable)child).Measure(context, maxWidth - edgeWidth); childWidth = measurement.Max; } var panelWidth = childWidth + edgeWidth; panelWidth = Math.Min(panelWidth, maxWidth); childWidth = panelWidth - edgeWidth; var result = new List <Segment>(); if (showBorder) { // Panel top AddTopBorder(result, context, border, borderStyle, panelWidth); } // Split the child segments into lines. var childSegments = ((IRenderable)child).Render(context, childWidth); foreach (var(_, _, last, line) in Segment.SplitLines(childSegments, childWidth).Enumerate()) { if (line.Count == 1 && line[0].IsWhiteSpace) { // NOTE: This check might impact other things. // Hopefully not, but there is a chance. continue; } if (showBorder) { result.Add(new Segment(border.GetPart(BoxBorderPart.Left), borderStyle)); } var content = new List <Segment>(); content.AddRange(line); // Do we need to pad the panel? var length = line.Sum(segment => segment.CellCount()); if (length < childWidth) { var diff = childWidth - length; content.Add(Segment.Padding(diff)); } result.AddRange(content); if (showBorder) { result.Add(new Segment(border.GetPart(BoxBorderPart.Right), borderStyle)); } // Don't emit a line break if this is the last // line, we're not showing the border, and we're // not rendering this inline. var emitLinebreak = !(last && !showBorder && !Inline); if (!emitLinebreak) { continue; } result.Add(Segment.LineBreak); } // Panel bottom if (showBorder) { result.Add(new Segment(border.GetPart(BoxBorderPart.BottomLeft), borderStyle)); result.Add(new Segment(border.GetPart(BoxBorderPart.Bottom).Repeat(panelWidth - EdgeWidth), borderStyle)); result.Add(new Segment(border.GetPart(BoxBorderPart.BottomRight), borderStyle)); } // TODO: Need a better name for this? // If we're rendering this as part of an inline parent renderable, // such as columns, we should not emit the last line break. if (!Inline) { result.Add(Segment.LineBreak); } return(result); }