昇思练习营打卡第二十二天(Diffusion扩散模子)

[复制链接]
发表于 2026-2-27 10:19:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Diffusion扩散模子是一种深度学习框架,紧张用于天生具有高真实性的图像、音频和文本数据。它基于物理学中的扩散过程,模拟数据从高维分布徐徐噪声化的过程,并通过学习这一过程的逆过程来天生数据。
以下是Diffusion扩散模子的根本原理和步调:
      
  • 正向扩散过程(噪声添加过程):
         
    • 从原始数据开始,徐徐添加随机噪声,直到数据变得完全随机。这个过程通常分为多个步调,每一步都向数据中添加肯定量的噪声。   
    • 正向过程的目的是学习数据分布,使得模子可以大概明白数据在各个阶段的特点。   
      
  • 反向扩散过程(去噪过程):
         
    • 从完全随机的噪声数据开始,徐徐去除噪声,规复出原始数据。这个过程也是分为多个步调,每一步都使用神经网络来推测去除噪声的过程。   
    • 反向过程的目的是练习一个可以大概从噪声数据中规复出原始数据的模子。   
      
  • 练习过程:
         
    • 在练习过程中,模子必要学习如安在每个时间步长上推测去除噪声的操纵。   
    • 通常使用深度神经网络作为推测模子,通过最小化推测的去噪操纵与真实去噪操纵之间的差异来举行练习。   
      
  • 天生过程:
         
    • 在天生数据时,从纯噪声数据开始,通过反向扩散过程徐徐去除噪声,终极得到天生的数据。   

构建Diffusion模子

  1. def rearrange(head, inputs):
  2.     b, hc, x, y = inputs.shape
  3.     c = hc // head
  4.     return inputs.reshape((b, head, c, x * y))
  5. def rsqrt(x):
  6.     res = ops.sqrt(x)
  7.     return ops.inv(res)
  8. def randn_like(x, dtype=None):
  9.     if dtype is None:
  10.         dtype = x.dtype
  11.     res = ops.standard_normal(x.shape).astype(dtype)
  12.     return res
  13. def randn(shape, dtype=None):
  14.     if dtype is None:
  15.         dtype = ms.float32
  16.     res = ops.standard_normal(shape).astype(dtype)
  17.     return res
  18. def randint(low, high, size, dtype=ms.int32):
  19.     res = ops.uniform(size, Tensor(low, dtype), Tensor(high, dtype), dtype=dtype)
  20.     return res
  21. def exists(x):
  22.     return x is not None
  23. def default(val, d):
  24.     if exists(val):
  25.         return val
  26.     return d() if callable(d) else d
  27. def _check_dtype(d1, d2):
  28.     if ms.float32 in (d1, d2):
  29.         return ms.float32
  30.     if d1 == d2:
  31.         return d1
  32.     raise ValueError('dtype is not supported.')
  33. class Residual(nn.Cell):
  34.     def __init__(self, fn):
  35.         super().__init__()
  36.         self.fn = fn
  37.     def construct(self, x, *args, **kwargs):
  38.         return self.fn(x, *args, **kwargs) + x
  39. def Upsample(dim):
  40.     return nn.Conv2dTranspose(dim, dim, 4, 2, pad_mode="pad", padding=1)
  41. def Downsample(dim):
  42.     return nn.Conv2d(dim, dim, 4, 2, pad_mode="pad
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表