{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def mean_square_error(y, t):\n", " return 0.5 * np.sum((y - t) ** 2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.097500000000000031" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_square_error(np.array(y), np.array(t))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y2 = [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.59750000000000003" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_square_error(np.array(y2), np.array(t))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def cross_entropy_error(y, t):\n", " delta = 1e-7\n", " return -np.sum(t * np.log(y + delta))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.51082545709933802" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(np.array(y), np.array(t))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.3025840929945458" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(np.array(y2), np.array(t))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from dataset.mnist import load_mnist" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "(x_train, t_train), (x_test, t_test) = \\\n", " load_mnist(normalize=True, one_hot_label=True)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(60000, 784)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train.shape" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(60000, 10)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_train.shape" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "train_size = x_train.shape[0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "batch_size = 10" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "batch_mask = np.random.choice(train_size, batch_size)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "x_batch = x_train[batch_mask]\n", "t_batch = t_train[batch_mask]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0., ..., 0., 0., 0.],\n", " [ 0., 0., 0., ..., 0., 0., 0.],\n", " [ 0., 0., 0., ..., 0., 0., 0.],\n", " ..., \n", " [ 0., 0., 0., ..., 0., 0., 0.],\n", " [ 0., 0., 0., ..., 0., 0., 0.],\n", " [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_batch" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", " [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n", " [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n", " [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_batch" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "def cross_entropy_error(y, t):\n", " if y.ndim == 1:\n", " t = t.reshape(1, t.size)\n", " y = y.reshape(1, y.size)\n", "\n", " delta = 1e-7\n", " batch_size = y.shape[0]\n", " return -np.sum(t * np.log(y + delta)) / batch_size" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.51082545709933802" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(np.array(y), np.array(t))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def numerical_diff(f, x):\n", " h = 1e-4\n", " return (f(x+h) - f(x-h)) / (2*h)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def function_1(x):\n", " return 0.01*x**2 + 0.1*x" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1999999999990898" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_1, 5)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.2999999999986347" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_1, 10)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def function_2(x0, x1):\n", " return x0**2 + x1**2" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def function_tmp1(x0):\n", " return function_2(x0, 4.0)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.00000000000378" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_tmp1, 3.0)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def function_tmp2(x1):\n", " return function_2(3.0, x1)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.999999999999119" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_tmp2, 4.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }