{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from mygrad import Layer\n", "from mygrad import Value\n", "import pickle\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def predict(x):\n", " x1 = hiddenLayer1(x) \n", " final = outputLayer([x1] + x)\n", " return final" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from sklearn.metrics import accuracy_score, precision_score, f1_score, recall_score\n", "def getAccuracy(X, Y):\n", " predicted = [1 if predict(x).data > 0.5 else 0 for x in X ]\n", " return accuracy_score(Y, predicted)\n", "def getPrecision(X, Y):\n", " predicted = [1 if predict(x).data > 0.5 else 0 for x in X ]\n", " return precision_score(Y, predicted)\n", "def getf1(X, Y):\n", " predicted = [1 if predict(x).data > 0.5 else 0 for x in X ]\n", " return f1_score(Y, predicted)\n", "def getRecall(X, Y):\n", " predicted = [1 if predict(x).data > 0.5 else 0 for x in X ]\n", " return recall_score(Y, predicted)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Load model\n", "\n", "def loadModel():\n", " neuron1weightsbias, outputneuronweightsbias = [], []\n", " with open(f'parameters/neuron1weightsbias_fn_reLu.pckl', 'rb') as file:\n", " neuron1weightsbias = pickle.load(file)\n", " with open('parameters/outputneuronweightsbias_fn_reLu.pckl', 'rb') as file:\n", " outputneuronweightsbias = pickle.load(file)\n", " hiddenLayer1_ = Layer(10, 1, 'reLu')\n", " outputLayer_ = Layer(11, 1, 'sigmoid')\n", "\n", " hiddenLayer1_.neurons[0].w = [Value(i) for i in neuron1weightsbias[:-1]]\n", " hiddenLayer1_.neurons[0].b = Value(neuron1weightsbias[-1])\n", "\n", " outputLayer_.neurons[0].w = [Value(i) for i in outputneuronweightsbias[:-1]]\n", " outputLayer_.neurons[0].b = Value(outputneuronweightsbias[-1])\n", " return hiddenLayer1_, outputLayer_, neuron1weightsbias, outputneuronweightsbias" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "hiddenLayer1, outputLayer, neuron1weightsbias, outputneuronweightsbias = loadModel()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import pickle\n", "with open('data.pckl', 'rb') as file:\n", " data = pickle.load(file)\n", "from sklearn.utils import shuffle\n", "data = shuffle(data)\n", "X = [list(number) for number in data['number']]\n", "Y = [label for label in data['label']]\n", "for ix, row in enumerate(X):\n", " X[ix] = [Value(float(item)) for item in row]\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9609375" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "getAccuracy(X, Y)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }