【DeepLearning笔记】python规范书写

1、代码模块书写规范

1.1、导入包

导入包需要注意分段

import os
os.environ['MKL_NUM_THREADS'] = '1'

from functools import partial
import random
import wandb
import sys
import collections

# Local imports
from data_loaders.data_manager import DataManager
from utils.utils import *

注:os.environ[‘MKL_NUM_THREADS’] = ‘1’
pytorch以及tensorflow的多线程输入设定过大,一般推荐较大数据流4线程,较小2线程。具体问题具体分析,要看数据输入是否是训练速度优化的瓶颈。
numpy或者opencv等的多线程操作或者tensorflow以及pytorch在cpu运行上的op。这些模块使用OMP或者MKL进行多线程加速,一般默认为cpu线程总数的一半,十分浪费计算力,推荐使用4线程,详见下表。

1.2、简化config

通过合并多个config,使用startswith识别可以减少config个数。

config['MODEL_NAME'].lower().startswith('stare'):

1.3、@staticmethod

常用在数据读取部分,可以省略调用前初始化类的过程。

class DataManager(object):
    """ Give me your args I'll give you a path to load the dataset with my superawesome AI """

    @staticmethod
    def load(config: Union[dict, FancyDict]) -> Callable:

1.4、函数声明规范

函数声明部分一般需要包含Decisions函数简介、:param *:参数介绍、:return:返回数据介绍。其中参数介绍与返回数据介绍部分均需说明数据类型。

    @staticmethod
    def get_alternative_graph_repr(raw: Union[List[List[int]], np.ndarray], config: dict) \
            -> Dict[str, np.ndarray]:
        """
        Decisions:

            Quals are represented differently here, i.e., more as a coo matrix
            s1 p1 o1 qr1 qe1 qr2 qe2    [edge index column 0]
            s2 p2 o2 qr3 qe3            [edge index column 1]

            edge index:
            [ [s1, s2],
              [o1, o2] ]

            edge type:
            [ p1, p2 ]

            quals will looks like
            [ [qr1, qr2, qr3],
              [qe1, qr2, qe3],
              [0  , 0  , 1  ]       <- obtained from the edge index columns

        :param raw: [[s, p, o, qr1, qe1, qr2, qe3...], ..., [...]]
            (already have a max qualifier length padded data)
        :param config: the config dict
        :return: output dict
        """

1.5、代码模块声明

在代码中常需要在下一个新的模块前声明新模块功能。

    """
        Make the model.
    """

1.6、传入参数设置

import argparse
def parse_config():
    parser = argparse.ArgumentParser()
    parser.add_argument('--max_len', type=int, default=128)
    parser.add_argument('--ckpt_path', type=str)
    parser.add_argument('--test_data',type=str)
    parser.add_argument('--out_path',type=str)
    parser.add_argument('--gpu_id',type=int, default
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值