pytorch-stuff/Numpy_NN.ipynb

197 lines
40 KiB
Plaintext
Raw Normal View History

2020-04-27 19:53:55 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import sklearn.datasets"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"X,y = sklearn.datasets.make_moons(200,noise=0.15)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x11f466f90>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOydeZzN5RfH38/37nd2xposKSIlOxVKIkWUkpRUWkhJqxItPyVKRVlalBYhiuxKtNh3yb6vYYwZs939fp/fH3cMd+69Y8bcuTPM9/16edV8l+d7LnfO93nOc87nCCklGhoaGhqXPkpxG6ChoaGhERk0h6+hoaFRStAcvoaGhkYpQXP4GhoaGqUEzeFraGholBL0xW1AKBITE2X16tWL2wwNDQ2Ni4r169cnSynLBTtXYh1+9erVWbduXXGboaGhoXFRIYQ4GOqcFtLR0NDQKCVoDl9DQ0OjlKA5fA0NDY1SgubwNTQ0NEoJmsPXiBiHdhzl36XbsWfai9sUDY1SSYnN0tG4dDh1LJUhnd7j0I6j6PQ6vG4vjw17gHue61jcpmlolCq0Gb5GkfNG5xHs23wQ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8ojbo1E6CMsMX0r5txCieh6XdAa+kz6PsUoIES+EqCSl1PQCQtCgTT32btqP2+mfieJ2uqlxXdWg91zd9MqgmSsmi5Eb726a5/Ma3XYd+zYfDIgp2zMdDO40HCEgoWI8N3e7kVljF+C0+WbK/y7dzqvthjL818HUu6lOwLgHtx/hxZvfzMlESTmWyrdvTSP56Cn6fvwo6xdtDion4XF7mD/hd/qM7JWn3flFSgkCckd
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(X[:,0],X[:,1],c=y)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(200, 2)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X.shape"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Hyperparameters\n",
"input_neurons = 2\n",
"output_neurons = 2\n",
"samples = X.shape[0]\n",
"learning_rate = 0.001\n",
"lambda_reg = 0.01\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_dic = {'W1': W1, 'b1': b1,'W2': W2, 'b2': b2}"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def retrieve(model_dict):\n",
" W1 = model_dic['W1']\n",
" b1 = model_dic['b1']\n",
" W2 = model_dic['W2']\n",
" b2 = model_dic['b2']\n",
" return W1,b1,W2,b2\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def forward(x, model_dict):\n",
" W1,b1,W2,b2 = retrieve(model_dict)\n",
" z1 = x.dot(W1) + b1\n",
" a1 = np.tanh(z1)\n",
" z2 = a1.dot(W2) + b2\n",
" a2 = np.tanh(z2)\n",
" exp_scores = np.exp(a2)\n",
" softmax = exp_scores / np.sum(exp_scores, dim=1, keepdims=True)\n",
" return softmax\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def loss(softmax, y):\n",
" W1,b1,W2,b2 = retrieve(model_dict)\n",
" m = np.zeros(200)\n",
" for i,correct_index in enumerate(y):\n",
" predicted = softmax[i][correct_index]\n",
" m[i] = predicted\n",
" log_prob = -np.log(predicted)\n",
" softmax_loss = np.sum(log_prob)\n",
" reg_loss = lambda_reg / 2*(np.sum(np.square(W1)) + np.sum(np.square(W2)))\n",
" loss = softmax_loss + reg_loss\n",
" return float(loss/y.shape[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"softmax = np.random.randn(200,2)\n"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}