系列文章:
1.ChatGPT 給我的 AI 自學計畫與落實情況(一)
2.ChatGPT 給我的 AI 自學計畫與落實情況(二)
3.ChatGPT 給我的 AI 自學計畫與落實情況(三)
4.ChatGPT 給我的 AI 自學計畫與落實情況(四)
上次用家裡的電腦上網用 ChatGPT 來學 AI,連續學四小時後將紀錄寫在1.ChatGPT 給我的 AI 自學計畫與落實情況(一)、2.ChatGPT 給我的 AI 自學計畫與落實情況(二)、3.ChatGPT 給我的 AI 自學計畫與落實情況(三)與 4.ChatGPT 給我的 AI 自學計畫與落實情況(四)。由於記憶功能的 ChatGPT 需要登入帳號,或是不關機的情況下持續使用。現在在不關機的情況下持續使用,因此重新記錄自我學習計畫與落實。
這個是 Prompt:
我的 AI 學習條件:程式程度:懂一點 Python;學習目標:深入研究機器學習與深度學習;每天時間:每天 1~2 小時;作業系統:Windows 10。請依照我的 AMD Ryzen 7 3700X、GTX 1660 6GB、24GB RAM,幫我制定從零開始的實際學習路線,包含軟體安裝、每天練習與第一個專案。
一、建立專屬 AI 專案資料夾: D:\AI_Research\ │ ├── 01_Python_Basics\ ├── 02_Mathematics\ ├── 03_Machine_Learning\ ├── 04_Deep_Learning\ ├── 05_CNN\ ├── 06_Transformer\ ├── datasets\ ├── models\ └── projects\ 二、安裝 Python 套件:
1.D槽 建立資料夾 AI_Researchpip install scipy scikit-learn jupyterlab
mkdir D:\AI_Research
2.進入 D槽資料夾 AI_Research cd /d D:\AI_Research 3.建立 python 虛擬環境 ai-env python -m venv ai-env 4.啟動 python 虛擬環境 ai-env ai-env\Scripts\activate 5.更新 pip python -m pip install --upgrade pip
6.安裝 python 基礎套件
pip install numpy pandas matplotlib seaborn
7.針對 GTX 1660,建議先使用一個固定且可驗證的 CUDA 12.6 套件組合:
pip install torch==2.7.1 torchvision==0.22.1 --index-url https://download.pytorch.org/whl/cu126
三、確認顯示卡是否成功啟用
檔案名稱:check_gpu.py
檔案內容:import torchprint("PyTorch:", torch.__version__)print("CUDA:", torch.cuda.is_available())if torch.cuda.is_available():print("GPU:", torch.cuda.get_device_name(0))print("CUDA Version:", torch.version.cuda)
結果:
目前的工作環境:第一堂:認識 Tensor 與 GPU 運算
1.安裝與確認 JupyterLab在目前的(ai-env)環境中執行:
pip install jupyterlab
接著啟動:
jupyter lab
瀏覽器會開啟 JupyterLab。2. 建立第一個 Tensor 程式
建立 Notebook,名稱:lesson01_tensor.ipynb
輸入:import torch# 建立一個矩陣x = torch.tensor([[1, 2, 3],[4, 5, 6]])print("Tensor:")print(x)print("Shape:", x.shape)print("Data type:", x.dtype)3. 把 Tensor 放到 GTX 1660
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")x = torch.randn(1000, 1000).to(device)print("Device:", x.device)print("Shape:", x.shape)4. 讓 GTX 1660 執行矩陣運算
import torchimport timedevice = "cuda"a = torch.randn(2000, 2000, device=device)b = torch.randn(2000, 2000, device=device)torch.cuda.synchronize()start = time.time()c = torch.matmul(a, b)torch.cuda.synchronize()end = time.time()print("運算完成")print("GPU:", torch.cuda.get_device_name(0))print("結果尺寸:", c.shape)print("執行時間:", end - start)第二堂課:Tensor 矩陣運算
接下來我們要學習深度學習非常重要的概念:矩陣乘法。
神經網路的核心運算之一就是:其中:
X:輸入資料
W:模型權重(Weight)
b:偏差值(Bias)
Y:運算結果
1. 實際操作矩陣乘法
檔案名稱:lesson02_matrix.py檔案內容:import torchdevice = "cuda"# 建立兩個矩陣A = torch.randn(2000, 2000, device=device)B = torch.randn(2000, 2000, device=device)# 矩陣乘法C = torch.matmul(A, B)print("GPU:", C.device)print("A:", A.shape)print("B:", B.shape)print("C:", C.shape)2. 理解矩陣乘法
這個概念非常重要,因為未來建立神經網路時,每一層都會使用類似的運算。三、下一個實驗:CPU 與 GPU 效能比較
先做一個很有研究價值的實驗:同樣的矩陣,CPU 和 GPU 誰比較快?檔案名稱:lesson02_cpu_gpu.py
檔案內容:import torchimport timeN = 3000# CPU 矩陣a_cpu = torch.randn(N, N)b_cpu = torch.randn(N, N)# CPU 運算start = time.perf_counter()c_cpu = torch.matmul(a_cpu, b_cpu)cpu_time = time.perf_counter() - start# GPU 矩陣a_gpu = a_cpu.to("cuda")b_gpu = b_cpu.to("cuda")torch.cuda.synchronize()# GPU 運算start = time.perf_counter()c_gpu = torch.matmul(a_gpu, b_gpu)torch.cuda.synchronize()gpu_time = time.perf_counter() - start# 比較結果:先將 GPU 結果移回 CPUc_gpu_cpu = c_gpu.cpu()print("CPU 時間:", cpu_time)print("GPU 時間:", gpu_time)print("CPU / GPU 加速比:", cpu_time / gpu_time)print("結果是否接近:",torch.allclose(c_cpu,c_gpu_cpu,atol=1e-2,rtol=1e-2))























