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

维护者

owners

committers

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.5.tar.gz (8.4 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.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: GauDLutils-0.2.5.tar.gz
  • Upload date:
  • Size: 8.4 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.5.tar.gz
Algorithm Hash digest
SHA256 9acc9f9892acc50195c8a23b40bc6bf75d1554849c2fa5cf6737c610e49f3e2b
MD5 89fbcd8d6df67d44b2a6af37137d100e
BLAKE2b-256 36b5e22d30a87a1e25673eadb1a12e4be7986b8b23e7d087f057e0cfa4495af6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: GauDLutils-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7f0e7df316d0860032cda2f5ce3534e0bb8661c120200cbcf9058b3521e660d7
MD5 a4fd98da94bef68d85ba6f0c061bb9af
BLAKE2b-256 40fba966952a3689a71be594e4346d24fcbbfcac4bf494f53f2ec6be9f1c3c81

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