def test_autocrop_array_shapes(self): from lasagne.layers.merge import autocrop_array_shapes crop0 = None crop1 = [None, 'lower', 'center', 'upper'] # Too few crop modes; should get padded with None crop2 = ['lower', 'upper'] # Invalid crop modes crop_bad = ['lower', 'upper', 'bad', 'worse'] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop0) == \ [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop1) == \ [(1, 2, 3, 2), (5, 2, 3, 2), (5, 2, 3, 2)] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop2) == \ [(1, 2, 3, 4), (1, 2, 7, 8), (1, 2, 3, 2)] assert autocrop_array_shapes( [(None, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop2) == \ [(None, 2, 3, 4), (None, 2, 7, 8), (None, 2, 3, 2)] with pytest.raises(ValueError): autocrop_array_shapes([(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop_bad) # Inconsistent dimensionality with pytest.raises(ValueError): autocrop_array_shapes([(1, 2, 3, 4), (5, 6, 7), (5, 4, 3, 2, 10)], crop1)
def test_autocrop_array_shapes(self): from lasagne.layers.merge import autocrop_array_shapes crop0 = None crop1 = [None, 'lower', 'center', 'upper'] # Too few crop modes; should get padded with None crop2 = ['lower', 'upper'] # Invalid crop modes crop_bad = ['lower', 'upper', 'bad', 'worse'] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop0) == \ [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop1) == \ [(1, 2, 3, 2), (5, 2, 3, 2), (5, 2, 3, 2)] assert autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop2) == \ [(1, 2, 3, 4), (1, 2, 7, 8), (1, 2, 3, 2)] assert autocrop_array_shapes( [(None, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop2) == \ [(None, 2, 3, 4), (None, 2, 7, 8), (None, 2, 3, 2)] with pytest.raises(ValueError): autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7, 8), (5, 4, 3, 2)], crop_bad) # Inconsistent dimensionality with pytest.raises(ValueError): autocrop_array_shapes( [(1, 2, 3, 4), (5, 6, 7), (5, 4, 3, 2, 10)], crop1)
def get_output_shape_for(self, input_shapes): input_shapes = autocrop_array_shapes(input_shapes, self.cropping) # Infer the output shape by grabbing, for each axis, the first # input size that is not `None` (if there is any) output_shape = input_shapes[1] return output_shape
def __init__(self, layers, croppings=[], axis=1, **kwargs): from lasagne.layers.merge import autocrop_array_shapes super(Concatenate, self).__init__(maxInConnections=None, **kwargs) self.layers = list(layers) self.croppings = croppings self.axis = axis self.croppedShapes = [] self.originalShapes = [] for l in self.layers: self.originalShapes.append(l.getShape_abs()) self.croppedShapes = autocrop_array_shapes(self.originalShapes, self.croppings) self.shape = [] for i in xrange(len(self.croppedShapes[0])): if i == self.axis: v = 0 for s in self.croppedShapes: v += s[i] else: v = self.croppedShapes[0][i] self.shape.append(v) self.shape = tuple(self.shape) self.streams = None for l in self.layers: l.connect(self) if self.streams is None: self.streams = set(l.outputs.streams) else: sl = set(l.outputs.streams) notCommon = self.streams - sl if len(notCommon) > 0: print MCAN.warning( "Layers %s do not share streams: %s with other layers .Will take the intersection" % (l.name, notCommon)) self.streams = self.streams & sl self.outputs = MTYPES.Variable(streams=self.streams)