Giter Site home page Giter Site logo

coding-tips's Introduction

python-tips

调用self()

在深度学习pytorch框架中,class中定义了forward()函数,则在本类中,其他函数中,调用self(x),即调用forward函数

@修饰器的用法

def funA(desA):
 print("It's funA")
 
def funB(desB):
 print("It's funB")
 
@funA
def funC():
 print("It's funC")

把funC()赋值给funA的形参

hydra

通过给@hydra.main()装饰器函数传入一个config_path来指定参数文件
示例

@hydra.main(config_path="conf/config.yaml")
def test(cfg):
    print(cfg.pretty())

config.yaml会在你运行应用程序的时候自动加载。可以通过命令行输入的参数来对配置文件中的参数进行覆盖

深度学习训练tips

pytorch lightning 中文教程

https://zhuanlan.zhihu.com/p/319810661
https://zhuanlan.zhihu.com/p/353985363

一种较好的config读入方式 —— 1 读入yaml配置文件

default yaml文件 + 其他 yaml 文件
argparse 读入用户自定义的config文件,再通过定义load_config()函数,读入 default config 的yaml文件,结合parser.parse_known_args()update_config(config, unknown)函数,载入所有的yaml文件 示例

# Arguments
parser = argparse.ArgumentParser(
    description='Train a GAN with different regularization strategies.'
)
parser.add_argument('--config', type=str, help='Path to config file.')

args, unknown = parser.parse_known_args() 
config = load_config(args.config, 'configs/default.yaml')
config = update_config(config, unknown)

一种较好的config读入方式 —— 2 OmegaConfig + hydra

vscode tips

debug 相关

accelerate launch 的 launch.json文件编写

与使用python命令进行launch的区别在于,去掉"program": "${file}",,转而使用"module": "accelerate.commands.launch",,要注意的是,"args"也要做修改,第一项须为待调试的python文件名称
示例

// lanch json for diffusers textural inversion
"configurations": [
    {
        "name": "Python: 当前文件",
        "python": "/opt/anaconda3/envs/wj-ldm/bin/python",
        "type": "python",
        "module": "accelerate.commands.launch",
        "request": "launch",
        // "program": "${file}",
        "console": "integratedTerminal",
        "justMyCode": true,
        "cwd": "/data1/Documents/weijia/diffusers/examples/textual_inversion",
        "args": [
            "textual_inversion.py",
            "--pretrained_model_name_or_path=/data1/Documents/lioo/stable-diffusion-finetune/diffusers/examples/textual_inversion/textual_inversion_bosideng",
            "--train_data_dir=/data1/Documents/weijia/data/few_downcoat",
            "--learnable_property='subject'",
            "--placeholder_token='<bosideng2022-52>'",
            "--initializer_token='coat'",
            "--resolution=512",   
            "--train_batch_size=1",  
            "--max_train_steps=1000",   
            "--learning_rate=5.0e-04", 
            "--scale_lr",   
            "--lr_scheduler='polynomial'",
            "--lr_warmup_steps=0",   
            "--output_dir='/data1/Documents/weijia/diffusers/examples/textual_inversion/textual_inversion_bosideng'", 
            "--mixed_precision=fp16",
            " --use_8bit_adam",
        ],
        "env": {"CUDA_VISIBLE_DEVICES": "0, 1, 2, 3"}
    }
]

coding-tips's People

Contributors

bestyw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.