{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'numo/narray'" ] }, { "cell_type": "code", "execution_count": 2, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "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": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":mean_squared_error" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2乗和誤差\n", "def mean_squared_error(y, t)\n", " # ニューラルネットワークの出力と教師データの各要素の差の2乗、の総和\n", " return 0.5 * ((y-t)**2).sum\n", "end" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.09750000000000003" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_squared_error(Numo::DFloat.asarray(y), Numo::DFloat.asarray(t))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "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": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5974999999999999" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean_squared_error(Numo::DFloat.asarray(y2), Numo::DFloat.asarray(t))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":cross_entropy_error" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 交差エントロピー誤差\n", "def cross_entropy_error(y, t)\n", " delta = 1e-7 # マイナス無限大を発生させないように微小な値を追加する\n", " return -(t * Numo::NMath.log(y + delta)).sum\n", "end" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.510825457099338" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(Numo::DFloat.asarray(y), Numo::DFloat.asarray(t))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.302584092994546" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(Numo::DFloat.asarray(y2), Numo::DFloat.asarray(t))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require_relative 'dataset/mnist'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[, , , ]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train, t_train, x_test, t_test = MNIST.load_mnist(\n", " normalize: true, one_hot_label: true)\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[60000, 784]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train.shape" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[60000, 10]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_train.shape" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Numo::SFloat#shape=[10,784]\n", "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]], Numo::UInt8#shape=[10,10]\n", "[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0], \n", " [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], \n", " [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], \n", " [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], \n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], \n", " [0, 0, 1, 0, 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, 1, 0, 0, 0, 0, 0]]]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_batch = x_train[0...10]\n", "t_batch = t_train[0...10]\n", "[x_batch, t_batch]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":cross_entropy_error" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "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", " end\n", " delta = 1e-7\n", " batch_size = y.shape[0]\n", " return -(t * Numo::NMath.log(y + delta)).sum / batch_size\n", "end" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.510825457099338" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cross_entropy_error(Numo::DFloat.asarray(y), Numo::DFloat.asarray(t))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":numerical_diff" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def numerical_diff(f, x)\n", " h = 1e-4\n", " # return (f(x+h) - f(x-h)) / (2*h)\n", " return (f.(x+h) - f.(x-h)) / (2*h)\n", "end" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# def function_1(x)\n", "function_1 = -> x do\n", " 0.01*x**2 + 0.1*x\n", "end" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1999999999990898" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_1, 5)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.2999999999986347" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_1, 10)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":function_2" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def function_2(x0, x1)\n", " return x0**2 + x1**2\n", "end" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function_tmp1 = -> x0 do\n", " function_2(x0, 4.0)\n", "end" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.00000000000378" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numerical_diff(function_tmp1, 3.0)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function_tmp2 = -> x1 do\n", " function_2(3.0, x1)\n", "end" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.999999999999119" ] }, "execution_count": 26, "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": "Ruby 2.4.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.1" } }, "nbformat": 4, "nbformat_minor": 2 }