{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Example of Incremental Learning Agent w/Python + Keras (MultiClass Example)\n", "\n", "Example of Incremental Learning Agent on Multiclass problem." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "from keras.models import Sequential\n", "from keras.layers import Dense\n", "import numpy as np\n", "import pandas as pd\n", "from sklearn.preprocessing import LabelEncoder\n", "\n", "np.random.seed(4)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.read_csv(\"../datasets/iris.csv\", header=None)\n", "df = df.values\n", "X = df[:,0:4].astype(float)\n", "y = df[:,4]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Encode class values\n", "from keras.utils import np_utils\n", "encoder = LabelEncoder()\n", "encoder.fit(y)\n", "encoded_Y = encoder.transform(y)\n", "# One hot encoding\n", "dummy_y = np_utils.to_categorical(encoded_Y)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import hashlib\n", "from keras.models import load_model" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class IncrementalAgent:\n", " \n", " def __init__(self, name):\n", " # Identify agent\n", " self.name = name\n", " # Need to keep track of predictions for evaluation of the model\n", " self.prediction_records = {}\n", " # Create model\n", " self.model = Sequential()\n", " self.model.add(Dense(4, input_dim=4, kernel_initializer='normal', activation='relu'))\n", " self.model.add(Dense(3, kernel_initializer='normal', activation='sigmoid'))\n", " # Compile model\n", " self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n", " print(\"Model Initialized.\")\n", "\n", " \n", " def train(self, feature_array, target, epoch_count, batch_count):\n", " target_to_array = np.array([target])\n", " print(target_to_array)\n", " self.model.fit(feature_array, target_to_array, epochs=epoch_count, batch_size=batch_count)\n", " self.model.save('{}.h5'.format(self.name))\n", " print(\"Model Trained.\")\n", " \n", " \n", " def evaluate(self, feature_array, target_class):\n", " # Check if the features have previously been seen\n", " prediction_ref = hashlib.sha256(feature_array).hexdigest()\n", " if prediction_ref in self.prediction_records:\n", " print(\"Previous Predicted Class: {}\".format(self.prediction_records[prediction_ref]))\n", " print(\"Actual Class: {}\".format(target_class))\n", " if self.prediction_records[prediction_ref] == target_class:\n", " print(\"Prediction Correct!\")\n", " else:\n", " print(\"Prediction Incorrect. Relearn...\")\n", " self.train(feature_array, target, 5, 1)\n", " print(\"Retry Predict & Update Previous Prediction\")\n", " self.predict(feature_array)\n", " \n", " else:\n", " print(\"First time seeing this sample.\")\n", " \n", "\n", " def predict(self, feature_array):\n", " # Create ID for prediction\n", " prediction_ref = hashlib.sha256(feature_array).hexdigest()\n", " # Actual prediction\n", " probability_prediction = self.model.predict(feature_array)\n", " \n", " # Get Class Prediction\n", " prediction = probability_prediction.argmax() \n", " \n", " # Add Prediction to Records\n", " self.prediction_records[prediction_ref] = prediction\n", " \n", " print(\"Predicted Class: {}\".format(prediction))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "# Split for training and evaluation\n", "X_train, X_test, y_train, y_test = train_test_split(X, dummy_y, test_size=0.33)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sample = np.array([X_train[2]])\n", "target = np.array([dummy_y[2]])[0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model Initialized.\n" ] } ], "source": [ "agent1 = IncrementalAgent(\"multiclass_agent\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0.]]\n", "Epoch 1/5\n", "1/1 [==============================] - 0s 190ms/step - loss: 1.0970 - acc: 0.0000e+00\n", "Epoch 2/5\n", "1/1 [==============================] - 0s 2ms/step - loss: 1.0941 - acc: 0.0000e+00\n", "Epoch 3/5\n", "1/1 [==============================] - 0s 2ms/step - loss: 1.0912 - acc: 0.0000e+00\n", "Epoch 4/5\n", "1/1 [==============================] - 0s 2ms/step - loss: 1.0882 - acc: 0.0000e+00\n", "Epoch 5/5\n", "1/1 [==============================] - 0s 2ms/step - loss: 1.0854 - acc: 1.0000\n", "Model Trained.\n" ] } ], "source": [ "agent1.train(sample, target, 5, 1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "next_sample = np.array([X_train[3]])\n", "next_target = np.array([dummy_y[3]])[0]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "next_target = next_target.argmax()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(next_target)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted Class: 0\n" ] } ], "source": [ "agent1.predict(next_sample)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Previous Predicted Class: 0\n", "Actual Class: 0\n", "Prediction Correct!\n" ] } ], "source": [ "agent1.evaluate(next_sample, next_target)" ] } ], "metadata": { "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }