通过矩阵运算有效地实现分解机?

时间:2018-07-02 18:48:52

标签: numpy tensorflow matrix machine-learning matrix-multiplication

链接在这里:https://www.csie.ntu.edu.tw/~r01922136/slides/ffm.pdf(幻灯片5-6)

给出以下矩阵:

X : n * d 
W : d * k

有没有一种仅使用矩阵运算(例如numpy,tensorflow)来计算n x 1矩阵的有效方法,其中第j个元素是:

编辑: 当前的尝试是这样,但显然空间效率不是很高,因为它需要存储大小为n*d*d的矩阵:

n = 1000
d = 256
k = 32

x = np.random.normal(size=[n,d])
w = np.random.normal(size=[d,k])

xxt = np.matmul(x.reshape([n,d,1]),x.reshape([n,1,d]))
wwt = np.matmul(w.reshape([1,d,k]),w.reshape([1,k,d]))
output = xxt*wwt
output = np.sum(output,(1,2))

1 个答案:

答案 0 :(得分:0)

避免使用大型临时数组

并非所有类型的算法都容易或显然可以向量化。可以使用regressor = estimator.DNNRegressor(feature_columns=my_feature_columns, label_dimension=2, hidden_units=hidden_layers, model_dir=MODEL_PATH) 来重写np.sum(xxt*wwt)。这应该比您的解决方案要快,但是还有其他一些限制(例如,没有多线程)。

为此,我建议使用像Numba这样的编译器。

示例

np.einsum

衡量效果

import numpy as np
import numba as nb
import time

@nb.njit(fastmath=True,parallel=True)
def factorization_nb(w,x):
  n = x.shape[0]
  d = x.shape[1]
  k = w.shape[1]

  output=np.empty(n,dtype=w.dtype)
  wwt=np.dot(w.reshape((d,k)),w.reshape((k,d)))

  for i in nb.prange(n):
    sum=0.
    for j in range(d):
      for jj in range(d):
        sum+=x[i,j]*x[i,jj]*wwt[j,jj]
    output[i]=sum
  return output

def factorization_orig(w,x):
  n = x.shape[0]
  d = x.shape[1]
  k = w.shape[1]

  xxt = np.matmul(x.reshape([n,d,1]),x.reshape([n,1,d]))
  wwt = np.matmul(w.reshape([1,d,k]),w.reshape([1,k,d]))
  output = xxt*wwt
  output = np.sum(output,(1,2))

  return output

时间

n = 1000
d = 256
k = 32

x = np.random.normal(size=[n,d])
w = np.random.normal(size=[d,k])

#first call has some compilation overhead
res_1=factorization_nb(w,x)
t1=time.time()
for i in range(100):
  res_1=factorization_nb(w,x)
  #res_2=factorization_orig(w,x)

print(time.time()-t1)