1. 官網(wǎng)安裝Python 3.9
Python Release Python 3.9.0 | Python.org
2. 安裝pycharm
https://download.jetbrains.com/python/pycharm-professional-2023.3.2.exe
3. 安裝miniconda
Miniconda — miniconda documentation
4. 安裝完miniconda 創(chuàng)建虛擬環(huán)境
conda create -n wave2 python=3.9
conda activate wave2
6. 安裝依賴包
pip install numpy
pip show numpy
顯示c:\users\xx\.conda\envs\wave2\lib\site-packages
7. Python安裝cuda 參考資料
Python+PyCharm+PyTorch+Cuda/GPU 安裝步驟_python 中使用pycharam 使安裝cuda-CSDN博客
nvidia-smi
有Nvidia顯卡,想用GPU跑程序時
先查版本!
(1) 確定顯卡支持的cuda版本
在cmd命令行中輸入命令:“nvidia-smi”回車(如提示命令不可用,可先執(zhí)行命令:cd C:\Program Files\NVIDIA Corporation\NVSMI)
NVIDIA-SMI 546.01 ? ? ? ? ? ? ? ?
Driver Version: 546.01 ? ? ?
CUDA Version: 12.3文章來源:http://www.zghlxwxcb.cn/news/detail-783493.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-783493.html
import torch
import time
def simulate_gpu_load_torch(usage_percentage, duration):
# 檢查是否有可用的GPU,如果沒有,將使用CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 模擬 GPU 計算任務(wù)
data = torch.rand((1000, 1000), device=device)
result = torch.matmul(data, data)
# 控制 GPU 使用率
busy_time = duration * (usage_percentage / 100)
idle_time = duration - busy_time
start_time = time.time()
while (time.time() - start_time) < duration:
result = torch.matmul(data, data)
time.sleep(busy_time)
# 空閑時間不進行計算
time.sleep(idle_time)
# 控制 GPU 使用率為50%,持續(xù)10秒
simulate_gpu_load_torch(50, 10)
from ast import Break
import torch
import torch.nn as nn
import os
torch.backends.cudnn.benchmark = False # 關(guān)閉 CuDNN 的 benchmark 模式
torch.backends.cudnn.deterministic = True # 開啟 deterministic 模式
torch.cuda.empty_cache() # 清理未使用的 GPU 內(nèi)存
# 設(shè)置設(shè)備
device = torch.device('cuda')
global memory_used
# 模型
# model = nn.Sequential(
# nn.Linear(1024, 2048),
# nn.ReLU(),
# nn.Linear(2048, 4096), # 更多神經(jīng)元
# nn.ReLU(),
# nn.Linear(4096, 8192), # 更多神經(jīng)元
# nn.ReLU(),
# nn.Linear(8192, 4096), # 更多神經(jīng)元
# nn.ReLU(),
# nn.Linear(4096, 2048), # 更多神經(jīng)元
# nn.ReLU(),
# nn.Linear(2048, 1024)
# ).to(device)
model = nn.Sequential(
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 1024),
nn.ReLU(),
nn.Linear(1024, 2048), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(2048, 4096), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(4096, 2048), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(2048, 4096), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(4096, 2048), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(2048, 4096), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(4096, 2048), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(2048, 1024), # 更多神經(jīng)元
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 1024)
).to(device)
# 優(yōu)化器
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
# 設(shè)置最大內(nèi)存
max_memory = 4 * 1024 * 1024 * 1024
#max_memory = 4096
# 初始參數(shù)
batch_size = 1024
# batch_size =512
memory_used = 0
#torch.cuda.empty_cache() # 關(guān)閉內(nèi)存回收
#while memory_used < max_memory:
totalmemory_used =0
while True:
# 生成數(shù)據(jù)
#count +=1
#inputs = torch.rand(batch_size, 1024).to(device)
inputs = torch.rand(batch_size, 1024).to(device)
# 前向傳播
outputs = model(inputs)
# 反向傳播和優(yōu)化
loss = outputs.mean()
loss.backward()
optimizer.step()
device = torch.device('cuda') # 或者 'cuda:0',如果有多個GPU
# 獲取當(dāng)前GPU的顯存占用量
memory_used = torch.cuda.memory_allocated(device)
# 將字節(jié)轉(zhuǎn)換為兆字節(jié)(MB)
memory_used_mb = memory_used / (1024 * 1024)
totalmemory_used += memory_used_mb
print(totalmemory_used)
torch.cuda.empty_cache()
# if count == 10:
# break
# 更新內(nèi)存和batchsize
#memory_used += 1024 * 1024 * 1024
# if int(torch.cuda.memory_allocated(device)) >= max_memory:
# break
os.system('nvidia-smi')
#if totalmemory_used > 4096:
# break
#batch_size += 1024
到了這里,關(guān)于Windows下Python+PyCharm+miniconda+Cuda/GPU 安裝步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!