{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "intro",
   "metadata": {},
   "source": [
    "# 05　1つのニューロンをのぞく\n",
    "\n",
    "FUJIMOTO LAB 深層学習コース。Web教材の図と説明を読んでから実行してください。Pythonの計算はColabの実行環境で行います。\n",
    "\n",
    "**この回の目標**：y = wx + b の各値の役割を説明できる\n",
    "\n",
    "このノートブックは小さな合成データを使用し、元の教材の固定Driveパスや動画を必要としません。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "predict",
   "metadata": {},
   "source": [
    "## 1　まず予想する\n",
    "\n",
    "コードを実行する前に、表示される値や形を予想してください。"
   ]
  },
  {
   "cell_type": "code",
   "id": "demo-one",
   "metadata": {},
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "layer = nn.Linear(1,1)\n",
    "with torch.no_grad():\n",
    "    layer.weight.fill_(2.0); layer.bias.fill_(1.0)\n",
    "x = torch.tensor([[3.0]])\n",
    "print(layer(x).item())"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "explain-one",
   "metadata": {},
   "source": [
    "**確かめ方**：2×3+1をPyTorchのLinear層が計算します。ここでは値を手で設定しました。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "part-two",
   "metadata": {},
   "source": [
    "## 2　ReLUで表せる形を増やす\n",
    "\n",
    "掛け算と足し算だけを重ねても、表せる関係には限りがあります。途中にReLUのような活性化関数を入れると、直線だけでは表しにくい境界も扱えるようになります。"
   ]
  },
  {
   "cell_type": "code",
   "id": "demo-two",
   "metadata": {},
   "source": [
    "relu = nn.ReLU()\n",
    "print(relu(torch.tensor([-2.0, 3.0])))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "explain-two",
   "metadata": {},
   "source": [
    "**結果を読む**：ReLUは負の値を0にし、正の値をそのまま通します。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "experiment",
   "metadata": {},
   "source": [
    "## 3　値を変えて比べる\n",
    "\n",
    "次のセルでは、表示される値や条件を変えて、何が結果を決めるかを確かめます。長くかかる実験は、少数の合成データで行います。"
   ]
  },
  {
   "cell_type": "code",
   "id": "lab",
   "metadata": {},
   "source": [
    "# 重みを変えたら、同じ入力でも出力はどうなる？\n",
    "with torch.no_grad():\n",
    "    layer.weight.fill_(3.0)\n",
    "print(layer(x).item())  # 3×3+1\n"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "extra-guide-1",
   "metadata": {},
   "source": [
    "## 追加実習 1　手計算とReLUをつなぐ\n",
    "\n",
    "重みが負なら出力はどう変わるでしょう。ReLUの前後を並べてみます。\n",
    "\n",
    "**実行前に予想**：何が変わり、何が変わらないでしょうか。"
   ]
  },
  {
   "cell_type": "code",
   "id": "extra-code-1",
   "metadata": {},
   "source": [
    "with torch.no_grad():\n",
    "    layer.weight.fill_(-2.0)\n",
    "    layer.bias.fill_(1.0)\n",
    "before_relu = layer(x)\n",
    "print('計算結果:', before_relu.item())\n",
    "print('ReLUの後:', relu(before_relu).item())\n",
    "print('手計算: -2×3+1 =', -2*3+1)\n"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "extra-reflect-1",
   "metadata": {},
   "source": [
    "**確認**：予想と違った点を一つ書き、値を一つ変えて再実行してください。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reflection",
   "metadata": {},
   "source": [
    "## 自分の言葉で答えよう\n",
    "\n",
    "x=4、w=2、b=1なら？\n",
    "\n",
    "- まず予想を書く\n",
    "- コードのどの行が答えを決めるか指す\n",
    "- 条件や値を1つ変えて、予想と実行結果を比べる\n",
    "\n",
    "**ヒント**：2×4+1=9です。"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "05-neuron.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
