jhfjhfj1/autokeras

View on GitHub
docs/ipynb/export.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab_type": "code"
   },
   "outputs": [],
   "source": [
    "!pip install autokeras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab_type": "code"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from keras.datasets import mnist\n",
    "from keras.models import load_model\n",
    "\n",
    "import autokeras as ak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "You can easily export your model the best model found by AutoKeras as a Keras\n",
    "Model.\n",
    "\n",
    "The following example uses [ImageClassifier](/image_classifier) as an example.\n",
    "All the tasks and the [AutoModel](/auto_model/#automodel-class) has this\n",
    "[export_model](/auto_model/#export_model-method) function.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab_type": "code"
   },
   "outputs": [],
   "source": [
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "# Initialize the image classifier.\n",
    "clf = ak.ImageClassifier(\n",
    "    overwrite=True, max_trials=1\n",
    ")  # Try only 1 model.(Increase accordingly)\n",
    "# Feed the image classifier with training data.\n",
    "clf.fit(x_train, y_train, epochs=1)  # Change no of epochs to improve the model\n",
    "# Export as a Keras Model.\n",
    "model = clf.export_model()\n",
    "\n",
    "print(type(model))  # <class 'tensorflow.python.keras.engine.training.Model'>\n",
    "\n",
    "model.save(\"model_autokeras.keras\")\n",
    "\n",
    "\n",
    "loaded_model = load_model(\n",
    "    \"model_autokeras.keras\", custom_objects=ak.CUSTOM_OBJECTS\n",
    ")\n",
    "\n",
    "predicted_y = loaded_model.predict(np.expand_dims(x_test, -1))\n",
    "print(predicted_y)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "export",
   "private_outputs": false,
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}