mindspore.SummaryLandscape
- class mindspore.SummaryLandscape(summary_dir)[源代码]
SummaryLandscape可以帮助您收集loss地形图的信息。通过计算loss,可以在PCA(Principal Component Analysis)方向或者随机方向创建地形图。
说明
SummaryLandscape仅支持Linux系统。
- 参数:
summary_dir (str) - 该路径将被用来保存创建地形图所使用的数据。
样例:
>>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore.train import Model, Accuracy, Loss >>> from mindspore import SummaryCollector, SummaryLandscape >>> >>> if __name__ == '__main__': ... # If the device_target is Ascend, set the device_target to "Ascend" ... ms.set_context(mode=ms.GRAPH_MODE, device_target="GPU") ... # Create the dataset taking MNIST as an example. Refer to ... # https://gitee.com/mindspore/docs/blob/r2.4.1/docs/mindspore/code/mnist.py ... ds_train = create_dataset() ... # Define the network structure of LeNet5. Refer to ... # https://gitee.com/mindspore/docs/blob/r2.4.1/docs/mindspore/code/lenet.py ... network = LeNet5() ... net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") ... net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9) ... model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) ... # Simple usage for collect landscape information: ... interval_1 = [1, 2, 3, 4, 5] ... summary_collector = SummaryCollector(summary_dir='./summary/lenet_interval_1', ... collect_specified_data={'collect_landscape':{"landscape_size": 4, ... "unit": "step", ... "create_landscape":{"train":True, ... "result":False}, ... "num_samples": 2048, ... "intervals": [interval_1]} ... }) ... model.train(1, ds_train, callbacks=[summary_collector], dataset_sink_mode=False) ... ... # Simple usage for visualization landscape: ... def callback_fn(): ... # Define the network structure of LeNet5. Refer to ... # https://gitee.com/mindspore/docs/blob/r2.4.1/docs/mindspore/code/lenet.py ... network = LeNet5() ... net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") ... metrics = {"Loss": Loss()} ... model = Model(network, net_loss, metrics=metrics) ... # Create the dataset taking MNIST as an example. Refer to ... # https://gitee.com/mindspore/docs/blob/r2.4.1/docs/mindspore/code/mnist.py ... ds_eval = create_dataset() ... return model, network, ds_eval, metrics ... ... summary_landscape = SummaryLandscape('./summary/lenet_interval_1') ... # parameters of collect_landscape can be modified or unchanged ... summary_landscape.gen_landscapes_with_multi_process(callback_fn, ... collect_landscape={"landscape_size": 4, ... "create_landscape":{"train":False, ... "result":False}, ... "num_samples": 2048, ... "intervals": [interval_1]}, ... device_ids=[1])
- gen_landscapes_with_multi_process(callback_fn, collect_landscape=None, device_ids=None, output=None)[源代码]
使用多进程来生成地形图。
- 参数:
callback_fn (python function) - Python函数对象,用户需要写一个没有输入的函数,返回值要求如下。
mindspore.train.Model:用户的模型。
mindspore.nn.Cell:用户的网络。
mindspore.dataset:创建loss所需要的用户数据集。
mindspore.train.Metrics:用户的评估指标。
collect_landscape (Union[dict, None]) - 创建loss地形图所用的参数含义与SummaryCollector同名字段一致。此处设置的目的是允许用户可以自由修改创建loss地形图参数。默认值:
None
。landscape_size (int) - 指定生成loss地形图的图像分辨率。例如:如果设置为
128
,则loss地形图的分辨率是128*128。计算loss地形图的时间随着分辨率的增大而增加。默认值:40
。可选值:3-256。create_landscape (dict) - 选择创建哪种类型的loss地形图,分为训练过程loss地形图(train)和训练结果loss地形图(result)。默认值:
{"train": True, "result": True}
。可选值:True
/False
。num_samples (int) - 创建loss地形图所使用的数据集的大小。例如:在图像数据集中,您可以设置 num_samples 是
128
,这意味着将有128张图片被用来创建loss地形图。注意:num_samples 越大,计算loss地形图时间越长。默认值:128
。intervals (List[List[int]]) - 指定创建loss地形图所需要的checkpoint区间。例如:如果用户想要创建两张训练过程的loss地形图,分别为1-5epoch和6-10epoch,则用户可以设置[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]。注意:每个区间至少包含3个epoch。
device_ids (List(int)) - 指定创建loss地形图所使用的目标设备的ID。例如:[0, 1]表示使用设备0和设备1来创建loss地形图。默认值:
None
。output (str) - 指定保存loss地形图的路径。默认值:
None
。默认保存路径与summary文件相同。