原 国内那些免费的CPU训练平台都是挂羊头卖狗肉
配置听着高的吓人,用测试用的训练代码一测试,数据低的离谱
# train_simple.py
$ .....
# 训练的代码
# 优化器(后面会重建)
optimizer = None
# -----------------------------
# 3. 阶段1:CPU 训练
# -----------------------------
print("? 开始 CPU 训练...")
cpu_start_time = time.time()
# 使用 CPU
model_cpu = UNet2DModel(
sample_size=32,
in_channels=3,
out_channels=3,
layers_per_block=1,
block_out_channels=(32, 64),
down_block_types=("DownBlock2D", "AttnDownBlock2D"),
up_block_types=("UpBlock2D", "AttnUpBlock2D"),
)
model_cpu.train()
model_cpu = model_cpu.to("cpu")
optimizer = torch.optim.Adam(model_cpu.parameters(), lr=1e-3)
n_steps = 10
for step in range(n_steps):
optimizer.zero_grad()
output = model_cpu(x_cpu, timesteps_cpu).sample
loss = torch.nn.functional.mse_loss(output, y_cpu)
loss.backward()
optimizer.step()
print(f"Step {step+1:2d} | Device: CPU | Loss: {loss.item():.6f}")
cpu_end_time = time.time()
cpu_elapsed = cpu_end_time - cpu_start_time
print(f"⏱️ CPU 训练耗时: {cpu_elapsed:.2f} 秒")
# -----------------------------
# 4. 阶段2:GPU 训练(如果可用)
# -----------------------------
gpu_elapsed = 0.0
if torch.cuda.is_available():
print("? 开始 GPU 训练...")
gpu_start_time = time.time()
model_gpu = UNet2DModel(
sample_size=32,
in_channels=3,
out_channels=3,
layers_per_block=1,
block_out_channels=(32, 64),
down_block_types=("DownBlock2D", "AttnDownBlock2D"),
up_block_types=("UpBlock2D", "AttnUpBlock2D"),
)
model_gpu.train()
device = torch.device("cuda")
model_gpu = model_gpu.to(device)
x_gpu = x_cpu.to(device)
y_gpu = y_cpu.to(device)
timesteps_gpu = timesteps_cpu.to(device)
optimizer = torch.optim.Adam(model_gpu.parameters(), lr=1e-3)
for step in range(n_steps):
optimizer.zero_grad()
output = model_gpu(x_gpu, timesteps_gpu).sample
loss = torch.nn.functional.mse_loss(output, y_gpu)
loss.backward()
optimizer.step()
print(f"Step {step+1:2d} | Device: GPU | Loss: {loss.item():.6f}")
gpu_end_time = time.time()
gpu_elapsed = gpu_end_time - gpu_start_time
print(f"⏱️ GPU 训练耗时: {gpu_elapsed:.2f} 秒")
else:
print("⚠️ GPU 不可用,跳过 GPU 训练阶段")
# .......20250903 1357 ...
浏览更多内容请先登录。
立即注册
更新于:2025-09-03 14:04:37
推荐内容
如何注册Spotify,注册中遇到的问题
起因是找阿特拉斯耸耸肩3里片尾的歌曲,后面用谷歌插件 aha music找到了歌曲名字和作曲人:The Beginning Elia Cmiral,然后资料在spotify有,于是就注册,甚至通过远程服务器,在服务器上打开浏览器也...
Spotify无法注册,想了很多办法后无果于是联系客服
Spotify无法注册,想了很多办法后无果于是联系客服
客服回答中国地区现在无法注册
If you still need help, contact Spotify Support.
mysql8使用自带全文索引(带中文分词)
如果之前建立全文索引,要先删除建立的索引,然后用下面的重新建立索引,亲测有效,nice 20200408 1307
ALTER TABLE `w_note` DROP INDEX content
ALTER TABLE `w_note` ADD FULLT...
MySQL如何重建索引
总结一下MySQL索引重建的方法:1: DROP INDEX + RECREATE INDEX.2: ALTER TABLE方法3: REPAIR TABLE方法,这种方法对于InnoDB存储引擎的表无效。4: OPTI...