def build_model(): input_layer = ak.Input() cnn_layer = ak.ConvBlock()(input_layer) cnn_layer2 = ak.ConvBlock()(cnn_layer) dense_layer = ak.DenseBlock()(cnn_layer2) dense_layer2 = ak.DenseBlock()(dense_layer) output_layer = ak.ClassificationHead(num_classes=10)(dense_layer2) automodel = ak.auto_model.AutoModel(input_layer, output_layer, max_trials=20, seed=123, project_name='autoML') return automodel
def test_graph_save_load(tmp_path): input1 = ak.Input() input2 = ak.Input() output1 = ak.DenseBlock()(input1) output2 = ak.ConvBlock()(input2) output = ak.Merge()([output1, output2]) output1 = ak.RegressionHead()(output) output2 = ak.ClassificationHead()(output) graph = graph_module.Graph( inputs=[input1, input2], outputs=[output1, output2], override_hps=[ hp_module.Choice("dense_block_1/num_layers", [6], default=6) ], ) path = os.path.join(tmp_path, "graph") graph.save(path) graph = graph_module.load_graph(path) assert len(graph.inputs) == 2 assert len(graph.outputs) == 2 assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock) assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock) assert isinstance(graph.override_hps[0], hp_module.Choice)
def test_graph_can_init_with_one_missing_output(): input_node = ak.ImageInput() output_node = ak.ConvBlock()(input_node) output_node = ak.RegressionHead()(output_node) ak.ClassificationHead()(output_node) graph_module.Graph(input_node, output_node)
def test_graph_save_load(tmp_dir): input1 = ak.Input() input2 = ak.Input() output1 = ak.DenseBlock()(input1) output2 = ak.ConvBlock()(input2) output = ak.Merge()([output1, output2]) output1 = ak.RegressionHead()(output) output2 = ak.ClassificationHead()(output) graph = graph_module.HyperGraph(inputs=[input1, input2], outputs=[output1, output2], override_hps=[ hp_module.Choice( 'dense_block_1/num_layers', [6], default=6) ]) path = os.path.join(tmp_dir, 'graph') graph.save(path) config = graph.get_config() graph = graph_module.HyperGraph.from_config(config) graph.reload(path) assert len(graph.inputs) == 2 assert len(graph.outputs) == 2 assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock) assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock) assert isinstance(graph.override_hps[0], hp_module.Choice)
def test_graph_compile_with_adadelta(): input_node = ak.ImageInput(shape=(32, 32, 3)) output_node = ak.ConvBlock()(input_node) output_node = ak.RegressionHead(output_shape=(1, ))(output_node) graph = graph_module.Graph(input_node, output_node) hp = kerastuner.HyperParameters() hp.values = {"optimizer": "adadelta"} graph.build(hp)
def create_image_regressor(self): input_node = ak.ImageInput() output_node = ak.ConvBlock()(input_node) output_node = ak.DenseBlock()(output_node) output_node = ak.RegressionHead()(output_node) reg = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=10) return reg
def test_functional_api(tmp_dir): # Prepare the data. num_instances = 20 (image_x, train_y), (test_x, test_y) = mnist.load_data() (text_x, train_y), (test_x, test_y) = common.imdb_raw() (structured_data_x, train_y), (test_x, test_y) = common.dataframe_numpy() image_x = image_x[:num_instances] text_x = text_x[:num_instances] structured_data_x = structured_data_x[:num_instances] classification_y = common.generate_one_hot_labels( num_instances=num_instances, num_classes=3) regression_y = common.generate_data(num_instances=num_instances, shape=(1, )) # Build model and train. image_input = ak.ImageInput() output = ak.Normalization()(image_input) output = ak.ImageAugmentation()(output) outputs1 = ak.ResNetBlock(version='next')(image_input) outputs2 = ak.XceptionBlock()(image_input) image_output = ak.Merge()((outputs1, outputs2)) structured_data_input = ak.StructuredDataInput( column_names=common.COLUMN_NAMES_FROM_CSV, column_types=common.COLUMN_TYPES_FROM_CSV) structured_data_output = ak.FeatureEngineering()(structured_data_input) structured_data_output = ak.DenseBlock()(structured_data_output) text_input = ak.TextInput() outputs1 = ak.TextToIntSequence()(text_input) outputs1 = ak.EmbeddingBlock()(outputs1) outputs1 = ak.ConvBlock(separable=True)(outputs1) outputs1 = ak.SpatialReduction()(outputs1) outputs2 = ak.TextToNgramVector()(text_input) outputs2 = ak.DenseBlock()(outputs2) text_output = ak.Merge()((outputs1, outputs2)) merged_outputs = ak.Merge()( (structured_data_output, image_output, text_output)) regression_outputs = ak.RegressionHead()(merged_outputs) classification_outputs = ak.ClassificationHead()(merged_outputs) automodel = ak.GraphAutoModel( inputs=[image_input, text_input, structured_data_input], directory=tmp_dir, outputs=[regression_outputs, classification_outputs], max_trials=2, seed=common.SEED) automodel.fit((image_x, text_x, structured_data_x), (regression_y, classification_y), validation_split=0.2, epochs=2)
def functional_api(): (x_train, y_train), (x_test, y_test) = mnist.load_data() input_node = ak.ImageInput() output_node = input_node output_node = ak.Normalization()(output_node) output_node = ak.ConvBlock()(output_node) output_node = ak.SpatialReduction()(output_node) output_node = ak.DenseBlock()(output_node) output_node = ak.ClassificationHead()(output_node) clf = ak.GraphAutoModel(input_node, output_node, seed=5, max_trials=3) clf.fit(x_train, y_train, validation_split=0.2) return clf.evaluate(x_test, y_test)
def test_functional_api(tmp_path): # Prepare the data. num_instances = 80 (image_x, train_y), (test_x, test_y) = mnist.load_data() (text_x, train_y), (test_x, test_y) = utils.imdb_raw() (structured_data_x, train_y), (test_x, test_y) = utils.dataframe_numpy() image_x = image_x[:num_instances] text_x = text_x[:num_instances] structured_data_x = structured_data_x[:num_instances] classification_y = utils.generate_one_hot_labels( num_instances=num_instances, num_classes=3) regression_y = utils.generate_data(num_instances=num_instances, shape=(1, )) # Build model and train. image_input = ak.ImageInput() output = ak.Normalization()(image_input) output = ak.ImageAugmentation()(output) outputs1 = ak.ResNetBlock(version='next')(output) outputs2 = ak.XceptionBlock()(output) image_output = ak.Merge()((outputs1, outputs2)) structured_data_input = ak.StructuredDataInput() structured_data_output = ak.CategoricalToNumerical()(structured_data_input) structured_data_output = ak.DenseBlock()(structured_data_output) text_input = ak.TextInput() outputs1 = ak.TextToIntSequence()(text_input) outputs1 = ak.Embedding()(outputs1) outputs1 = ak.ConvBlock(separable=True)(outputs1) outputs1 = ak.SpatialReduction()(outputs1) outputs2 = ak.TextToNgramVector()(text_input) outputs2 = ak.DenseBlock()(outputs2) text_output = ak.Merge()((outputs1, outputs2)) merged_outputs = ak.Merge()( (structured_data_output, image_output, text_output)) regression_outputs = ak.RegressionHead()(merged_outputs) classification_outputs = ak.ClassificationHead()(merged_outputs) automodel = ak.AutoModel( inputs=[image_input, text_input, structured_data_input], directory=tmp_path, outputs=[regression_outputs, classification_outputs], max_trials=2, tuner=ak.Hyperband, seed=utils.SEED) automodel.fit((image_x, text_x, structured_data_x), (regression_y, classification_y), validation_split=0.2, epochs=1)
def CreateSupergraph(output_dir, hp_tuner): input_node = ak.Input() conv2d_1 = ak.ConvBlock(num_blocks=1, num_layers=3, max_pooling=True, dropout=0)(input_node) dense_1 = ak.DenseBlock(dropout=0)(conv2d_1) output_node = ak.ClassificationHead(num_classes=4, metrics=['accuracy'])(dense_1) automodel = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=3, directory=output_dir, project_name="autoML", tuner=hp_tuner, seed=123) return automodel
def test_text_and_structured_data(tmp_path): # Prepare the data. num_instances = 80 (x_text, y_train), (x_test, y_test) = utils.imdb_raw() x_structured_data = pd.read_csv(utils.TRAIN_CSV_PATH) x_text = x_text[:num_instances] x_structured_data = x_structured_data[:num_instances] y_classification = utils.generate_one_hot_labels( num_instances=num_instances, num_classes=3) y_regression = utils.generate_data(num_instances=num_instances, shape=(1, )) # Build model and train. structured_data_input = ak.StructuredDataInput() structured_data_output = ak.CategoricalToNumerical()(structured_data_input) structured_data_output = ak.DenseBlock()(structured_data_output) text_input = ak.TextInput() outputs1 = ak.TextToIntSequence()(text_input) outputs1 = ak.Embedding()(outputs1) outputs1 = ak.ConvBlock(separable=True)(outputs1) outputs1 = ak.SpatialReduction()(outputs1) outputs2 = ak.TextToNgramVector()(text_input) outputs2 = ak.DenseBlock()(outputs2) text_output = ak.Merge()((outputs1, outputs2)) merged_outputs = ak.Merge()((structured_data_output, text_output)) regression_outputs = ak.RegressionHead()(merged_outputs) classification_outputs = ak.ClassificationHead()(merged_outputs) automodel = ak.AutoModel( inputs=[text_input, structured_data_input], directory=tmp_path, outputs=[regression_outputs, classification_outputs], max_trials=2, tuner=ak.Hyperband, seed=utils.SEED, ) automodel.fit( (x_text, x_structured_data), (y_regression, y_classification), validation_split=0.2, epochs=1, )
def test_conv_block(tmp_dir): x_train = np.random.rand(100, 3, 3, 3) y_train = np.random.randint(10, size=100) y_train = tf.keras.utils.to_categorical(y_train) input_node = ak.Input() output_node = input_node output_node = ak.ConvBlock()(output_node) output_node = ak.ClassificationHead()(output_node) input_node.shape = (3, 3, 3) output_node[0].shape = (10,) graph = ak.GraphAutoModel(input_node, output_node, directory=tmp_dir) model = graph.build(kerastuner.HyperParameters()) model.fit(x_train, y_train, epochs=1, batch_size=100, verbose=False) result = model.predict(x_train) assert result.shape == (100, 10)
def test_minist(self): (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train.shape) # (60000, 28, 28) print(y_train.shape) # (60000,) print(y_train[:3]) # array([7, 2, 1], dtype=uint8) input_node = ak.ImageInput() output_node = ak.ConvBlock()(input_node) output_node = ak.DenseBlock()(output_node) output_node = ak.ClassificationHead()(output_node) clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=10) clf.fit(x_train, y_train) model = clf.export_model() print(type(model)) model.save('./auto_model.hd5')
def functional_api(): max_features = 20000 max_words = 400 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data( num_words=max_features, index_from=3) x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_words) x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_words) print(x_train.dtype) print(x_train[:10]) input_node = ak.Input() output_node = input_node output_node = ak.EmbeddingBlock(max_features=max_features)(output_node) output_node = ak.ConvBlock()(output_node) output_node = ak.SpatialReduction()(output_node) output_node = ak.DenseBlock()(output_node) output_node = ak.ClassificationHead()(output_node) clf = ak.AutoModel(input_node, output_node, seed=5, max_trials=3) clf.fit(x_train, y_train, validation_split=0.2) return clf.evaluate(x_test, y_test)
def test_graph_save_load(tmp_path): input1 = ak.Input() input2 = ak.Input() output1 = ak.DenseBlock()(input1) output2 = ak.ConvBlock()(input2) output = ak.Merge()([output1, output2]) output1 = ak.RegressionHead()(output) output2 = ak.ClassificationHead()(output) graph = graph_module.Graph( inputs=[input1, input2], outputs=[output1, output2], ) path = os.path.join(tmp_path, "graph") graph.save(path) graph = graph_module.load_graph(path) assert len(graph.inputs) == 2 assert len(graph.outputs) == 2 assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock) assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
def test_conv_block(tmp_dir): x_train = np.random.rand(100, 3, 3, 3) y_train = np.random.randint(10, size=100) y_train = tf.keras.utils.to_categorical(y_train) input_node = ak.Input() output_node = input_node output_node = ak.ConvBlock()(output_node) output_node = ak.ClassificationHead()(output_node) input_node.shape = (3, 3, 3) output_node[0].shape = (10,) graph = ak.GraphAutoModel(input_node, output_node, directory=tmp_dir, max_trials=1) graph.fit(x_train, y_train, epochs=1, batch_size=100, verbose=False, validation_split=0.2) result = graph.predict(x_train) assert result.shape == (100, 10)
id5 --> id6 id7(StructuredDataInput) --> id8(CategoricalToNumerical) id8 --> id9(DenseBlock) id6 --> id10(Merge) id9 --> id10 id10 --> id11(Classification Head) id10 --> id12(Regression Head) </div> """ import autokeras as ak input_node1 = ak.ImageInput() output_node = ak.Normalization()(input_node1) output_node = ak.ImageAugmentation()(output_node) output_node1 = ak.ConvBlock()(output_node) output_node2 = ak.ResNetBlock(version='v2')(output_node) output_node1 = ak.Merge()([output_node1, output_node2]) input_node2 = ak.StructuredDataInput() output_node = ak.CategoricalToNumerical()(input_node2) output_node2 = ak.DenseBlock()(output_node) output_node = ak.Merge()([output_node1, output_node2]) output_node1 = ak.ClassificationHead()(output_node) output_node2 = ak.RegressionHead()(output_node) auto_model = ak.AutoModel(inputs=[input_node1, input_node2], outputs=[output_node1, output_node2], overwrite=True, max_trials=2)
[functional API](https://www.tensorflow.org/guide/keras/functional) of Keras. Basically, you are building a graph, whose edges are blocks and the nodes are intermediate outputs of blocks. To add an edge from `input_node` to `output_node` with `output_node = ak.[some_block]([block_args])(input_node)`. You can even also use more fine grained blocks to customize the search space even further. See the following example. """ import autokeras as ak input_node = ak.TextInput() output_node = ak.TextToIntSequence()(input_node) output_node = ak.Embedding()(output_node) # Use separable Conv layers in Keras. output_node = ak.ConvBlock(separable=True)(output_node) output_node = ak.ClassificationHead()(output_node) clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=1) clf.fit(x_train, y_train, epochs=2) """ ## Data Format The AutoKeras TextClassifier is quite flexible for the data format. For the text, the input data should be one-dimensional For the classification labels, AutoKeras accepts both plain labels, i.e. strings or integers, and one-hot encoded encoded labels, i.e. vectors of 0s and 1s. We also support using [tf.data.Dataset]( https://www.tensorflow.org/api_docs/python/tf/data/Dataset?version=stable) format for the training data. The labels have to be one-hot encoded for multi-class
y_train = np.zeros(num_instances).astype(np.float32) y_train[0 : int(num_instances / 2)] = 1 x_test = np.random.rand(num_instances, num_features).astype(np.float32) y_test = np.zeros(num_instances).astype(np.float32) y_train[0 : int(num_instances / 2)] = 1 x_train = np.expand_dims( x_train, axis=2 ) # This step it's very important an CNN will only accept this data shape print(x_train.shape) print(y_train.shape) # Prepare Automodel for search input_node = ak.Input() output_node = ak.ConvBlock()(input_node) # output_node = ak.DenseBlock()(output_node) #optional # output_node = ak.SpatialReduction()(output_node) #optional output_node = ak.ClassificationHead(num_classes=2, multi_label=True)(output_node) auto_model = ak.AutoModel( inputs=input_node, outputs=output_node, overwrite=True, max_trials=1 ) # Search auto_model.fit(x_train, y_train, epochs=1) print(auto_model.evaluate(x_test, y_test)) # Export as a Keras Model