diff --git a/Files/AI/Models/Prediction_with_keras.ipynb b/Files/AI/Models/Prediction_with_keras.ipynb new file mode 100644 index 0000000..492e158 --- /dev/null +++ b/Files/AI/Models/Prediction_with_keras.ipynb @@ -0,0 +1,174 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "zTKyKPBvAGXt" + }, + "outputs": [], + "source": [ + "from keras.models import Sequential\n", + "from keras.layers import Dense\n", + "import keras\n", + "import pandas as pd\n", + "import numpy as np\n", + "from keras.models import load_model\n", + "from keras.layers import Dropout\n", + "from sklearn.model_selection import train_test_split\n", + "import matplotlib.pyplot as plt\n", + "import tensorflow as tf" + ] + }, + { + "cell_type": "code", + "source": [ + "path = \"/content/wd.csv\"\n", + "df = pd.read_csv(path,sep=\",\")" + ], + "metadata": { + "id": "GgwnEH8cAZ-X" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "df.drop(labels=[\"Unnamed: 0\"],axis=1,inplace=True)" + ], + "metadata": { + "id": "7o5tKojYq-8i" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "data = df.iloc[:-1,1:].values\n", + "data = np.transpose(data)\n", + "total = df.iloc[-1][1:].values\n", + "X = np.asarray(data).astype(np.float32)\n", + "Y = np.asarray(total).astype(np.float32)" + ], + "metadata": { + "id": "B2V80bNlAhCO" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(data, total, test_size = 1/5, random_state = 123, shuffle=1)\n", + "X = np.asarray(X_train).astype(np.float32)\n", + "Y = np.asarray(y_train).astype(np.float32)\n", + "X_test = np.asarray(X_test).astype(np.float32)\n", + "Y_test = np.asarray(y_test).astype(np.float32)" + ], + "metadata": { + "id": "lplZPLbdAsKP" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "model = Sequential()\n", + "model.add(Dense(512, activation='relu'))\n", + "model.add(Dropout(0.5))\n", + "model.add(Dense(256, activation='relu'))\n", + "model.add(Dropout(0.25))\n", + "model.add(Dense(1))\n", + "model.compile(optimizer='Adam',\n", + " loss='mean_absolute_error',\n", + " metrics=['RootMeanSquaredError'])\n", + "history = model.fit(X,Y,\n", + " batch_size=16,\n", + " epochs=5000,\n", + " verbose=1)\n", + "model.summary()" + ], + "metadata": { + "id": "SVa_xfZeEJk-" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "predict = model.predict(X)\n", + "for i in range(len(X)):\n", + " print(int(abs(predict[i]-Y[i])))" + ], + "metadata": { + "id": "cjiiE0kS6aob" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "plt.plot(history.history['loss'])\n", + "#plt.plot(history.history['val_loss'])\n", + "plt.title('Mean Absolute Error')\n", + "plt.ylabel('loss')\n", + "plt.xlabel('epoch')\n", + "plt.legend(['train', 'test'], loc='upper left')\n", + "plt.show()" + ], + "metadata": { + "id": "WisGZSpFJyHr" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#model = keras.models.load_model('path/to/location.keras')" + ], + "metadata": { + "id": "WLCiSMyIH5xG" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "model.save('model.keras')\n", + "def convert(model):\n", + " converter = tf.lite.TFLiteConverter.from_keras_model(model)\n", + " tflite_model = converter.convert()\n", + "\n", + " with open('app_model.tflite', 'wb') as f:\n", + " f.write(tflite_model)\n", + "convert(model)" + ], + "metadata": { + "id": "Yp-jEIQ-PeeG" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file