Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 70 additions & 17 deletions notebooks/ch04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,49 +228,102 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0.17576655 0.225064 -0.40083055]\n",
" [ 0.26364982 0.337596 -0.60124582]]\n"
"[[ 0.29193618 0.20277891 -0.49471508]\n",
" [ 0.43790426 0.30416836 -0.74207263]]\n",
"Hello Phoenix, this is my forked version 🚀\n"
]
}
],
"source": [
"import sys, os\n",
"sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定\n",
"import sys\n",
"import os\n",
"sys.path.append(os.pardir) # Setting to import files from parent directory\n",
"import numpy as np\n",
"from typing import Union, List, Tuple\n",
"# Remove ndarray from typing import and use numpy's type hints instead\n",
"from common.functions import softmax, cross_entropy_error\n",
"from common.gradient import numerical_gradient\n",
"\n",
"\n",
"class simpleNet:\n",
" def __init__(self):\n",
" self.W = np.random.randn(2,3)\n",
"\n",
" def predict(self, x):\n",
"class SimpleNet:\n",
" \"\"\"\n",
" A simple neural network with one weight matrix.\n",
" \n",
" This class implements a basic neural network that can make predictions\n",
" and calculate loss using softmax activation and cross-entropy error.\n",
" \"\"\"\n",
" \n",
" def __init__(self) -> None:\n",
" \"\"\"\n",
" Initialize the network with random weights.\n",
" \"\"\"\n",
" self.W = np.random.randn(2, 3) # Weight matrix with shape (2, 3)\n",
"\n",
" def predict(self, x: np.ndarray) -> np.ndarray: # Use np.ndarray instead of ndarray\n",
" \"\"\"\n",
" Make a prediction using the network.\n",
" \n",
" Args:\n",
" x (np.ndarray): Input data with shape matching the first dimension of weights\n",
" \n",
" Returns:\n",
" np.ndarray: The prediction result\n",
" \"\"\"\n",
" return np.dot(x, self.W)\n",
"\n",
" def loss(self, x, t):\n",
" def loss(self, x: np.ndarray, t: np.ndarray) -> float: # Use np.ndarray instead of ndarray\n",
" \"\"\"\n",
" Calculate the loss for given input and target.\n",
" \n",
" Args:\n",
" x (np.ndarray): Input data\n",
" t (np.ndarray): Target (correct) labels\n",
" \n",
" Returns:\n",
" float: Cross-entropy loss value\n",
" \"\"\"\n",
" z = self.predict(x)\n",
" y = softmax(z)\n",
" loss = cross_entropy_error(y, t)\n",
"\n",
" \n",
" return loss\n",
"\n",
"\n",
"# Example usage\n",
"x = np.array([0.6, 0.9])\n",
"t = np.array([0, 0, 1])\n",
"\n",
"net = simpleNet()\n",
"net = SimpleNet()\n",
"\n",
"f = lambda w: net.loss(x, t)\n",
"dW = numerical_gradient(f, net.W)\n",
"\n",
"print(dW)"
"print(dW)\n",
"print(\"Hello Phoenix, this is my forked version 🚀\")\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Phoenix, this is my forked version 🚀\n"
]
}
],
"source": [
"print(\"Hello Phoenix, this is my forked version 🚀\")\n"
]
},
{
Expand Down Expand Up @@ -478,9 +531,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "dezero:Python",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-env-dezero-py"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -492,7 +545,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down