routine
This commit is contained in:
		
							
								
								
									
										83
									
								
								lab/6_MLP-pt.ipynb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								lab/6_MLP-pt.ipynb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 4,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "MultiLayerPerceptron(\n",
 | 
			
		||||
      "  (hidden_layer1): Linear(in_features=10, out_features=20, bias=True)\n",
 | 
			
		||||
      "  (hidden_layer2): Linear(in_features=20, out_features=10, bias=True)\n",
 | 
			
		||||
      "  (output_layer): Linear(in_features=10, out_features=2, bias=True)\n",
 | 
			
		||||
      "  (activation1): ReLU()\n",
 | 
			
		||||
      "  (activation2): Sigmoid()\n",
 | 
			
		||||
      ")\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "import torch\n",
 | 
			
		||||
    "import torch.nn as nn\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 设置使用gpu7\n",
 | 
			
		||||
    "device = torch.device(\"cuda:7\" if torch.cuda.is_available() else \"cpu\")\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 定义一个简单的神经元层\n",
 | 
			
		||||
    "class MultiLayerPerceptron(nn.Module):\n",
 | 
			
		||||
    "    def __init__(self, input_size, hidden_size1, hidden_size2, output_size):\n",
 | 
			
		||||
    "        super(MultiLayerPerceptron, self).__init__()\n",
 | 
			
		||||
    "        self.hidden_layer1 = nn.Linear(input_size, hidden_size1)\n",
 | 
			
		||||
    "        self.hidden_layer2 = nn.Linear(hidden_size1, hidden_size2)\n",
 | 
			
		||||
    "        self.output_layer = nn.Linear(hidden_size2, output_size)\n",
 | 
			
		||||
    "        \n",
 | 
			
		||||
    "        # 定义不同的激活函数\n",
 | 
			
		||||
    "        self.activation1 = nn.ReLU()  # 第一个隐藏层使用 ReLU\n",
 | 
			
		||||
    "        self.activation2 = nn.Sigmoid()  # 第二个隐藏层使用 Sigmoid\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "    def forward(self, x):\n",
 | 
			
		||||
    "        # 第一个隐藏层及其激活函数\n",
 | 
			
		||||
    "        x = self.hidden_layer1(x)\n",
 | 
			
		||||
    "        x = self.activation1(x)\n",
 | 
			
		||||
    "        \n",
 | 
			
		||||
    "        # 第二个隐藏层及其激活函数\n",
 | 
			
		||||
    "        x = self.hidden_layer2(x)\n",
 | 
			
		||||
    "        x = self.activation2(x)\n",
 | 
			
		||||
    "        \n",
 | 
			
		||||
    "        # 输出层\n",
 | 
			
		||||
    "        x = self.output_layer(x)\n",
 | 
			
		||||
    "        return x\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 创建一个MLP实例\n",
 | 
			
		||||
    "mlp = MultiLayerPerceptron(input_size=10, hidden_size1=20, hidden_size2=10, output_size=2)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 打印模型结构\n",
 | 
			
		||||
    "print(mlp)\n"
 | 
			
		||||
   ]
 | 
			
		||||
  }
 | 
			
		||||
 ],
 | 
			
		||||
 "metadata": {
 | 
			
		||||
  "kernelspec": {
 | 
			
		||||
   "display_name": "ail",
 | 
			
		||||
   "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.11.9"
 | 
			
		||||
  }
 | 
			
		||||
 },
 | 
			
		||||
 "nbformat": 4,
 | 
			
		||||
 "nbformat_minor": 2
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user