routine
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -7,3 +7,5 @@
 | 
			
		||||
.idea
 | 
			
		||||
 | 
			
		||||
**/.ipynb_checkpoints
 | 
			
		||||
 | 
			
		||||
.vscode
 | 
			
		||||
							
								
								
									
										41
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								README.md
									
									
									
									
									
								
							@@ -1,47 +1,30 @@
 | 
			
		||||
# pytorch study
 | 
			
		||||
# AI-Learning
 | 
			
		||||
 | 
			
		||||
## BASE ENV
 | 
			
		||||
```shell
 | 
			
		||||
conda create -n pt python=3.10 -y
 | 
			
		||||
 | 
			
		||||
conda activate pt
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## MAC
 | 
			
		||||
```shell
 | 
			
		||||
conda create -n ail python=3.10 -y
 | 
			
		||||
conda activate ail
 | 
			
		||||
# 安装 pytorch v1.12版本已经正式支持了用于mac m1芯片gpu加速的mps后端
 | 
			
		||||
conda install pytorch::pytorch torchvision torchaudio -c pytorch -y
 | 
			
		||||
 | 
			
		||||
# tensorflow mac apple silicon
 | 
			
		||||
pip install tensorflow-macos
 | 
			
		||||
pip install tensorflow-metal
 | 
			
		||||
 | 
			
		||||
pip install -r requirements.txt
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## linux
 | 
			
		||||
```shell
 | 
			
		||||
conda create -n ail-tf python=3.9 -y
 | 
			
		||||
conda create -n ail-pt python=3.10 -y
 | 
			
		||||
 | 
			
		||||
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
 | 
			
		||||
# tensorflow 需要cudnn支持
 | 
			
		||||
pip install tensorflow
 | 
			
		||||
 | 
			
		||||
pip install -r requirements.txt
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Jupyter
 | 
			
		||||
```shell
 | 
			
		||||
# env
 | 
			
		||||
pip install jupyter
 | 
			
		||||
```
 | 
			
		||||
```shell
 | 
			
		||||
jupyter notebook
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## tensorflow
 | 
			
		||||
### python
 | 
			
		||||
