File size: 3,911 Bytes
c92f2e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
 "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
}