马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Diffusion扩散模子是一种深度学习框架,紧张用于天生具有高真实性的图像、音频和文本数据。它基于物理学中的扩散过程,模拟数据从高维分布徐徐噪声化的过程,并通过学习这一过程的逆过程来天生数据。
以下是Diffusion扩散模子的根本原理和步调:
- 正向扩散过程(噪声添加过程):
- 从原始数据开始,徐徐添加随机噪声,直到数据变得完全随机。这个过程通常分为多个步调,每一步都向数据中添加肯定量的噪声。
- 正向过程的目的是学习数据分布,使得模子可以大概明白数据在各个阶段的特点。
- 反向扩散过程(去噪过程):
- 从完全随机的噪声数据开始,徐徐去除噪声,规复出原始数据。这个过程也是分为多个步调,每一步都使用神经网络来推测去除噪声的过程。
- 反向过程的目的是练习一个可以大概从噪声数据中规复出原始数据的模子。
- 练习过程:
- 在练习过程中,模子必要学习如安在每个时间步长上推测去除噪声的操纵。
- 通常使用深度神经网络作为推测模子,通过最小化推测的去噪操纵与真实去噪操纵之间的差异来举行练习。
- 天生过程:
- 在天生数据时,从纯噪声数据开始,通过反向扩散过程徐徐去除噪声,终极得到天生的数据。
构建Diffusion模子
- def rearrange(head, inputs):
- b, hc, x, y = inputs.shape
- c = hc // head
- return inputs.reshape((b, head, c, x * y))
- def rsqrt(x):
- res = ops.sqrt(x)
- return ops.inv(res)
- def randn_like(x, dtype=None):
- if dtype is None:
- dtype = x.dtype
- res = ops.standard_normal(x.shape).astype(dtype)
- return res
- def randn(shape, dtype=None):
- if dtype is None:
- dtype = ms.float32
- res = ops.standard_normal(shape).astype(dtype)
- return res
- def randint(low, high, size, dtype=ms.int32):
- res = ops.uniform(size, Tensor(low, dtype), Tensor(high, dtype), dtype=dtype)
- return res
- def exists(x):
- return x is not None
- def default(val, d):
- if exists(val):
- return val
- return d() if callable(d) else d
- def _check_dtype(d1, d2):
- if ms.float32 in (d1, d2):
- return ms.float32
- if d1 == d2:
- return d1
- raise ValueError('dtype is not supported.')
- class Residual(nn.Cell):
- def __init__(self, fn):
- super().__init__()
- self.fn = fn
- def construct(self, x, *args, **kwargs):
- return self.fn(x, *args, **kwargs) + x
- def Upsample(dim):
- return nn.Conv2dTranspose(dim, dim, 4, 2, pad_mode="pad", padding=1)
- def Downsample(dim):
- return nn.Conv2d(dim, dim, 4, 2, pad_mode="pad
复制代码 |