当前位置: 首页 > news >正文

AF3 squeeze_features函数解读

AlphaFold3  data_transforms 模块的 squeeze_features 函数的作用去除 蛋白质特征张量中不必要的单维度(singleton dimensions)和重复维度,以使其适配 AlphaFold3 预期的输入格式。

源代码:

def squeeze_features(protein):
    """Remove singleton and repeated dimensions in protein features."""
    protein["aatype"] = torch.argmax(protein["aatype"], dim=-1)
    for k in [
        "domain_name",
        "msa",
        "num_alignments",
        "seq_length",
        "sequence",
        "superfamily",
        "deletion_matrix",
        "resolution",
        "between_segment_residues",
        "residue_index",
        "template_all_atom_mask",
    ]:
        if k in protein:
            final_dim = protein[k].shape[-1]
            if isinstance(final_dim, int) and final_dim == 1:
                if torch.is_tensor(protein[k]):
                    protein[k] = torch.squeeze(protein[k], dim=-1)
                else:
                    protein[k] = np.squeeze(protein[k], axis=-1)

    for k in ["seq_length", "num_alignments"]:
        if k in protein:
            protein[k] = protein[k][0]

    return protein

源码解读:

  • 该函数接收 protein(一个 包含蛋白质特征的字典)作为输入。
  • 主要任务:
    1. 将 one-hot aatype 转换为索引表示
    2. 移除 shape 为 (N, ..., 1) 的单维度
    3. 提取 seq_length 和 num_alignments 的实际数值
Step 1: 处理 aatype
protein["aatype"] = torch.argmax(protein["aatype"], dim=-1)
  • 输入 aatype(氨基酸类型)通常是 one-hot 编码
  • 通过 torch.argmax(..., dim=-1) 获取 索引
  • 目的:简化 aatype 的数据表示,使其直接存储氨基酸索引,而不是 one-hot 矩阵。
Step 2: 移除单维度
for k in [
    "domain_name",
    "msa",
    "num_alignments",
    "seq_length",
    "sequence",
    "superfamily",
    "deletion_matrix",
    "resolution",
    "between_segment_residues",
    "residue_index",
    "template_all_atom_mask",
]:
    if k in protein:
        final_dim = protein[k].shape[-1]  # 获取最后一维的大小
        if isinstance(final_dim, int) and final_dim == 1:
            if torch.is_tensor(protein[k]):
                protein[k] = torch.squeeze(protein[k], dim=-1)  # 去掉单维度
            else:
                protein[k] = np.squeeze(protein[k], axis=-1)
  • 遍历多个 protein 特征字段,检查它们是否存在。
  • 如果最后一维 final_dim 为 1,说明这个维度是无意义的单维度,需要去除:
    • 如果是 PyTorch 张量torch.Tensor),使用 torch.squeeze(dim=-1)
    • 如果是 NumPy 数组,使用 np.squeeze(axis=-1)
Step 3: 处理 seq_length 和 num_alignments
for k in ["seq_length", "num_alignments"]:
    if k in protein:
        protein[k] = protein[k][0]

seq_length 和 num_alignments 可能是 列表或张量,但它们的数值其实是一个单独的整数,因此需要转换成 标量值

结论

1️⃣ 转换 aatype: 从 one-hot 编码 转换成 索引表示
2️⃣ 移除无用的单维度: 让 msaresolutiondeletion_matrix 等数据符合 AlphaFold3 预期格式。
3️⃣ 转换 seq_length 和 num_alignments 为标量: 确保它们不会以张量形式存在,而是整数。

💡 最终作用:保证输入数据的维度符合 AlphaFold3 训练时的输入要求,提高数据处理效率。

相关文章:

  • 高效运行 QwQ-32B + 错误修复
  • Ubuntu 下 Docker 企业级运维指南:核心命令与最佳实践深度解析20250309
  • AI Copilot——维新派的贾维斯,守旧派的墓志铭(程序员视角)
  • Synchronized 锁升级过程
  • 推荐一些免费开源支持Vue3甘特图组件
  • Python批量将中文文件名称转为英文、拼音的方法
  • 《基于深度学习的图像修复技术研究与应用-图像修复》—3000字论文模板
  • C++之“string”类的模拟实现
  • 二叉树中的深搜
  • IPFS:下一代互联网传输协议
  • ARM 嵌入式处理器内核与架构深度剖析(2): ARM 处理器架构剖析
  • LOCKUP的场景和典型结构
  • 深入探讨 Docker 层次结构及其备份策略20250309
  • 【C++】C++11部分
  • 基于SpringBoot的在线付费问答系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • 【keil】一种将STM32的armcc例程转换为armclang的方式
  • 快速从C过度C++(一):namespace,C++的输入和输出,缺省参数,函数重载
  • 躲藏博弈:概率论与博弈论视角下的最优策略选择
  • PHP:格式化JSON为PHP语法格式
  • 【深入解析Inception网络:从V1到V3的理论演进与对比,包含pytorch实现Inception模块的代码】
  • php做网站主要怎么布局/企业微信营销管理软件
  • 西安品牌网站建设/东莞新闻头条新闻
  • 幻灯片在什么网站做/培训心得体会1000字通用
  • 网站建设需求单/河南省干部任免最新公示
  • 网站建设考试题/长沙谷歌seo
  • 企业搭建一个营销型网站多少钱/学seo建网站