打印中间过程信息
每次迭代(iteration)尤其是每次 epoch 的结果信息
- 观察收敛性
- 是否如预期,便于调试;
- 对一些执行时间较长,需要打印一些信息,说明程序正在执行,比如从 url 读取数据到本地(时态一定是现在进行时)
if (not os.path.isfile(dataset)) and dataset == 'mnist.pkl.gz':
origin = 'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
print 'Downloading data from %s' % origin
urllib.urlretrieve(origin, dataset)
清晰的注释
# Standard Libraries
import cPickle
import gzip
# Third-party library
import numpy as np
辅助函数与辅助类
辅助函数和辅助类的目的就是:
- 为了提供更好用的 api,
- 为了代码的可复用;
单元测试 —— 构建大型框架的前提
对函数返回值进行判断
返回值往往以为着一种状态 status,比如返回 bool 值,成功或者失败,对返回值进行提高是程序健壮性的重要手段。
std::find()
auto pos = std::find(vec.begin(), vec.end(), val); if (pos == vec.end()) ... else ...
str.find()
auto pos = str.find('...'); if (pos == string::npos) ....
文件打开
与“对函数的返回值进行判断”相对的即是,我们要设计能够返回程序执行状态信息的函数。
换行/断句/喘气
shared_x = thean.shared(
np.asarray(data[0], dtype=theano.config.floatX), borrow=True)
shared_y = thean.shared(
np.asarray(data[1], dtype=theano.config.floatX), borrow=True)
跨平台(操作系统)应用程序
# timeit.py
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
所以我们在客户端进行调用时,要有意识地使用 timeit.default_timer() ,出于性能的考虑:
import timeit
t0 = timeit.default_timer()
...
t1 = timeit.default_timer()