def generate_mlp_architectures(class_or_reg='reg'): """ pylint: disable=bad-whitespace. """ # pylint: disable=bad-whitespace all_layer_label_classes = get_mlp_layer_labels(class_or_reg) last_layer_label = 'linear' if class_or_reg == 'reg' else 'softmax' # Network 1 layer_labels = [ 'ip', 'op', 'tanh', 'logistic', 'softplus', 'relu', 'elu', last_layer_label ] num_units_each_layer = [None, None, 32, 64, 16, 8, 8, None] A = get_dok_mat_with_set_coords(8, [(0, 4), (2, 3), (3, 7), (4, 5), (4, 6), (5, 2), (6, 2), (7, 1)]) mlp_1 = MultiLayerPerceptron(class_or_reg, layer_labels, A, num_units_each_layer, all_layer_label_classes) # Network 2 layer_labels = [ 'ip', 'softplus', 'elu', 'tanh', 'logistic', last_layer_label, 'op' ] num_units_each_layer = [None, 16, 16, 32, 64, None, None] A = get_dok_mat_with_set_coords(7, [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]) mlp_2 = MultiLayerPerceptron(class_or_reg, layer_labels, A, num_units_each_layer, all_layer_label_classes) # Network 3 layer_labels = [ 'ip', 'tanh', 'logistic', 'logistic', 'tanh', 'elu', 'relu', last_layer_label, 'op' ] num_units_each_layer = [None, 8, 8, 8, 8, 16, 16, None, None] A = get_dok_mat_with_set_coords(9, [(0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 7), (6, 7), (7, 8)]) mlp_3 = MultiLayerPerceptron(class_or_reg, layer_labels, A, num_units_each_layer, all_layer_label_classes) # Network 4 layer_labels = [ 'ip', 'logistic', 'relu', 'softplus', last_layer_label, 'op', 'relu', 'tanh' ] num_units_each_layer = [None, 8, 8, 16, None, None, 16, 8] A = get_dok_mat_with_set_coords(8, [(0, 1), (0, 7), (1, 2), (2, 3), (2, 6), (7, 6), (7, 3), (3, 4), (6, 4), (4, 5)]) mlp_4 = MultiLayerPerceptron(class_or_reg, layer_labels, A, num_units_each_layer, all_layer_label_classes) return [ mlp_1, mlp_2, mlp_3, mlp_4, get_blocked_mlp(class_or_reg, 4, 3), get_blocked_mlp(class_or_reg, 8, 2), get_multidepth_mlp_eg1(class_or_reg), get_multidepth_mlp_eg2(class_or_reg), ]
def get_multidepth_mlp_eg1(class_or_reg): """ Multi depth MLP eg 1. """ layer_labels, num_units_in_each_layer, edges, mlp_layer_labels = \ _get_multidepth_mlp_eg12_common(class_or_reg) conn_mat = get_dok_mat_with_set_coords(len(layer_labels), edges) return MultiLayerPerceptron(class_or_reg, layer_labels, conn_mat, num_units_in_each_layer, mlp_layer_labels)
def get_new_nn(old_nn, layer_labels, num_units_in_each_layer, conn_mat, mandatory_child_attributes): """ Returns a new neural network of the same type as old_nn. """ known_nn_class = True try: if old_nn.nn_class == 'cnn': new_cnn = ConvNeuralNetwork(layer_labels, conn_mat, num_units_in_each_layer, mandatory_child_attributes.strides, old_nn.all_layer_label_classes, old_nn.layer_label_similarities) return new_cnn elif old_nn.nn_class.startswith('mlp'): return MultiLayerPerceptron(old_nn.nn_class[4:], layer_labels, conn_mat, num_units_in_each_layer, old_nn.all_layer_label_classes, old_nn.layer_label_similarities) else: known_nn_class = False except (CNNImageSizeMismatchException, CNNNoConvAfterIPException, AssertionError): return None if not known_nn_class: raise ValueError('Unidentified nn_class %s.'%(old_nn.nn_class))
def get_blocked_mlp(class_or_reg, num_blocks, num_layers_per_block=None, num_units_in_each_layer=None): """ Creates a blocked MLP. """ # Create rectifiers and sigmoids rectifiers = ['relu', 'elu', 'crelu', 'leaky-relu', 'softplus'] sigmoids = ['logistic', 'tanh'] obtl_label = 'linear' if class_or_reg == 'reg' else 'softmax' np.random.shuffle(rectifiers) np.random.shuffle(sigmoids) rect_count = 0 sig_count = 0 # Preprocess args # Create the network layer_labels = ['ip'] num_units_in_each_layer = num_units_in_each_layer if num_units_in_each_layer \ is not None else _dflt_unit_sizes(num_blocks) # Construct blocks for block_idx in range(num_blocks): if block_idx % 2 == 0: layer_type = rectifiers[rect_count % len(rectifiers)] rect_count += 1 else: layer_type = sigmoids[sig_count % len(sigmoids)] sig_count += 1 layer_labels.extend([layer_type] * num_layers_per_block) num_units_in_each_layer.extend([num_units_in_each_layer[block_idx]] * num_layers_per_block) # A linear layer at the end of the block layer_labels.extend([obtl_label, 'op']) num_units_in_each_layer.extend([None, None]) conn_mat = get_feedforward_adj_mat(len(layer_labels)) all_layer_labels = get_mlp_layer_labels(class_or_reg) return MultiLayerPerceptron(class_or_reg, layer_labels, conn_mat, num_units_in_each_layer, all_layer_labels)