```shell
 | 
			
		||||
# win and linux
 | 
			
		||||
pip install tensorflow
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
#mac apple silicon
 | 
			
		||||
pip install tensorflow-macos
 | 
			
		||||
pip install tensorflow-metal
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### c++
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										395
									
								
								lab/5_MLP-tf.ipynb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										395
									
								
								lab/5_MLP-tf.ipynb
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										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
 | 
			
		||||
}
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 493 KiB After Width: | Height: | Size: 493 KiB  | 
@@ -3,5 +3,5 @@ pandas
 | 
			
		||||
pillow
 | 
			
		||||
matplotlib
 | 
			
		||||
ipywidgets
 | 
			
		||||
tensorflow
 | 
			
		||||
jupyter
 | 
			
		||||
scikit-learn
 | 
			
		||||
							
								
								
									
										3
									
								
								tf/docker-run.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								tf/docker-run.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
# nvcr.io/nvidia/tensorflow:24.12-tf2-py3
 | 
			
		||||
docker run -itd --gpus all --name ail-tf -v /data/szh2/Project/AI-learning:/workspace/ail -p 58888:8888 nvcr.io/nvidia/tensorflow:24.12-tf2-py3 jupyter notebook --NotebookApp.token='12345678'
 | 
			
		||||
							
								
								
									
										112
									
								
								tf/gpu.ipynb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								tf/gpu.ipynb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 1,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stderr",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "2025-01-09 10:12:54.526706: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
 | 
			
		||||
      "2025-01-09 10:12:54.541661: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
 | 
			
		||||
      "2025-01-09 10:12:54.560110: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8473] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
 | 
			
		||||
      "2025-01-09 10:12:54.565525: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1471] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
 | 
			
		||||
      "2025-01-09 10:12:54.578678: I tensorflow/core/platform/cpu_feature_guard.cc:211] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
 | 
			
		||||
      "To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "b'Hello, TensorFlow!'\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stderr",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "2025-01-09 10:12:58.748603: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1143 MB memory:  -> device: 0, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:35:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.751140: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 78412 MB memory:  -> device: 1, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:36:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.752975: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:2 with 78412 MB memory:  -> device: 2, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:39:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.754821: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:3 with 78412 MB memory:  -> device: 3, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:3d:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.756707: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:4 with 78412 MB memory:  -> device: 4, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:9c:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.758405: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:5 with 78412 MB memory:  -> device: 5, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:9d:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.760115: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:6 with 78412 MB memory:  -> device: 6, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:a0:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:12:58.761971: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:7 with 77296 MB memory:  -> device: 7, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:a4:00.0, compute capability: 8.0\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "import tensorflow as tf\n",
 | 
			
		||||
    "# 创建一个TensorFlow常量\n",
 | 
			
		||||
    "hello = tf.constant('Hello, TensorFlow!')\n",
 | 
			
		||||
    "# 将TensorFlow张量转换为NumPy数组\n",
 | 
			
		||||
    "numpy_array = hello.numpy()\n",
 | 
			
		||||
    "print(numpy_array)  # 输出: b'Hello, TensorFlow!'"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 2,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "WARNING:tensorflow:From /tmp/ipykernel_137363/2801978163.py:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.\n",
 | 
			
		||||
      "Instructions for updating:\n",
 | 
			
		||||
      "Use `tf.config.list_physical_devices('GPU')` instead.\n",
 | 
			
		||||
      "True\n",
 | 
			
		||||
      "[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:3', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:4', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:5', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:6', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:7', device_type='GPU')] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stderr",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "2025-01-09 10:13:03.154945: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:0 with 1143 MB memory:  -> device: 0, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:35:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.156613: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:1 with 78412 MB memory:  -> device: 1, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:36:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.158071: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:2 with 78412 MB memory:  -> device: 2, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:39:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.159472: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:3 with 78412 MB memory:  -> device: 3, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:3d:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.161049: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:4 with 78412 MB memory:  -> device: 4, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:9c:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.162507: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:5 with 78412 MB memory:  -> device: 5, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:9d:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.163784: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:6 with 78412 MB memory:  -> device: 6, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:a0:00.0, compute capability: 8.0\n",
 | 
			
		||||
      "2025-01-09 10:13:03.165104: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /device:GPU:7 with 77296 MB memory:  -> device: 7, name: NVIDIA A800 80GB PCIe, pci bus id: 0000:a4:00.0, compute capability: 8.0\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "import tensorflow as tf\n",
 | 
			
		||||
    "print(tf.test.is_gpu_available())\n",
 | 
			
		||||
    "gpus = tf.config.experimental.list_physical_devices(device_type='GPU')\n",
 | 
			
		||||
    "cpus = tf.config.experimental.list_physical_devices(device_type='CPU')\n",
 | 
			
		||||
    "print(gpus, cpus)"
 | 
			
		||||
   ]
 | 
			
		||||
  }
 | 
			
		||||
 ],
 | 
			
		||||
 "metadata": {
 | 
			
		||||
  "kernelspec": {
 | 
			
		||||
   "display_name": "Python 3 (ipykernel)",
 | 
			
		||||
   "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.12.3"
 | 
			
		||||
  }
 | 
			
		||||
 },
 | 
			
		||||
 "nbformat": 4,
 | 
			
		||||
 "nbformat_minor": 2
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								tf/requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								tf/requirements.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
matplotlib
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
import tensorflow as tf
 | 
			
		||||
 | 
			
		||||
# 创建一个TensorFlow常量
 | 
			
		||||
hello = tf.constant('Hello, TensorFlow!')
 | 
			
		||||
 | 
			
		||||
# 将TensorFlow张量转换为NumPy数组
 | 
			
		||||
numpy_array = hello.numpy()
 | 
			
		||||
 | 
			
		||||
print(numpy_array)  # 输出: b'Hello, TensorFlow!'
 | 
			
		||||
		Reference in New Issue
	
	Block a user