ai-for-science-tf-to-pytorch
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "ai-for-science-tf-to-pytorch":https://skillpi.com/zh/skills/deepskill-1e7e4547b43a12e61f11e01c5f532d362112db212759c7bde3201d11294f9e56。安装前核对上游来源和版本:https://github.com/ascend-ai-coding/awesome-ascend-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
将 TensorFlow 或 Keras 模型改写到 PyTorch 的通用技能,适用于在华为 Ascend NPU 或其他依赖 PyTorch 生态的平台上完成层级映射、权重转换、逐层数值验证与端到端精度对比。尤其适合 ProteinBERT、DeepFRI 等科学模型的跨框架迁移。
介绍来源: third_party · ascend-ai-coding · 0.0.1 参考来源
SkillPi 解读
- 能完成的任务
- TensorFlow 或 Keras 模型改写到 PyTorch 的通用 Skill,适用于在华为 Ascend NPU 或其他依赖 PyTorch 生态的平台上完成层级映射、权重转换、逐层数值验证和端到端精度对比,尤其适合 ProteinBERT、DeepFRI 这类科学模型的跨框架迁移。
- 适合谁
- 翻译待完成,请参阅来源文档。
- 输入和输出
- 翻译待完成,请参阅来源文档。
- 实测状态
- 未测试
文件浏览
Frontmatter
name: ai-for-science-tf-to-pytorch
description: TensorFlow 或 Keras 模型改写到 PyTorch 的通用 Skill,适用于在华为 Ascend NPU 或其他依赖 PyTorch 生态的平台上完成层级映射、权重转换、逐层数值验证和端到端精度对比,尤其适合 ProteinBERT、DeepFRI 这类科学模型的跨框架迁移。
keywords:
- ai-for-science
- tensorflow
- keras
- pytorch
- conversion
- weight-mappingTensorFlow/Keras to PyTorch Conversion
A systematic approach to converting TF/Keras models to PyTorch with numerical precision verification. Derived from real production experience converting ProteinBERT (6-layer attention model, 16M params, 145 weight arrays) and DeepFRI (GCN + CNN protein function prediction with CuDNNLSTM language model).
When to Use
- Target platform has no TF support (e.g. Ascend NPU with torch_npu)
- Need PyTorch ecosystem features (torchscript, ONNX, distributed)
- Migrating research code from TF to PyTorch for maintenance
- Converting pretrained TF weights to PyTorch format
Conversion Workflow
1. Analyze TF model → Map every layer, activation, parameter shape
2. Rewrite in PyTorch → Create equivalent nn.Module hierarchy
3. Convert weights → Map TF arrays to PyTorch state_dict
4. Verify layer-by-layer → Compare intermediate outputs at every layer
5. Fix discrepancies → Address framework-specific differences
6. End-to-end validate → Compare final outputs, run downstream tasks
Step 1: Analyze the TF Model
Before writing any PyTorch code, fully understand the TF model:
# Print all layers and shapes
for layer in model.layers:
print(f"{layer.name:40s} type={type(layer).__name__:25s} output_shape={layer.output_shape}")
# Print all weight shapes and names
for i, var in enumerate(model.variables):
print(f"[{i:3d}] {var.name:50s} shape={var.shape}")
Key things to document:
- Layer names and their order (may differ from code order)
- Weight shapes and dtypes
- Activation functions used (and whether they're fused into layers)
- Custom layers (especially attention mechanisms)
- Default hyperparameters (epsilon, momentum, etc.)
Step 2: Layer-by-Layer Conversion Rules
Dense / Linear
| Property | TF/Keras | PyTorch |
|---|---|---|
| Class | keras.layers.Dense |
nn.Linear |
| Kernel shape | (in_features, out_features) |
(out_features, in_features) |
| Activation | Fused: Dense(n, activation='gelu') |
Separate: nn.Linear(n) + F.gelu() |
| Weight conversion | kernel |
.weight = kernel.T ← must transpose |
| Bias conversion | bias |
.bias = bias (direct copy) |
Embedding
| Property | TF/Keras | PyTorch |
|---|---|---|
| Class | keras.layers.Embedding |
nn.Embedding |
| Weight shape | (vocab_size, embed_dim) |
(vocab_size, embed_dim) |
| Weight conversion | embeddings |
.weight = embeddings (direct copy, no transpose) |
Conv1D
| Property | TF/Keras | PyTorch |
|---|---|---|
| Class | keras.layers.Conv1D |
nn.Conv1d |
| Input format | (batch, length, channels) |
(batch, channels, length) |
| Kernel shape | (kernel_size, in_ch, out_ch) |
(out_ch, in_ch, kernel_size) |
| Weight conversion | kernel |
.weight = np.transpose(kernel, (2,1,0)) |
| Padding "same" | Built-in, may use asymmetric padding | padding='same' (PyTorch 1.9+) |
Important: When using Conv1d in PyTorch, transpose the input before and after:
# TF: hidden_seq shape (batch, length, channels) - Conv1D operates directly
# PyTorch:
seq_t = hidden_seq.transpose(1, 2) # (batch, channels, length)
out = F.gelu(self.conv(seq_t)).transpose(1, 2) # back to (batch, length, channels)
LayerNormalization / LayerNorm
| Property | TF/Keras | PyTorch |
|---|---|---|
| Class | keras.layers.LayerNormalization |
nn.LayerNorm |
| Default epsilon | 1e-3 (0.001) | 1e-5 (0.00001) |
| Weight names | gamma, beta |
weight, bias |
| Weight conversion | Direct copy | Direct copy |
⚠️ CRITICAL: Always set
nn.LayerNorm(dim, eps=1e-3)to match TF default. This 100x epsilon difference propagates through deep networks and causes massive numerical divergence. This is the #1 most common silent bug.
BatchNormalization / BatchNorm
| Property | TF/Keras | PyTorch |
|---|---|---|
| Class | keras.layers.BatchNormalization |
nn.BatchNorm1d / nn.BatchNorm2d |
| Default epsilon | 1e-3 (0.001) | 1e-5 (0.00001) |
| Default momentum | 0.99 (as momentum) |
0.1 (as 1 - momentum) |
| Weight names | gamma, beta, moving_mean, moving_variance |
weight, bias, running_mean, running_var |
| Weight conversion | Direct copy | Direct copy |
⚠️ CRITICAL: Same 100x epsilon trap as LayerNorm. Always set
nn.BatchNorm1d(dim, eps=1e-3). When BNmoving_variancecontains near-zero values (common after training), the wrong epsilon causes division-by-near-zero → output explodes → softmax saturates to 0/1 → model outputs all zeros. This was the #1 bug in DeepFRI CNN conversion.
CuDNNLSTM / LSTM
TF CuDNNLSTM is a GPU-optimized LSTM with different weight layout than
standard keras.layers.LSTM. It's commonly found in older TF models.
| Property | TF CuDNNLSTM | TF Keras LSTM | PyTorch nn.LSTM |
|---|---|---|---|
| Kernel shape | (input, 4*H) |
(input, 4*H) |
weight_ih: (4*H, input) |
| Recurrent kernel | (H, 4*H) |
(H, 4*H) |
weight_hh: (4*H, H) |
| Bias shape | *`(8H,)`** ← two groups | (4*H,) single |
bias_ih: (4*H,) + bias_hh: (4*H,) |
| Recurrent activation | sigmoid (fixed) | configurable | sigmoid (default) |
CuDNNLSTM weight conversion:
# Kernel and recurrent kernel: just transpose
pt_weight_ih = tf_kernel.T # (input, 4*H) -> (4*H, input)
pt_weight_hh = tf_recurrent_kernel.T # (H, 4*H) -> (4*H, H)
# Bias: CuDNNLSTM stores TWO bias groups concatenated
# bias[:4*H] = input-hidden bias, bias[4*H:] = hidden-hidden bias
pt_bias_ih = tf_bias[:4 * hidden_dim]
pt_bias_hh = tf_bias[4 * hidden_dim:]
Standard Keras LSTM weight conversion:
pt_weight_ih = tf_kernel.T
pt_weight_hh = tf_recurrent_kernel.T
pt_bias_ih = tf_bias # single group (4*H,)
pt_bias_hh = np.zeros(4 * hidden_dim) # PyTorch needs both; set hh to zero
⚠️ WARNING: CuDNNLSTM models are GPU-only weights. When converted to PyTorch
nn.LSTM, the output is mathematically equivalent per-step, but accumulated floating-point differences over long sequences can be amplified by downstream layers (e.g. graph convolutions). If the original model has both GPU (CuDNNLSTM) and CPU (standard LSTM) versions, prefer the CPU version for non-CUDA deployments (e.g. Ascend NPU).
GELU Activation
| Property | TF/Keras (>=2.4) | PyTorch |
|---|---|---|
| Default | Exact: x * Φ(x) |
Exact: x * Φ(x) |
| Approximate | N/A | F.gelu(x, approximate="tanh") |
| Match? | ✅ Both use exact by default | Use F.gelu() without approximate |
Step 3: K.dot and High-Dimensional Tensor Operations
This is the most dangerous part of conversion. TF's K.dot has different
semantics than any single PyTorch operation.
K.dot Behavior
K.dot(A, B) contracts A's last dimension with B's second-to-last dimension
(standard matrix multiplication semantics, generalized to higher dimensions).
# Example: K.dot(S, W) where S:(batch, length, d_in), W:(n_heads, d_in, d_out)
# Contracts S[..., d_in] with W[..., d_in, :] -> result: (batch, length, n_heads, d_out)
⚠️ Common einsum Mistakes
# W shape: (n_heads, d_in, d_out) - d_in is the SECOND dimension (index 1)
# ❌ WRONG - contracts with W's LAST dimension
VS = torch.einsum('bls,hvs->bhlv', S, W) # s matches W's dim 2 (last)
# ✅ CORRECT - contracts with W's SECOND dimension
VS = torch.einsum('bls,hsv->bhlv', S, W) # s matches W's dim 1 (second-to-last)
Verification Pattern
Always verify K.dot equivalence with random data:
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
import torch
S = np.random.randn(2, 5, 128).astype(np.float32)
W = np.random.randn(4, 128, 64).astype(np.float32)
tf_result = K.dot(tf.constant(S), tf.constant(W)).numpy() # (2, 5, 4, 64)
pt_result = torch.einsum('bls,hsv->blhv', torch.tensor(S), torch.tensor(W)).numpy()
np.testing.assert_allclose(tf_result, pt_result, atol=1e-6) # must pass
K.permute_dimensions Mapping
After K.dot, TF typically reorders dimensions with K.permute_dimensions.
Map the full chain:
# TF: result = K.permute_dimensions(K.dot(S, W), (0, 2, 1, 3))
# K.dot: (batch, length, heads, dim)
# permute: (batch, heads, length, dim)
#
# PyTorch equivalent (single einsum):
result = torch.einsum('bls,hsv->bhlv', S, W) # directly (batch, heads, length, dim)
Step 4: Weight Mapping
Determine Weight Order
TF pickle/checkpoint weight order may NOT match code definition order. Always inspect the actual order first:
import pickle
with open('model.pkl', 'rb') as f:
data = pickle.load(f)
for i, w in enumerate(data['weights']):
print(f"[{i:3d}] shape={str(w.shape):25s}")
Then map each weight to the corresponding PyTorch parameter by shape matching.
Weight Conversion Checklist
For each weight array:
- [ ] Dense kernel: transpose
.T - [ ] Dense bias: direct copy
- [ ] Conv1D kernel: transpose
np.transpose(w, (2,1,0)) - [ ] Conv1D bias: direct copy
- [ ] LayerNorm gamma/beta: direct copy, set eps=1e-3
- [ ] BatchNorm gamma/beta/moving_mean/moving_var: direct copy, set eps=1e-3
- [ ] Embedding: direct copy (no transpose)
- [ ] CuDNNLSTM kernel/recurrent_kernel: transpose
.T - [ ] CuDNNLSTM bias
(8*H,): split intobias_ih[:4H]+bias_hh[4H:] - [ ] Standard LSTM kernel/recurrent_kernel: transpose
.T - [ ] Standard LSTM bias
(4*H,): copy tobias_ih, setbias_hhto zeros - [ ] Custom attention weights (Wq/Wk/Wv): usually direct copy, but verify shape
Step 5: Layer-by-Layer Verification
This is the most important step. Never skip it.
Create Debug Scripts
Create paired debug scripts that print identical statistics at every layer:
# Both GPU (TF) and NPU/CPU (PyTorch) scripts should print:
for layer_name, output in layer_outputs:
print(f"{layer_name:45s} shape={str(output.shape):25s} "
f"mean={output.mean():.6f} std={output.std():.6f} "
f"min={output.min():.6f} max={output.max():.6f}")
Identify Divergence Point
Compare outputs layer by layer. The FIRST layer where mean/std diverge significantly (>1e-3) reveals the bug location.
Typical pattern:
embedding: GPU mean=0.000829 NPU mean=0.000829 ✅ match
dense-global-input: GPU mean=-0.005728 NPU mean=-0.005728 ✅ match
global-merge1-norm-b1: GPU mean=-0.031465 NPU mean=0.017317 ❌ DIVERGED
→ Bug is between dense-global-input and global-merge1-norm-block1.
Common Divergence Sources (ranked by frequency)
- Normalization epsilon (LayerNorm/BatchNorm 1e-3 vs 1e-5) - causes output explosion or gradual drift
- einsum dimension mismatch (K.dot semantics) - causes total output corruption
- CuDNNLSTM bias splitting (8*H bias not split into ih+hh) - causes wrong LSTM output
- Conv1D weight transpose wrong - causes garbage output from convolutions
- Dense weight not transposed - causes completely wrong linear projections
- Activation variant mismatch (approximate vs exact GELU) - minor drift
Step 6: Fine-tuning Verification
When converting fine-tuning code, also verify:
Hidden Layer Concatenation
If TF code uses get_model_with_hidden_layers_as_outputs or similar to
concatenate hidden layers for the fine-tuning head:
- Check exactly WHICH layers TF collects (filter by name + type)
- Some layer names in filters may not exist in the model (dead filters)
- Match the concatenated dimension exactly
Optimizer Differences
| Property | TF Adam | PyTorch Adam |
|---|---|---|
| Default epsilon | 1e-7 | 1e-8 |
| Weight decay | Separate AdamW |
weight_decay param |
| LR schedule | Callbacks | lr_scheduler |
These cause minor training dynamics differences but should not affect final model quality significantly.
Quick Reference: Conversion Cheat Sheet
# Dense
pt_weight = tf_kernel.T
pt_bias = tf_bias
# Conv1D
pt_weight = np.transpose(tf_kernel, (2, 1, 0))
pt_bias = tf_bias
# LayerNorm
nn.LayerNorm(dim, eps=1e-3) # MUST set eps to match TF
pt_weight = tf_gamma
pt_bias = tf_beta
# BatchNorm
nn.BatchNorm1d(dim, eps=1e-3) # MUST set eps to match TF
pt_weight = tf_gamma
pt_bias = tf_beta
pt_running_mean = tf_moving_mean
pt_running_var = tf_moving_variance
# Embedding
pt_weight = tf_embeddings # no transpose
# CuDNNLSTM (bias shape 8*H)
pt_weight_ih = tf_kernel.T
pt_weight_hh = tf_recurrent_kernel.T
pt_bias_ih = tf_bias[:4*H]
pt_bias_hh = tf_bias[4*H:]
# Standard Keras LSTM (bias shape 4*H)
pt_weight_ih = tf_kernel.T
pt_weight_hh = tf_recurrent_kernel.T
pt_bias_ih = tf_bias
pt_bias_hh = np.zeros(4*H)
# K.dot(S[b,l,s], W[h,s,v]) -> einsum('bls,hsv->blhv', S, W)
# K.dot contracts last dim of A with second-to-last dim of B
For detailed weight mapping examples and real conversion scripts,
read references/weight_conversion_example.md.
配套脚本
- 数值对比辅助脚本:
python scripts/compare_arrays.py reference.npz candidate.npz --atol 1e-5 --rtol 1e-4
参考资料
- 跨框架数值验证清单:
references/numerical-validation.md
当前收录版本
0.0.1
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 针对科学计算模型(如 ProteinBERT、DeepFRI)提供完整的 TensorFlow 到 PyTorch 转换方案
- 覆盖层级映射、权重转换、逐层数值验证和端到端精度对比全流程
- 特别适配华为 Ascend NPU 等依赖 PyTorch 生态的部署场景
亮点
- 触发词精准覆盖 TensorFlow、Keras、PyTorch、conversion、weight-mapping 等核心概念
- 明确限定于科学模型跨框架迁移场景,边界清晰无歧义
亮点
- 文档结构完整,包含 When to Use、Workflow、Step-by-Step 指南和验证清单
- 提供详细的转换规则表格和代码示例,frontmatter 规范完整
亮点
- 关键参数设置明确(如 LayerNorm/BatchNorm eps=1e-3)
- 覆盖 CuDNNLSTM bias 拆分、Conv1D 权重转置等高风险操作
- 提供逐层输出对比和数值断言验证机制
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
读取本地文件
限定到你选择的文件或项目目录;避免读取无关的个人资料、密钥和配置文件。
主文档提及 · 第 241 行
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。