Skip to main content

tools for deeplearning developing

Project description

Gutils

各种工具库,如权重转换等

安装

pip install -v -e .

快速开始

1. 权重转化使用例子

  1. 观察源权重关键字以及目标权重关键字的区别
from gutils.ckpt_tr import WeightTrans

wt = WeightTrans(r"/path/to/source/weight.pdparams",r"/path/to/source/target.pdparams")

初始化后会自动打印关于权重文件的信息,如前缀出现的top3概率分析、source权重的key在target中出现的比例、source权重的key在target中出现的比例、未出现在target中和未出现在source中不同前缀的关键字分别列出10个例子。以下是一个例子:

-----------------------------------------------------
PREFIX ANALYSE:
    source_most_like_prefix: {'noise_estimator': 0.6201780415430254, 'latent_embedder': 0.3442136498516315, 'noise_scheduler': 0.03560830860534124}
    target_most_like_prefix: {'noise_estimator': 0.6763754045307432, 'latent_embedder': 0.2847896440129448, 'noise_scheduler': 0.03883495145631069}
Key word 'noise_estimator' most like to be a prefix of the source. 
Key word 'noise_estimator' most like to be a prefix of the target.
-----------------------------------------------------
KEY_WORD MISSING ANALYSE:
    Source targe in 0.9940652818991098 %, Target target in 0.9967637540453075 %:
    IN SOURCE NOT IN TARGET: 
        noise_estimator.outc.conv.conv.weight
        noise_estimator.outc.conv.conv.bias
        latent_embedder.perceiver.loss_fn.scaling_layer.shift
        latent_embedder.perceiver.loss_fn.scaling_layer.scale
        latent_embedder.perceiver.loss_fn.net.slice1.0.weight
        latent_embedder.perceiver.loss_fn.net.slice1.0.bias
        latent_embedder.perceiver.loss_fn.net.slice1.2.weight
        latent_embedder.perceiver.loss_fn.net.slice1.2.bias
        latent_embedder.perceiver.loss_fn.net.slice2.5.weight
        latent_embedder.perceiver.loss_fn.net.slice2.5.bias
        latent_embedder.perceiver.loss_fn.net.slice2.7.weight
        latent_embedder.perceiver.loss_fn.net.slice2.7.bias
    IN TARGET NOT IN SOURCE: 
        noise_estimator.outc.weight
        noise_estimator.outc.bias
  1. 根据关键字做出源关键字对目标关键字的映射方法,并重写 self.source2target_rule 方法 (该方法接收源key,返回一个新的key,用户在该方法中实现转换逻辑。),并执行 self.source2target 方法,第一次尝试进行权重转换。
from gutils.ckpt_tr import WeightTrans

class WeightTrans(WeightTrans):
    def source2target_rule(self, key):
        if 'noise_estimator.outc' in key:
            key = key.replace('noise_estimator.outc.conv.conv', 'noise_estimator.outc')
        return key
wt = WeightTrans(r"/path/to/source/weight.pdparams",r"/path/to/source/target.pdparams")
wt.source2target(r"/path/to/save/, "paddle")
  1. 若执行过程中出现权重的shape对应不正确,会产生一个警告,指出shape不同的权重对应的 新key以及源权重shape和目标权重shape,然后根据警告提示进行修改 self.transfer_weight 方法(该方法接收新key 以及写入该key 的权重,用户在该方法中实现权重转换的逻辑,注意一般权重的type是 flaot32),并执行 self.source2target 方法,再次尝试进行权重转换。
class WeightTrans(WeightTrans):
    def source2target_rule(self, key):
        if 'noise_estimator.outc' in key:
            key = key.replace('noise_estimator.outc.conv.conv', 'noise_estimator.outc')
        return key

    def transfer_weight(self, key, source_weight):
        if key == 'noise_estimator.cond_embedder.embedding.weight':
            return source_weight.astype('float32')
        shape_len = len(source_weight.shape)
        trs_idx = [i for i in range(shape_len)]
        if shape_len == 2:
            trs_idx = trs_idx[::-1]
        if  shape_len > 2:
            trs_idx = trs_idx[:-2] + [trs_idx[-1], trs_idx[-2]]
        if 'weight' in key:
            return source_weight.transpose(*trs_idx).astype('float32')
        else:
            return source_weight.astype('float32')

wt = WeightTrans(r"/path/to/source/weight.pdparams",r"/path/to/source/target.pdparams")
wt.source2target(r"/path/to/save/", "paddle")

注意: /path/to/save/ 不用写后缀 如 /root/dest,程序会自动根据第二个位置参数进行判断,并补全为 /root/dest.pdparams 或者 /root/dest.pth

版本信息

本项目的各版本信息和变更历史可以在[这里][changelog]查看。

维护者

owners

committers

Changelog

以下记录了项目中所有值得关注的变更内容,其格式基于Keep a Changelog

本项目版本遵守Semantic VersioningPEP-440

Unreleased

Added

  • 这里记录新添加的内容

Changed

  • 这里记录变更的内容

0.1.0 - 2023-09-27

Added

  • 创建项目

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

GauDLutils-0.2.4.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

GauDLutils-0.2.4-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file GauDLutils-0.2.4.tar.gz.

File metadata

  • Download URL: GauDLutils-0.2.4.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for GauDLutils-0.2.4.tar.gz
Algorithm Hash digest
SHA256 3e5c165fa4685f895880aa3e496d717cdf59d4bb2e4ebc174fbade01b30348c6
MD5 257ec23e3a3cb1efa19df44b5769ee0c
BLAKE2b-256 a67a3c309310d8f2333b3d52d7a801f3fce821d9c89a4b4fe48d95716848edb7

See more details on using hashes here.

File details

Details for the file GauDLutils-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: GauDLutils-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for GauDLutils-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ab150892829c85d40e26ab36986defa9aecf655551cabfe1cce48545e7c61fb0
MD5 dbda06a04248dd4452fb7393ff7bc6c3
BLAKE2b-256 e7e1289eb33536a2bd105289c3124c36c3b8e7add4a145abb26adbf176fcd271

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page