CuriousY A world with wonder

Using sphinx to build Python document

| Comment
  1. 使用sphinx-quickstart命令来初始化创建一些必要的文件(建议先创建一个docs文件夹,然后在其中执行该命令)。

  2. 使用sphinx-build命令来生成对应的文档,比如sphinx-build -b html source build会生成html格式的页面。当然,如果你在执行sphinx-quickstart命令时选择了同时生成Makefile的话,则可以通过make html来达到一样的效果。

  3. 使用sphinx-build命令来生成文档时,并不需要指定对应的项目路径,原因在于sphinx是直接在python代码中import你在rst文件中填写的模块的。因此,一定要确保在所有依赖包都安装好的virtualenv中来执行sphinx-build命令(sphinx最好也安装在该virtualenv中),并且,确保对应的项目路径可以在python环境中被import(把项目路径添加到PYTHONPATH环境变量中再执行命令或是在项目路径的父级目录执行命令)。 比如下面的rst:

    .. autoclass:: my_module.sub_module.ClassA
    

    实际上做的事情类似于:

    from my_module.sub_module import ClassA
    
  4. 使用sphinx-apidoc -f -o docs/source projectdir来自动生成对应于项目中每个函数的api的文档。

  5. 建议加入sphinx.ext.napoleon扩展,这样在转换代码中的docstring时会支持google和numpy的风格(example)。

  6. sphinx默认会渲染所有的rst格式的文件,如果想要使用mark down来书写文档的话,需要额外安装解析mark down的库,并设置sphinx来渲染它们(参考http://www.sphinx-doc.org/en/master/usage/markdown.html)。

  7. 避免重复渲染同一个文件:默认所有文件夹下的rst文件都会被sphinx渲染,所以在rst文件中直接引用该文件名即可,而不需要.. include:来引入。 比如,我有一个api.rst文件,我需要在index.rst文件中引用它作为目录:

    .. toctree::
       :maxdepth: 2
       :caption: Contents:
    
       api
    

回归分析二三事

| Comment
  1. 一些经典的回归分析算法有:linear regression, logistic regression, SVR, Ridge regression等;
  2. 这些经典的回归分析算法对于特征的选择要求比较高,比如:目标函数y=x1*x2+2(其中x1和x2为两个特征,y为输出),以x1和x2为输入并不能训练出一个能准确预测y的模型(即使是非线性的回归模型好像也不行),这时候就要以x1和x2的乘积作为一维特征作为输入来训练;
  3. 通过计算预测值与真值的方差以及r2(R-squared)来评估回归模型是否准确,其中r2约接近1则表示模型越准确;
  4. 有时候使用pyecharts来生成图表能达到比matlibplot更好的展现效果;
  5. 使用tensorflow的DNNRegressor训练时出现NaN loss during training的错误,有可能是因为某些特征的值太大了,经过几次训练迭代之后超出了float32的范围(当然也有可能是learning rate太高了,参考网上其他的解释),解决方法就是将这些特征通过转换来控制在某个取值范围内;
  6. 如果你期望回归预测的值只包含非负数(比如预测的是价格,负数是没有意义的),那么可以通过一些数学转换来达到目的,比如使用对数:在训练时将目标值处理为log(x),预测时把结果通过Smearing retransformation之类的方法重建出来(参考这里这里)(需要注意的是这种方式往往会降低预测的准确率)。或者,就直接把小于0的预测值设为0或某个固定的最小值;
  7. tensorflow的训练模型可以传入一个model_dir来把模型保存在一个文件夹内,如果要修改参数来重新训练的话,可能需要把之前的模型删除或重新选择一个新的路径来存储模型,否则可能会报错(猜测tensorflow会存储训练的中间状态,这样在修改比如steps参数时就不用从头开始迭代了);
  8. 对于tensorflow中现成的一些神经网络模型(比如DNNRegressor),在生成的模型文件夹中也同时存放了tensorboard所需要的文件,因此我们可以直接使用tensorboard命令来查看训练的过程。

PWA二三事

| Comment
  1. Web service worker is by default enabled in the project created by latest create-react-app. However, npm start do not enable web service workers (for better dev experience as no cache). Instead, you should (npm install -g serve if cmd serve not installed):

    npm run build
    serve -T -s build
    
  2. To solve the following error when testing the app locally:

    DOMException: Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script
    

    Please open the chrome with extra options, e.g.

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost:5000 --user-data-dir=/tmp/foo
    
  3. You can view the PWA status (e.g. the data stored in Local Storage/IndexedDB) by Chrome dev console (the Application tab);

  4. You can evaluate your PWA by Google lighthouse tool which is also availiable by Chrome dev console (the Audits tab); lighthouse

  5. Use library like pouchDB to save your app state into local computer storage, so that your PWA can work offline as normal.

Core dump in Linux

| Comment

How to enable core dump

Enable core dump

使用ulimit命令,比如:

  • 设置生成的core dump文件大小不受限制:ulimit -c unlimited
  • 查看当前的core dump限制(默认为0,即不生成core dump):ulimit -c

更推荐修改/etc/security/limits.conf设置文件中下面这一行,以保证此session之外的core dump设置依然生效:

*               soft    core            unlimited

Set the location/format of core dump file

修改/proc/sys/kernel/core_pattern文件即可。比如:

echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

其中,%后面的为特殊的格式:

	%p	pid
	%P	global pid (init PID namespace)
	%i	tid
	%I	global tid (init PID namespace)
	%u	uid (in initial user namespace)
	%g	gid (in initial user namespace)
	%d	dump mode, matches PR_SET_DUMPABLE and
		/proc/sys/fs/suid_dumpable
	%s	signal number
	%t	UNIX time of dump
	%h	hostname
	%e	executable filename (may be shortened)
	%E	executable path
Read more

About 'deque mutated during iteration'

| Comment

写代码用到Python标准库中的deque数据结构,结果在遍历时出现了如下的异常:

RuntimeError: deque mutated during iteration

一开始我理解为了在遍历deque时不能修改队列中的元素,尝试了一下发现不是这样子的:

from collections import deque

q = deque()
q.append(1)
q.append({'a': 1})
q.append(3)
v = iter(q)
next(v)  # 1
q[1]['b'] = 2
next(v)  # {'a': 1, 'b': 2}
next(v)  # 3

上面的代码可以正常运行。所以,这个异常也许表示在遍历时,deque长度不能发生变化?

from collections import deque

q = deque()
q.append(1)
q.append({'a': 1})
q.append(3)
v = iter(q)
next(v)  # 1
q.append(4)
next(v)  # raise RuntimeError: deque mutated during iteration

THAT’S IT!

所以,如果你的队列在遍历时的同时可能还会有新的元素插入进来的话,可以考虑使用list或是Queue.Queue,而不是deque。

| Page 3 of 23 |