机器学习与深度学习

https://github.com/gaowanlu/machineLearning-deepLearning

推荐书籍

常用环境准备

PS C:\Users\dmlt> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Wed_Apr__9_19:29:17_Pacific_Daylight_Time_2025
Cuda compilation tools, release 12.9, V12.9.41
Build cuda_12.9.r12.9/compiler.35813241_0
PS C:\Users\dmlt> nvidia-smi
Thu May  8 14:16:12 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 576.02                 Driver Version: 576.02         CUDA Version: 12.9     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                  Driver-Model | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce GTX 1050 Ti   WDDM  |   00000000:01:00.0  On |                  N/A |
| 50%   35C    P8            N/A  /   75W |     865MiB /   4096MiB |     10%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A            4752    C+G   ...8bbwe\PhoneExperienceHost.exe      N/A      |
|    0   N/A  N/A            4788    C+G   D:\vscode-portable\app\Code.exe       N/A      |
|    0   N/A  N/A            5456    C+G   ...App_cw5n1h2txyewy\LockApp.exe      N/A      |
|    0   N/A  N/A            5612    C+G   ...5n1h2txyewy\TextInputHost.exe      N/A      |
|    0   N/A  N/A            6716    C+G   D:\Feishu\app\Feishu.exe              N/A      |
|    0   N/A  N/A            8744    C+G   ...yb3d8bbwe\WindowsTerminal.exe      N/A      |
|    0   N/A  N/A           10300    C+G   C:\Windows\explorer.exe               N/A      |
|    0   N/A  N/A           11624    C+G   ...h_cw5n1h2txyewy\SearchApp.exe      N/A      |
|    0   N/A  N/A           12472    C+G   ...xyewy\ShellExperienceHost.exe      N/A      |
|    0   N/A  N/A           13432    C+G   ...t\Edge\Application\msedge.exe      N/A      |
|    0   N/A  N/A           13780    C+G   D:\Microsoft VS Code\Code.exe         N/A      |
|    0   N/A  N/A           14804    C+G   ...ram Files\Listary\Listary.exe      N/A      |
|    0   N/A  N/A           15188    C+G   D:\CloudMusic\cloudmusic.exe          N/A      |
|    0   N/A  N/A           15404    C+G   ...k\current\Client\CorpLink.exe      N/A      |
|    0   N/A  N/A           20272    C+G   ...4__8wekyb3d8bbwe\Video.UI.exe      N/A      |
|    0   N/A  N/A           22848    C+G   ...Browser\Application\brave.exe      N/A      |
|    0   N/A  N/A           24104    C+G   ...ntrolPanel\SystemSettings.exe      N/A      |
+-----------------------------------------------------------------------------------------+
PS C:\Users\dmlt>
pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
# GPU支持的pytorch
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple --index-url https://download.pytorch.org/whl/cu118
import torch
import sys
import torch.version
import torch.cuda

print(torch.version.cuda)

if torch.version.cuda == None:
    print("请卸载torch重新安装GPU版本")

def test_pytorch_gpu():
    # 打印PyTorch版本信息
    print(f"PyTorch版本: {torch.__version__}")
    
    # 检查CUDA是否可用
    cuda_available = torch.cuda.is_available()
    print(f"CUDA是否可用: {cuda_available}")
    
    if cuda_available:
        # 打印可用的GPU数量
        gpu_count = torch.cuda.device_count()
        print(f"可用GPU数量: {gpu_count}")
        
        # 打印当前GPU设备名称
        current_device = torch.cuda.current_device()
        print(f"当前GPU设备ID: {current_device}")
        print(f"当前GPU设备名称: {torch.cuda.get_device_name(current_device)}")
        
        # 测试简单的CUDA操作
        print("\n测试CUDA张量操作...")
        x = torch.rand(5, 3).cuda()
        y = torch.rand(5, 3).cuda()
        z = x + y
        print(f"CUDA张量操作成功,结果形状: {z.shape}")
        
        # 测试更复杂的操作(矩阵乘法)
        a = torch.rand(1000, 1000).cuda()
        b = torch.rand(1000, 1000).cuda()
        print("执行GPU上的大矩阵乘法...")
        c = torch.matmul(a, b)
        print(f"矩阵乘法完成,结果形状: {c.shape}")
        
        print("\nGPU测试成功!✅")
    else:
        print("\n⚠️ 无法检测到GPU或CUDA不可用")
        print("请检查:")
        print("1. 您的计算机是否配备了NVIDIA GPU")
        print("2. NVIDIA驱动程序是否正确安装")
        print("3. CUDA工具包是否正确安装")
        print("4. PyTorch是否安装了支持CUDA的版本")

if __name__ == "__main__":
    print("==== PyTorch GPU 测试 ====")
    test_pytorch_gpu()
    print("==== 测试完成 ====")

目录