def build_feature_model(input_shape, name='', dense_layers=[1000, 1000], activation='relu', residual=False, dropout_rate=0, permanent_dropout=True): print('input_shape', input_shape) x_input = Input(shape=input_shape) h = x_input for i, layer in enumerate(dense_layers): x = h h = Dense(layer, activation=activation)(h) if dropout_rate > 0: if permanent_dropout: h = PermanentDropout(dropout_rate)(h) else: h = Dropout(dropout_rate)(h) if residual: try: h = Add([h, x]) except ValueError: pass model = Model(x_input, h, name=name) return model
def build_model(loader, args, permanent_dropout=True, silent=False): input_shapes = {} dropout_rate = args.dropout for fea_type, shape in loader.feature_shapes.items(): base_type = fea_type.split('.')[0] if base_type in ['cell', 'drug']: if not silent: print('Feature encoding submodel for %s:', fea_type) #box.summary(print_fn=logger.debug) input_shapes[fea_type] = shape inputs = [] encoded_inputs = [] for fea_name, fea_type in loader.input_features.items(): shape = loader.feature_shapes[fea_type] fea_input = Input(shape, name='input.' + fea_name) inputs.append(fea_input) if fea_type in input_shapes: if args.dense_cell_feature_layers is not None and base_type == 'cell': dense_feature_layers = args.dense_cell_feature_layers elif args.dense_drug_feature_layers is not None and base_type == 'drug': dense_feature_layers = args.dense_drug_feature_layers else: dense_feature_layers = args.dense_feature_layers input_model = build_feature_model( input_shape=shape, name=fea_type, dense_layers=dense_feature_layers, dropout_rate=dropout_rate, permanent_dropout=permanent_dropout) encoded = input_model(fea_input) else: encoded = fea_input encoded_inputs.append(encoded) merged = Concatenate(axis=1)(encoded_inputs) h = merged for i, layer in enumerate(args.dense): x = h h = Dense(layer, activation=args.activation)(h) if dropout_rate > 0: if permanent_dropout: h = PermanentDropout(dropout_rate)(h) else: h = Dropout(dropout_rate)(h) if args.residual: try: h = Add([h, x]) except ValueError: pass output = Dense(1)(h) return Model(inputs, output)
def add_test(): input1 = Input(shape=(16, ), dtype="float32") x1 = Dense(8, activation='relu')(input1) input2 = Input(shape=(32,), dtype="float32") x2 = Dense(8, activation='relu')(input2) subtracted = Add()([x1, x2]) out = Dense(4)(subtracted) model = Model([input1, input2], out) opt = flexflow.keras.optimizers.SGD(learning_rate=0.01) model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy', 'sparse_categorical_crossentropy']) print(model.summary()) model.ffmodel.init_layers()