-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
钙钛矿太阳能电池人工神经网络ppsci实现 #1055
Open
ZhangPassion
wants to merge
5
commits into
PaddlePaddle:develop
Choose a base branch
from
ZhangPassion:dev_model
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
钙钛矿太阳能电池人工神经网络ppsci实现 #1055
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40edf00
钙钛矿太阳能电池人工神经网络ppsci实现
ZhangPassion 3dc91d3
feat: add perovskite solar cells model with hyperparameter optimization
ZhangPassion 6c5dbcc
Merge branch 'develop' into dev_model
ZhangPassion 305b188
feat: add perovskite solar cells model with hyperparameter optimization
ZhangPassion 0e57d92
Merge branch 'dev_model' of https://github.com/ZhangPassion/PaddleSci…
ZhangPassion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# psc_nn(Machine Learning for Perovskite Solar Cells: An Open-Source Pipeline) | ||
|
||
!!! note "注意事项" | ||
|
||
1. 开始训练前,请确保数据集已正确放置在 `data/cleaned/` 目录下。 | ||
2. 训练和评估需要安装额外的依赖包,请使用 `pip install -r requirements.txt` 安装。 | ||
3. 为获得最佳性能,建议使用 GPU 进行训练。 | ||
|
||
=== "模型训练命令" | ||
|
||
``` sh | ||
python psc_nn.py mode=train | ||
``` | ||
|
||
=== "模型评估命令" | ||
|
||
``` sh | ||
# 使用本地预训练模型 | ||
python psc_nn.py mode=eval eval.pretrained_model_path="Your pdparams path" | ||
``` | ||
|
||
``` sh | ||
# 或使用远程预训练模型 | ||
python psc_nn.py mode=eval eval.pretrained_model_path="https://paddle-org.bj.bcebos.com/paddlescience/models/PerovskiteSolarCells/solar_cell_pretrained.pdparams" | ||
``` | ||
|
||
| 预训练模型 | 指标 | | ||
|:--| :--| | ||
| [solar_cell_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/PerovskiteSolarCells/solar_cell_pretrained.pdparams) | RMSE: 3.91798 | | ||
|
||
## 1. 背景简介 | ||
|
||
太阳能电池是一种通过光电效应将光能直接转换为电能的关键能源器件,其性能预测是优化和设计太阳能电池的重要环节。然而,传统的性能预测方法往往依赖于复杂的物理模拟和大量的实验测试,不仅成本高昂,且耗时较长,制约了研究与开发的效率。 | ||
|
||
近年来,深度学习和机器学习技术的快速发展,为太阳能电池性能预测提供了创新的方法。通过机器学习技术,可以显著加快开发速度,同时实现与实验结果相当的预测精度。特别是在钙钛矿太阳能电池研究中,材料的化学组成和结构多样性为模型训练带来了新的挑战。为了解决这一问题,研究者们通常将材料的特性转换为固定长度的特征向量,以适配机器学习模型。尽管如此,不同性能指标的特征表示设计仍需不断优化,同时对模型预测结果的可解释性要求也更为严格。 | ||
|
||
本研究中,通过利用包含钙钛矿太阳能电池特性信息的全面数据库(PDP),我们构建并评估了包括 XGBoost、psc_nn 在内的多种机器学习模型,专注于预测短路电流密度(Jsc)。研究结果表明,结合深度学习与超参数优化工具(如 Optuna)能够显著提升太阳能电池设计的效率,为新型太阳能电池研发提供了更精确且高效的解决方案。 | ||
|
||
## 2. 模型原理 | ||
|
||
本章节仅对太阳能电池性能预测模型的原理进行简单地介绍,详细的理论推导请阅读 [Machine Learning for Perovskite Solar Cells: An Open-Source Pipeline](https://onlinelibrary.wiley.com/doi/10.1002/apxr.202400060)。 | ||
|
||
该方法的主要思想是通过人工神经网络建立光谱响应数据与短路电流密度(Jsc)之间的非线性映射关系。人工神经网络模型的总体结构如下图所示: | ||
|
||
![psc_nn_overview](../images/ML/psc_nn_overview.png) | ||
|
||
本案例采用多层感知机(MLP)作为基础模型架构,主要包括以下几个部分: | ||
|
||
1. 输入层:接收 2808 维的光谱响应数据 | ||
2. 隐藏层:4-6 层全连接层,每层的神经元数量通过 Optuna 优化 | ||
3. 激活函数:使用 ReLU 激活函数引入非线性特性 | ||
4. 输出层:输出预测的 Jsc 值 | ||
|
||
通过这种方式,我们可以自动找到最适合当前任务的模型配置,提高模型的预测性能。 | ||
|
||
## 3. 模型实现 | ||
|
||
本章节我们讲解如何基于 PaddleScience 代码实现钙钛矿太阳能电池性能预测模型。本案例结合 Optuna 框架进行超参数优化,并使用 PaddleScience 内置的各种功能模块。为了快速理解 PaddleScience,接下来仅对模型构建、约束构建、评估器构建等关键步骤进行阐述,而其余细节请参考 [API文档](../api/arch.md)。 | ||
|
||
### 3.1 数据集介绍 | ||
|
||
本案例使用的数据集包含 Perovskite Database Project(PDP) 数据。数据集分为以下几个部分: | ||
|
||
1. 训练集: | ||
- 特征数据:`data/cleaned/training.csv` | ||
- 标签数据:`data/cleaned/training_labels.csv` | ||
2. 验证集: | ||
- 特征数据:`data/cleaned/validation.csv` | ||
- 标签数据:`data/cleaned/validation_labels.csv` | ||
|
||
为了方便数据处理,我们实现了一个辅助函数 `create_tensor_dict` 来创建输入和标签的 tensor 字典: | ||
|
||
``` py linenums="31" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:31:37 | ||
--8<-- | ||
``` | ||
|
||
数据集的读取和预处理代码如下: | ||
|
||
``` py linenums="118" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:118:129 | ||
--8<-- | ||
``` | ||
|
||
为了进行超参数优化,我们将训练集进一步划分为训练集和验证集: | ||
|
||
``` py linenums="130" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:130:135 | ||
--8<-- | ||
``` | ||
|
||
### 3.2 模型构建 | ||
|
||
本案例使用 PaddleScience 内置的 `ppsci.arch.MLP` 构建多层感知机模型。模型的超参数通过 Optuna 框架进行优化,主要包括: | ||
|
||
1. 网络层数:4-6层 | ||
2. 每层神经元数量:10-input_dim/2 | ||
3. 激活函数:ReLU | ||
4. 输入维度:2808(光谱响应数据维度) | ||
5. 输出维度:1(Jsc 预测值) | ||
|
||
模型定义代码如下: | ||
|
||
``` py linenums="98" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:98:115 | ||
--8<-- | ||
``` | ||
|
||
### 3.3 损失函数设计 | ||
|
||
考虑到数据集中不同样本的重要性可能不同,我们设计了一个加权均方误差损失函数。该函数对较大的 Jsc 值赋予更高的权重,以提高模型在高性能太阳能电池上的预测准确性: | ||
|
||
``` py linenums="20" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:20:30 | ||
--8<-- | ||
``` | ||
|
||
### 3.4 约束构建 | ||
|
||
本案例基于数据驱动的方法求解问题,因此使用 PaddleScience 内置的 `SupervisedConstraint` 构建监督约束。为了减少代码重复,我们实现了 `create_constraint` 函数来创建监督约束: | ||
|
||
``` py linenums="38" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:38:58 | ||
--8<-- | ||
``` | ||
|
||
### 3.5 评估器构建 | ||
|
||
为了实时监测模型的训练情况,我们实现了 `create_validator` 函数来创建评估器: | ||
|
||
``` py linenums="59" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:59:78 | ||
--8<-- | ||
``` | ||
|
||
### 3.6 优化器构建 | ||
|
||
为了统一管理优化器和学习率调度器的创建,我们实现了 `create_optimizer` 函数: | ||
|
||
``` py linenums="79" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:79:97 | ||
--8<-- | ||
``` | ||
|
||
### 3.7 模型训练与评估 | ||
|
||
在训练过程中,我们使用上述封装的函数来创建数据字典、约束、评估器和优化器: | ||
|
||
``` py linenums="230" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py:230:280 | ||
--8<-- | ||
``` | ||
|
||
## 4. 完整代码 | ||
|
||
``` py linenums="1" title="examples/perovskite_solar_cells/psc_nn.py" | ||
--8<-- | ||
examples/perovskite_solar_cells/psc_nn.py | ||
--8<-- | ||
``` | ||
|
||
## 5. 参考文献 | ||
|
||
- [Machine Learning for Perovskite Solar Cells: An Open-Source Pipeline](https://onlinelibrary.wiley.com/doi/10.1002/apxr.202400060) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||
defaults: | ||||||||||
- ppsci_default | ||||||||||
- TRAIN: train_default | ||||||||||
- TRAIN/ema: ema_default | ||||||||||
- TRAIN/swa: swa_default | ||||||||||
- EVAL: eval_default | ||||||||||
- INFER: infer_default | ||||||||||
- hydra/job/config/override_dirname/exclude_keys: exclude_keys_default | ||||||||||
- _self_ | ||||||||||
|
||||||||||
hydra: | ||||||||||
run: | ||||||||||
dir: outputs_allen_cahn_piratenet/${now:%Y-%m-%d}/${now:%H-%M-%S}/${hydra.job.override_dirname} | ||||||||||
job: | ||||||||||
name: ${mode} | ||||||||||
chdir: false | ||||||||||
callbacks: | ||||||||||
init_callback: | ||||||||||
_target_: ppsci.utils.callbacks.InitCallback | ||||||||||
sweep: | ||||||||||
dir: ${hydra.run.dir} | ||||||||||
subdir: ./ | ||||||||||
|
||||||||||
mode: "train" | ||||||||||
seed: 42 | ||||||||||
output_dir: "./output" | ||||||||||
|
||||||||||
data: | ||||||||||
train_features_path: "./data/cleaned/training.csv" | ||||||||||
train_labels_path: "./data/cleaned/training_labels.csv" | ||||||||||
val_features_path: "./data/cleaned/validation.csv" | ||||||||||
val_labels_path: "./data/cleaned/validation_labels.csv" | ||||||||||
|
||||||||||
model: | ||||||||||
num_layers: 4 | ||||||||||
hidden_size: [128, 96, 64, 32] | ||||||||||
activation: "relu" | ||||||||||
input_dim: 2808 | ||||||||||
output_dim: 1 | ||||||||||
|
||||||||||
TRAIN: | ||||||||||
epochs: 10 | ||||||||||
batch_size: 64 | ||||||||||
learning_rate: 0.001 | ||||||||||
eval_during_train: true | ||||||||||
eval_freq: 5 | ||||||||||
save_freq: 10 | ||||||||||
log_freq: 50 | ||||||||||
lr_scheduler: | ||||||||||
gamma: 0.95 | ||||||||||
decay_steps: 5 | ||||||||||
warmup_epoch: 2 | ||||||||||
warmup_start_lr: 1.0e-6 | ||||||||||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
eval: | ||||||||||
batch_size: 64 | ||||||||||
eval_with_no_grad: true | ||||||||||
pretrained_model_path: null | ||||||||||
log_freq: 50 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.