def mutate(self, input_shape, output_shape, mutate_options, entropy): """Randomly alter all aspects of the layer stack.""" mutagen = mutate.Mutagen(mutate_options, entropy) self.optimizer.mutate(mutagen) shape = self.mutate_layers("image", self.image_layers, input_shape, None, mutagen) expand_output = None if self.flatten else output_shape shape = self.mutate_layers("expand", self.expand_layers, shape, expand_output, mutagen) if self.flatten: shape = convnet.flatten_output_shape(shape) return self.mutate_layers("hidden", self.hidden_layers, shape, output_shape, mutagen)
def parameter_count(self, input_shape): count = 0 shape = input_shape for layer in itertools.chain(self.image_layers, self.expand_layers): count += layer.parameter_count(shape) shape = layer.output_shape(shape) if self.flatten: shape = convnet.flatten_output_shape(shape) for layer in self.hidden_layers: count += layer.parameter_count(shape) shape = layer.output_shape(shape) return count
def make_safe(self, input_shape, output_shape): """Try to fix any layer settings to make compatible with the specified input and output shapes.""" shape = input_shape for layer in self.image_layers: shape = layer.make_safe(shape, None) for layer in self.expand_layers: is_output_layer = (layer == self.expand_layers[-1] and not self.flatten) shape = layer.make_safe(shape, output_shape if is_output_layer else None) if self.flatten: shape = convnet.flatten_output_shape(shape) for layer in self.hidden_layers: is_output_layer = (layer == self.hidden_layers[-1]) shape = layer.make_safe(shape, output_shape if is_output_layer else None) return shape
def construct(self, input_shape, output_shape=None): """Construct the convnet operations for this stack.""" operations = [] shape = input_shape for layer in self.image_layers: shape = layer.construct(shape, operations) for layer in self.expand_layers: shape = layer.construct(shape, operations) if self.flatten: operations.append(convnet.create_flatten()) shape = convnet.flatten_output_shape(shape) for layer in self.hidden_layers: shape = layer.construct(shape, operations) if output_shape and shape != output_shape: if convnet.can_slice(shape, output_shape): operations.append(convnet.create_slice(output_shape)) return operations