mindflow.cell
- class mindflow.cell.FCSequential(in_channels, out_channels, layers, neurons, residual=True, act='sin', weight_init='normal', has_bias=True, bias_init='default', weight_norm=False)[源代码]
全连接层的一个时序容器,按序放入全连接层。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
layers (int) - 层的总数,包括输入/隐藏/输出层。
neurons (int) - 隐藏层的神经元数量。
residual (bool) - 隐藏层是否使用残差网络模块。若为True,使用残差网络模块。若为False,使用线性模块。默认值:True。
act (Union[str, Cell, Primitive, None]) - 激活应用于全连接层输出的函数,例如”ReLU”。默认值:”sin”。
weight_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始权重值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:’normal’。
has_bias (bool) - 指定图层是否使用偏置向量。默认值:True。
bias_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始偏差值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:’default’。
weight_norm (bool) - 是否计算权重的平方和。默认值:False。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
shape为
的Tensor。- 异常:
TypeError - 如果 layers 不是int类型。
TypeError - 如果 neurons 不是int类型。
TypeError - 如果 residual 不是bool类型。
ValueError - 如果 layers 小于3。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindflow.cell import FCSequential >>> from mindspore import Tensor >>> inputs = np.ones((16, 3)) >>> inputs = Tensor(inputs.astype(np.float32)) >>> net = FCSequential(3, 3, 5, 32, weight_init="ones", bias_init="zeros") >>> output = net(inputs).asnumpy() >>> print(output.shape) (16, 3)
- class mindflow.cell.FNO1D(in_channels, out_channels, resolution, modes, channels=20, depths=4, mlp_ratio=4, compute_dtype=mstype.float32)[源代码]
一维傅里叶神经算子(FNO1D)包含一个提升层、多个傅里叶层和一个解码器层。 有关更多详细信息,请参考论文 Fourier Neural Operator for Parametric Partial Differential Equations 。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
resolution (int) - 输入的分辨率。
modes (int) - 要保留的低频分量的数量。
channels (int) - 输入提升尺寸后的通道数。默认值:20。
depths (int) - FNO层的数量。默认值:4。
mlp_ratio (int) - 解码器层的通道数提升比率。默认值:4。
compute_dtype (dtype.Number) - 密集的计算类型。默认mstype.float16。支持以下数据类型:mstype.float32或mstype.float16。GPU后端建议使用mstype.float32,Ascend后端建议使用mstype.float16。
- 输入:
x (Tensor) - shape为
的Tensor。
- 输出:
Tensor,FNO网络的输出。
output (Tensor) - shape为
的Tensor。
- 异常:
TypeError - 如果 in_channels 不是int。
TypeError - 如果 out_channels 不是int。
TypeError - 如果 resolution 不是int。
TypeError - 如果 modes 不是int。
ValueError - 如果 modes 小于1。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindspore.common.initializer import initializer, Normal >>> from mindflow.cell.neural_operators import FNO1D >>> B, W, C = 32,1024,1 >>> input_ = initializer(Normal(), [B, W, C]) >>> net = FNO1D(in_channels=1, out_channels=1, resolution=64, modes=12) >>> output = net(input_) >>> print(output.shape) (32, 1024, 1)
- class mindflow.cell.FNO2D(in_channels, out_channels, resolution, modes, channels=20, depths=4, mlp_ratio=4, compute_dtype=mstype.float32)[源代码]
二维傅里叶神经算子(FNO2D)包含一个提升层、多个傅里叶层和一个解码器层。 有关更多详细信息,请参考论文 Fourier Neural Operator for Parametric Partial Differential Equations 。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
resolution (int) - 输入的分辨率。
modes (int) - 要保留的低频分量的数量。
channels (int) - 输入提升尺寸后的通道数。默认值:20。
depths (int) - FNO层的数量。默认值:4。
mlp_ratio (int) - 解码器层的通道数提升比率。默认值:4。
compute_dtype (dtype.Number) - 密集的计算类型。默认mstype.float16。支持以下数据类型:mstype.float16或mstype.float32。GPU后端建议使用mstype.float32,Ascend后端建议使用mstype.float16。
- 输入:
x (Tensor) - shape为
的Tensor。
- 输出:
Tensor,此FNO网络的输出。
output (Tensor) - shape为
的Tensor。
- 异常:
TypeError - 如果 in_channels 不是int。
TypeError - 如果 out_channels 不是int。
TypeError - 如果 resolution 不是int。
TypeError - 如果 modes 不是int。
ValueError - 如果 modes 小于1。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindspore.common.initializer import initializer, Normal >>> from mindflow.cell.neural_operators import FNO2D >>> B, H, W, C = 32, 64, 64, 1 >>> input = initializer(Normal(), [B, H, W, C]) >>> net = FNO2D(in_channels=1, out_channels=1, resolution=64, modes=12) >>> output = net(input) >>> print(output.shape) (32, 64, 64, 1)
- class mindflow.cell.InputScaleNet(input_scale, input_center=None)[源代码]
将输入值缩放到指定的区域。
- 参数:
input_scale (list) - 输入x/y/t的比例因子。
input_center (Union[list, None]) - 坐标转换的中心位置。默认值:None。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
shape为
的Tensor。- 异常:
TypeError - 如果 input_scale 不是list类型。
TypeError - 如果 input_center 不是list或者None类型。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindflow.cell import InputScaleNet >>> from mindspore import Tensor >>> inputs = np.random.uniform(size=(16, 3)) + 3.0 >>> inputs = Tensor(inputs.astype(np.float32)) >>> input_scale = [1.0, 2.0, 4.0] >>> input_center = [3.5, 3.5, 3.5] >>> net = InputScaleNet(input_scale, input_center) >>> output = net(inputs).asnumpy() >>> assert np.all(output[:, 0] <= 0.5) and np.all(output[:, 0] >= -0.5) >>> assert np.all(output[:, 1] <= 1.0) and np.all(output[:, 0] >= -1.0) >>> assert np.all(output[:, 2] <= 2.0) and np.all(output[:, 0] >= -2.0)
- class mindflow.cell.LinearBlock(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None)[源代码]
连接层Block。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
weight_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始权重值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”normal”。
bias_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始偏差值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”zeros”。
has_bias (bool) - 指定图层是否使用偏置向量。默认值:True。
activation (Union[str, Cell, Primitive, None]) - 应用于全连接输出的激活函数层。默认值:None。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
shape为
的Tensor。- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindelec.architecture import LinearBlock >>> from mindspore import Tensor >>> input = Tensor(np.array([[180, 234, 154], [244, 48, 247]], np.float32)) >>> net = LinearBlock(3, 4) >>> output = net(input) >>> print(output.shape) (2, 4)
- class mindflow.cell.MultiScaleFCCell(in_channels, out_channels, layers, neurons, residual=True, act='sin', weight_init='normal', weight_norm=False, has_bias=True, bias_init='default', num_scales=4, amp_factor=1.0, scale_factor=2.0, input_scale=None, input_center=None, latent_vector=None)[源代码]
多尺度神经网络。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
layers (int) - 层总数,包括输入/隐藏/输出层。
neurons (int) - 隐藏层的神经元数量。
residual (bool) - 隐藏层的残差块的全连接。默认值:True。
act (Union[str, Cell, Primitive, None]) - 激活应用于全连接层输出的函数,例如“ReLU”。默认值:sin。
weight_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始权重值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”normal”。
weight_norm (bool) - 是否计算权重的平方和。默认值:False。
has_bias (bool) - 指定图层是否使用偏置向量。默认值:True。
bias_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始偏差值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”default”。
num_scales (int) - 多规模网络的子网号。默认值:4
amp_factor (Union[int, float]) - 输入的放大系数。默认值:1.0
scale_factor (Union[int, float]) - 基本比例因子。默认值:2.0
input_scale (Union[list, None]) - 输入x/y/t的比例因子。如果不是None,则输入将在网络中设置之前缩放。默认值:None。
input_center (Union[list, None]) - 坐标转换的中心位置。如果不是None,则输入将在网络中设置之前翻译。默认值:None。
latent_vector (Union[Parameter, None]) - 将与采样输入连接的可训练的parameter并在训练期间更新。默认值:None。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
shape为
的Tensor。- 异常:
TypeError - 如果 num_scales 不是int类型。
TypeError - 如果 amp_factor 不是int及或者float类型。
TypeError - 如果 scale_factor 不是int及或者float类型。
TypeError - 如果 latent_vector 不是Parameter或者None类型。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindflow.cell import MultiScaleFCCell >>> from mindspore import Tensor, Parameter >>> inputs = np.ones((64,3)) + 3.0 >>> inputs = Tensor(inputs.astype(np.float32)) >>> num_scenarios = 4 >>> latent_size = 16 >>> latent_init = np.ones((num_scenarios, latent_size)).astype(np.float32) >>> latent_vector = Parameter(Tensor(latent_init), requires_grad=True) >>> input_scale = [1.0, 2.0, 4.0] >>> input_center = [3.5, 3.5, 3.5] >>> net = MultiScaleFCCell(3, 3, 5, 32, ... weight_init="ones", bias_init="zeros", ... input_scale=input_scale, input_center=input_center, latent_vector=latent_vector) >>> output = net(inputs).asnumpy() >>> print(output.shape) (64, 3)
- class mindflow.cell.PDENet(height, width, channels, kernel_size, max_order, step, dx=0.01, dy=0.01, dt=0.01, periodic=True, enable_moment=True, if_fronzen=False)[源代码]
PDE-Net模型。 PDE-Net是一个前馈深度网络,可同时实现两个目标:准确预测复杂的系统,并揭示底层隐藏的PDE模型。基本思想是学习微分算子通过学习卷积核(过滤器),并将神经网络或其他机器学习方法应用于 近似未知非线性响应。PDE-Net的特殊性在于,卷积核受“矩”的约束,这使得模型能够轻松地识别PDE模型,同时仍保持网络的表达能力和预测能力。 这些约束通过充分利用微分算子的阶数与卷积核的关系得到的.一个重要的概念起源于小波理论。有关更多详细信息,请参考论文 PDE-NET: LEARNING PDES FROM DATA 。
- 参数:
height (int) - PDE-Net输入和输出Tensor的高度。
width (int) - PDE-Net输入和输出Tensor的宽度。
channels (int) - PDE-Net输入和输出Tensor的通。
kernel_size (int) - 指定2D卷积内核的高度和宽度。
max_order (int) - PDE模型的最大顺序。
step (int) - PDE-Net中使用的增量-T块的数量。
dx (float) - x维的空间分辨率。默认值:0.01。
dy (float) - y维的空间分辨率。默认值:0.01。
dt (float) - PDE-Net的时间步长。默认值:0.01。
periodic (bool) - 指定周期是否与卷积核一起使用。默认值:True。
enable_moment (bool) - 指定卷积核是否受moment约束。默认值:True。
if_fronzen (bool) - moment里的参数是否参与训练。默认值:False。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
Tensor,具有与 input 相同的形状,数据类型为float32。
- 异常:
TypeError - 如果 height 、 width 、 channels 、 kernel_size 、 max_order 或 step 不是int。
TypeError - 如果 periodic 、 enable_moment 、 if_fronzen 不是bool。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.common.dtype as mstype >>> from mindflow.cell.neural_operators import PDENet >>> input = Tensor(np.random.rand(1, 2, 16, 16), mstype.float32) >>> net = PDENet(16, 16, 2, 5, 3, 2) >>> output = net(input) >>> print(output.shape) (1, 2, 16, 16)
- class mindflow.cell.ResBlock(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None, weight_norm=False)[源代码]
密集层的ResBlock。
- 参数:
in_channels (int) - 输入中的通道数。
out_channels (int) - 输出中的通道数。
weight_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始权重值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”normal”。
bias_init (Union[Tensor, str, Initializer, numbers.Number]) - 可训练的初始偏差值。数据类型与输入 input 相同。str的值引用函数 initializer 。默认值:”zeros”。
has_bias (bool) - 指定图层是否使用偏置向量。默认值:True。
activation (Union[str, Cell, Primitive, None]) - 应用于密集层输出的激活函数。默认值:None。
weight_norm (bool) - 是否计算权重的平方和。默认值:False。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
shape为
的Tensor。- 异常:
ValueError - 如果 in_channels 不等于 out_channels 。
TypeError - 如果 activation 类型不是str或者Cell或者Primitive。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindflow.cell import ResBlock >>> from mindspore import Tensor >>> input = Tensor(np.array([[180, 234, 154], [244, 48, 247]], np.float32)) >>> net = ResBlock(3, 3) >>> output = net(input) >>> print(output.shape) (2, 3)
- class mindflow.cell.ViT(image_size=(192, 384), in_channels=7, out_channels=3, patch_size=16, encoder_depths=12, encoder_embed_dim=768, encoder_num_heads=12, decoder_depths=8, decoder_embed_dim=512, decoder_num_heads=16, mlp_ratio=4, dropout_rate=1.0, compute_dtype=mstype.float16)[源代码]
该模块基于ViT,包括encoder层、decoding_embedding层、decoder层和dense层。
- 参数:
image_size (tuple[int]) - 输入的图像尺寸。默认值:(192,384)。
in_channels (int) - 输入的输入特征维度。默认值:7。
out_channels (int) - 输出的输出特征维度。默认值:3。
patch_size (int) - 图像的path尺寸。默认值:16。
encoder_depths (int) - encoder层的层数。默认值:12。
encoder_embed_dim (int) - encoder层的编码器维度。默认值:768。
encoder_num_heads (int) - encoder层的head数。默认值:12。
decoder_depths (int) - decoder层的解码器深度。默认值:8。
decoder_embed_dim (int) - decoder层的解码器维度。默认值:512。
decoder_num_heads (int) - decoder层的head数。默认值:16。
mlp_ratio (int) - mlp层的比例。默认值:4。
dropout_rate (float) - dropout层的速率。默认值:1.0。
compute_dtype (dtype) - encoder层、decoding_embedding层、decoder层和dense层的数据类型。默认值:mstype.float16。
- 输入:
input (Tensor) - shape为
的Tensor。
- 输出:
output (Tensor) - shape为
的Tensor。其中,patchify_size = (image_height * image_width) / (patch_size * patch_size)
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import context >>> from mindspore import dtype as mstype >>> from mindflow.cell import ViT >>> input_tensor = Tensor(np.ones((32, 3, 192, 384)), mstype.float32) >>> print(input_tensor.shape) (32, 3, 192, 384) >>> model = ViT(in_channels=3, >>> out_channels=3, >>> encoder_depths=6, >>> encoder_embed_dim=768, >>> encoder_num_heads=12, >>> decoder_depths=6, >>> decoder_embed_dim=512, >>> decoder_num_heads=16, >>> ) >>> output_tensor = model(input_tensor) >>> print(output_tensor.shape) (32, 288, 768)
- mindflow.cell.get_activation(name)[源代码]
获取激活函数。
- 参数:
name (Union[str, None]) - 激活函数的名称。若输入为None,函数返回None。
- 返回:
Function,激活函数。
- 支持平台:
Ascend
GPU
样例:
>>> import numpy as np >>> from mindflow.cell import get_activation >>> from mindspore import Tensor >>> input_x = Tensor(np.array([[1.2, 0.1], [0.2, 3.2]], dtype=np.float32)) >>> sigmoid = get_activation('sigmoid') >>> output = sigmoid(input_x) >>> print(output) [[0.7685248 0.5249792 ] [0.54983395 0.96083426]]