我正在运行一个用Python实现并使用NumPy的算法.算法中计算成本最高的部分涉及求解一组线性系统(即调用numpy.linalg.solve().我想出了这个小基准:
import numpy as np
import time
# Create two large random matrices
a = np.random.randn(5000, 5000)
b = np.random.randn(5000, 5000)
t1 = time.time()
# That's the expensive call:
np.linalg.solve(a, b)
print time.time() - t1
Run Code Online (Sandbox Code Playgroud)
我一直在运行:
sysctl -n machdep.cpu.brand_string给我Intel(R)Core(TM)i7-4750HQ CPU @ 2.00GHz)c3.xlarge实例,具有4个vCPU.亚马逊宣称它们为"高频英特尔至强E5-2680 v2(Ivy Bridge)处理器"底线:
我也在其他基于OpenBLAS/Intel MKL的设置上尝试过它,运行时总是与我在EC2实例上得到的(模块化硬件配置)相当.
任何人都可以解释为什么Mac(使用Accelerate Framework)的性能提高了4倍?下面提供了有关NumPy/BLAS设置的更多详细信息.
numpy.show_config() 给我:
atlas_threads_info:
NOT AVAILABLE
blas_opt_info:
extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
extra_compile_args …Run Code Online (Sandbox Code Playgroud) 我正在使用以下方法在python中开发一个小型Web服务:
我有一个自定义HTTP标头,Unison-UUID我正在使用它来检索我的数据库中的信息.
这是(稍为简单的重写)片段,我遇到了麻烦:
uuid = flask.request.headers['Unison-UUID']
store = storm.locals.Store(my_database)
user = store.get(models.User, uuid)
Run Code Online (Sandbox Code Playgroud)
这个班User或多或少是这样的:
class User(Storm):
uuid = Unicode(primary=True)
# Other columns....
Run Code Online (Sandbox Code Playgroud)
上面的代码以下列方式失败:
File "/Users/lum/Documents/unison-recsys/www/api/unison/unison.py", line 27, in decorated
user = g.store.get(models.User, uuid)
File "/Users/lum/Documents/unison-recsys/venv/lib/python2.6/site-packages/storm/store.py", line 165, in get
variable = column.variable_factory(value=variable)
File "/Users/lum/Documents/unison-recsys/venv/lib/python2.6/site-packages/storm/variables.py", line 396, in parse_set
% (type(value), value))
TypeError: Expected unicode, found <type 'str'>: '00000000-0000-0000-0000-000000000009'
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会这样,我能做些什么呢.我认为Flask是100%unicode.
我找到的快速修复方法是解码标头值,即uuid = uuid.decode('utf-8').这真的需要做什么?这看起来有点骇人听闻.有没有办法直接获得unicode,而无需手动"解码"?
我正在尝试最小化以下功能scipy.optimize:

其梯度是这样的:

(对于那些感兴趣的人,这是成对比较的Bradley-Terry-Luce模型的似然函数.与逻辑回归密切相关.)
很明显,为所有参数添加常量不会改变函数的值.因此,我让\ theta_1 = 0.这里是目标函数的实现和python中的渐变(theta变为x这里):
def objective(x):
x = np.insert(x, 0, 0.0)
tiles = np.tile(x, (len(x), 1))
combs = tiles.T - tiles
exps = np.dstack((zeros, combs))
return np.sum(cijs * scipy.misc.logsumexp(exps, axis=2))
def gradient(x):
zeros = np.zeros(cijs.shape)
x = np.insert(x, 0, 0.0)
tiles = np.tile(x, (len(x), 1))
combs = tiles - tiles.T
one = 1.0 / (np.exp(combs) + 1)
two = 1.0 / (np.exp(combs.T) + 1)
mat = (cijs * one) + (cijs.T * two) …Run Code Online (Sandbox Code Playgroud)