赞
踩
Diffusion Model的效果如下:

源码如下:
# 1、选择一个数据集 %matplotlib inline import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_s_curve import torch s_curve, _ = make_s_curve(10**4, noise=0.1) # 10000个点 s_curve = s_curve[:, [0,2]]/10.0 # 每个点只取第0维和第2维 print(f'[Info] s_curve[0]: { s_curve[0]}') print(f"[Info] shape of moons: { np.shape(s_curve)}") data = s_curve.T fig, ax = plt.subplots() ax.scatter(*data, color="red", edgecolor="white") ax.axis("off") dataset = torch.Tensor(s_curve).float() # 2、确定超参数的值 num_steps = 100 # 制定每一步的beta betas = torch.linspace(-6, 6, num_steps) print(f'[Info] betas: { betas.shape}, betas[0]: { betas[1]}') betas = torch.sigmoid(betas) * (0.5e-2 - 1e-5) + 1e-5 # 近似:最小值0.00001,最大值0.005 print(f'[Info] betas[0]: { betas[0]}, betas[-1]: { betas[-1]}') # 计算alpha alphas = 1 - betas alphas_prod = torch.cumprod(alphas, 0) # prod是product乘法,连乘 alphas_prod_p = torch.cat([torch.tensor([1]).float(), alphas_prod[:-1]], 0) alphas_bar_sqrt = torch.sqrt(alphas_prod) one_minus_alphas_bar_log = torch.log(1 - alphas_prod) one_minus_alphas_bar_sqrt = torch.sqrt(1 - alphas_prod) assert alphas.shape == alphas_prod.shape == alphas_prod_p.shape == alphas_bar_sqrt.shape \ == one_minus_alphas_bar_log.shape == one_minus_alphas_bar_sqrt.shape print("[Info] all the same shape:", betas.shape) # 测试linspace、cumprod alist = torch.linspace(1, 4, 4) # [1, 2, 3, 4]</
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。