小行星

热爱平淡,向往未知


  • 首页

  • 分类

  • 归档

  • 关于

  • 阅读排行

  • 搜索

pytorch 多了module

发表于 2019-01-Sat | 阅读次数:

使用了net = torch.nn.DataParallel(net)后会多一个module
如果没使用这个,就会少一个module.

truediv代替div

发表于 2019-01-Tue | 阅读次数:
1
2
def __truediv__():
pass

空格转成tab

发表于 2019-01-Sat | 阅读次数:
1
2
3
:set tabstop=2      " To match the sample file
:set noexpandtab " Use tabs, not spaces
:%retab! " Retabulate the whole file

python读文件

发表于 2019-01-Thu | 阅读次数:
1
2
3
4
5
6
7
8
9
10
11
import binascii


filePath = "mysong.mp3"
file = open(filePath, "rb")
with file:
byte = file.read(1)
hexadecimal = binascii.hexlify(byte)
decimal = int(hexadecimal, 16)
binary = bin(decimal)[2:].zfill(8)
print("hex: %s, decimal: %s, binary: %s" % (hexadecimal, decimal, binary))

python3调用c

发表于 2019-01-Thu | 阅读次数:

c函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int res;
if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int res;
if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}

setup.py

1
2
3
4
from setuptools import setup, Extension

great_module = Extension('great_module', sources=["great_module.c"])
setup(ext_modules=[great_module])

或者是用gcc

1
g++ -fPIC -shared great_class_wrap.cxx -o _great_class.so  -I/usr/include/python2.7/ -lpython2.7

原始的c函数文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args)
{
int _a;
int res;

if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initgreat_module(void) {
(void) Py_InitModule("great_module", GreateModuleMethods);
}
1
gcc -fPIC -shared great_module.c -o great_module.so -I/home/xxx/miniconda2/envs/env3.6/include/python3.6m -lpython3.6m

多个参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <Python.h>

int great_function(int a, int b) {
return a + b;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int _b;
int res;
if (!PyArg_ParseTuple(args, "ii", &_a, &_b))
return NULL;
res = great_function(_a, _b);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}

动态库查找路径

发表于 2019-01-Thu | 阅读次数:
1
2
3
gcc --print-search-dirs 

ldconfig -p | grep python

网络权重正交化

发表于 2018-12-Sun | 阅读次数:

$F^n$ 的操作和矩阵的特征根有关系,如果特征根不是和1接近,那么对于RNN来说
梯度会爆炸。 因为里面包含了多个这样的操作。

然后正交矩阵的特征根都是1/-1, 所以能比较好的保持这种性质。

ffmpeg使用

发表于 2018-12-Fri | 阅读次数:

图片放大缩小

1
2
ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png
ffmpeg -i input.jpg -vf scale=320:-1 output_320.png

numpy None转nan

发表于 2018-12-Tue | 阅读次数:
1
a = a.astype(float)

pytorch按batch softmax

发表于 2018-12-Tue | 阅读次数:
1
2
3
x = Variable(torch.cat((torch.ones(1, 1, 10, 10), torch.ones(1, 1, 10, 10)*2), dim=0))
softmax = nn.Softmax(dim=1)
y = softmax(x.view(2, -1)).view(2, 1, 10, 10)
1…345…59
fangyh

fangyh

最爱的是那苍穹之外的浩渺宇宙

588 日志
4 分类
66 标签
© 2020 fangyh
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.3
|本站总访问量次