AutoKeras integration for automatic neural network architecture selection
AutoKeras — Neural Architecture Search (NAS) on top of Keras/TensorFlow. Automatically searches for the optimal neural network architecture for a given task: number of layers, layer types, dimensions, dropout, and normalization.
Supported tasks
AutoKeras covers:
autokeras_tasks = {
'ImageClassifier': 'классификация изображений — CNN архитектура',
'ImageRegressor': 'регрессия на изображениях',
'TextClassifier': 'классификация текста — Transformer/LSTM',
'TextRegressor': 'регрессия на тексте',
'StructuredDataClassifier': 'табличные данные — MLP + attention',
'StructuredDataRegressor': 'регрессия на табличных данных',
'TimeseriesForecaster': 'прогнозирование временных рядов',
'MultiModal': 'комбинация типов данных'
}
AutoKeras for images
Search CNN architecture:
import autokeras as ak
import numpy as np
from sklearn.model_selection import train_test_split
def search_image_classifier(images: np.ndarray,
labels: np.ndarray,
max_trials: int = 30,
epochs: int = 20) -> dict:
"""
images: (N, H, W, C) или (N, H, W)
max_trials: количество архитектур для попытки
"""
X_train, X_val, y_train, y_val = train_test_split(
images, labels, test_size=0.2, random_state=42
)
clf = ak.ImageClassifier(
overwrite=True,
max_trials=max_trials,
objective='val_accuracy',
directory='/tmp/autokeras_image'
)
clf.fit(
X_train, y_train,
epochs=epochs,
validation_data=(X_val, y_val),
callbacks=[
ak.callbacks.EarlyStopping(patience=5)
]
)
# Экспорт лучшей модели
best_model = clf.export_model()
val_accuracy = clf.evaluate(X_val, y_val)[1]
return {
'best_architecture': best_model.summary(),
'val_accuracy': val_accuracy,
'trials_evaluated': max_trials
}
AutoKeras for text
Searching for Transformer/LSTM architecture:
def search_text_classifier(texts: list, labels: np.ndarray,
max_trials: int = 20) -> dict:
"""
AutoKeras пробует: BERT fine-tuning, LSTM, Transformer с NAS.
Автоматически токенизирует и создаёт embeddings.
"""
clf = ak.TextClassifier(
overwrite=True,
max_trials=max_trials
)
clf.fit(texts, labels, epochs=10)
best_model = clf.export_model()
return {'model': best_model}
Custom Block Architecture
When standard tasks don't fit:
def custom_block_search():
"""
AutoKeras Block API: задаём пространство поиска вручную.
Полезно для нестандартных архитектур.
"""
input_node = ak.ImageInput()
# Блок: пробуем разные CNN backbone
output_node = ak.ImageBlock(
block_type='efficient', # efficientnet или 'resnet', 'xception'
augment=True
)(input_node)
# Блок: пробуем размеры Dense слоёв
output_node = ak.DenseBlock(
num_layers=ak.hyperparameters.Choice('dense_layers', [1, 2, 3]),
use_batchnorm=True
)(output_node)
output_node = ak.ClassificationHead()(output_node)
automodel = ak.AutoModel(
inputs=input_node,
outputs=output_node,
max_trials=15,
overwrite=True
)
return automodel
Export to TensorFlow Serving:
import tensorflow as tf
def export_for_serving(autokeras_model, export_path: str):
"""AutoKeras модель = Keras модель → стандартный экспорт"""
tf.saved_model.save(autokeras_model, export_path)
# TFLite для мобильных/edge
converter = tf.lite.TFLiteConverter.from_saved_model(export_path)
tflite_model = converter.convert()
with open(f'{export_path}/model.tflite', 'wb') as f:
f.write(tflite_model)
Deadlines: AutoKeras baseline for a standard task: 1-3 days. Custom Block API, export to TF Serving/TFLite, and multi-modal tasks: 1-2 weeks.







