{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"original: \n",
"\n",
"+ http://qiita.com/niwasawa/items/42c3ed816ddffc14d770\n",
"+ http://qiita.com/niwasawa/items/139311850632a599caa2"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'numo/narray'\n",
"require 'numo/gnuplot'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":step"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ステップ関数\n",
"def step(x)\n",
" x > 0 # Numo::Bit を返す\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":sigmoid"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# シグモイド関数\n",
"def sigmoid(x)\n",
" 1 / (1 + Numo::NMath.exp(-x)) # Numo::DFloat を返す\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":relu"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ReLU (Rectified Linear Unit) 関数\n",
"def relu(x)\n",
" y = Numo::DFloat[x] # コピー\n",
" y[y < 0] = 0 # 0より小さい値の場合は0を代入する\n",
" y\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# データの作成\n",
"x = Numo::DFloat.new(100).seq(-5.0, 0.1)\n",
"y1 = step(x)\n",
"y2 = sigmoid(x)\n",
"y3 = relu(x)\n",
"nil"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# グラフの描画\n",
"g = Numo::noteplot do\n",
" # set term: {png: {size: [480, 640]}} # 画像サイズ\n",
" # set output: 'ruby_graph.png'\n",
" set title: 'Step, Sigmoid, ReLU' # タイトル\n",
" set key: 'box left top'\n",
" set xrange: -5.5...5.5 # x軸の範囲\n",
" set yrange: -0.2...5.2 # y軸の範囲\n",
" set offset: [0, 0, 0, 0]\n",
" plot x, y1, {w: 'lines', lw: 3, title: 'Step'},\n",
" x, y2, {w: 'lines', lw: 3, title: 'Sigmoid'},\n",
" x, y3, {w: 'lines', lw: 3, title: 'ReLU'}\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":init_network"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 重みとバイアスの初期化\n",
"def init_network()\n",
" network = {}\n",
" network['W1'] = Numo::DFloat[[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]]\n",
" network['b1'] = Numo::DFloat[0.1, 0.2, 0.3]\n",
" network['W2'] = Numo::DFloat[[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]]\n",
" network['b2'] = Numo::DFloat[0.1, 0.2]\n",
" network['W3'] = Numo::DFloat[[0.1, 0.3], [0.2, 0.4]]\n",
" network['b3'] = Numo::DFloat[0.1, 0.2]\n",
" network\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":forword"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 入力信号を出力へと変換\n",
"def forword(network, x)\n",
" w1 = network['W1']; w2 = network['W2']; w3 = network['W3']\n",
" b1 = network['b1']; b2 = network['b2']; b3 = network['b3']\n",
" a1 = x.dot(w1) + b1\n",
" z1 = sigmoid(a1)\n",
" a2 = z1.dot(w2) + b2\n",
" z2 = sigmoid(a2)\n",
" a3 = z2.dot(w3) + b3\n",
" identity_function(a3)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":identity_function"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 恒等関数\n",
"def identity_function(x)\n",
" x\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
":sigmoid"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# シグモイド関数\n",
"def sigmoid(x)\n",
" 1 / (1 + Numo::NMath.exp(-x)) # Numo::DFloat を返す\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$\\left(\\begin{array}{cc} 0.3168270764110298 & 0.6962790898619668 \\end{array}\\right)$$"
],
"text/plain": [
"Numo::DFloat#shape=[2]\n",
"[0.316827, 0.696279]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 実行\n",
"network = init_network()\n",
"x = Numo::DFloat[1.0, 0.5] # 入力層\n",
"y = forword(network, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.4.0",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}