提交 bcb902e9 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix shape errors in the gradient (but it's still not the right value).

上级 09658800
...@@ -50,15 +50,14 @@ def gemv(alpha, A, x, beta, y): ...@@ -50,15 +50,14 @@ def gemv(alpha, A, x, beta, y):
def ger(alpha, x, y, A): def ger(alpha, x, y, A):
print A.shape, x.shape, y.shape
assert A.shape[1] == x.shape[0] assert A.shape[1] == x.shape[0]
assert A.shape[0] == y.shape[0] assert A.shape[0] == y.shape[0]
handle = scikits.cuda.misc._global_cublas_handle handle = scikits.cuda.misc._global_cublas_handle
cublas.cublasSger(handle, A.shape[0], A.shape[1], alpha, cublas.cublasSger(handle, A.shape[1], A.shape[0], alpha,
x.gpudata, x.strides[0],
y.gpudata, y.strides[0], y.gpudata, y.strides[0],
x.gpudata, x.strides[0],
A.gpudata, A.strides[0]) A.gpudata, A.strides[0])
...@@ -113,9 +112,10 @@ class SparseBlockGemvDS(GpuOp): ...@@ -113,9 +112,10 @@ class SparseBlockGemvDS(GpuOp):
go = grads[0] go = grads[0]
# might revise that interface to not have a huge output # might revise that interface to not have a huge output
Wgrad = sparse_block_outer_ss(W.zeros_like(), go, h, Wgrad = sparse_block_outer_ss(W.zeros_like().dimshuffle((1, 0, 3, 2)),
outputIdx, inputIdx) go, h, outputIdx, inputIdx)
hgrad = sparse_block_gemv_ds(h.zeros_like(), W.T, go, hgrad = sparse_block_gemv_ds(h.zeros_like(), W.dimshuffle((1, 0, 3, 2)),
go,
outputIdx, inputIdx) outputIdx, inputIdx)
return [go, Wgrad, hgrad, return [go, Wgrad, hgrad,
grad_undefined(self, 3, inputIdx, grad_undefined(self, 3, inputIdx,
...@@ -146,9 +146,9 @@ class SparseBlockOuterSS(GpuOp): ...@@ -146,9 +146,9 @@ class SparseBlockOuterSS(GpuOp):
o = o.copy() o = o.copy()
for i in range(x.shape[0]): for i in range(x.shape[0]):
out_id = xIdx[i] inp_id = xIdx[i]
for j in range(y.shape[0]): for j in range(y.shape[0]):
inp_id = yIdx[j] out_id = yIdx[j]
ger(numpy.float32(1.0), x[i], ger(numpy.float32(1.0), x[i],
y[j], o[inp_id, out_id]) y[j], o[inp_id, out_id])
......
...@@ -90,3 +90,21 @@ def test_blocksparse_op_grad(): ...@@ -90,3 +90,21 @@ def test_blocksparse_op_grad():
return sparse_block_gemv_ds(b.take(oIdx, axis=0), W, h, iIdx, oIdx) return sparse_block_gemv_ds(b.take(oIdx, axis=0), W, h, iIdx, oIdx)
utt.verify_grad(f, [b_val, W_val, h_val]) utt.verify_grad(f, [b_val, W_val, h_val])
def test_blocksparse_op_grad2():
b = tensor.fmatrix()
W = tensor.ftensor4()
h = tensor.fmatrix()
iIdx = tensor.lvector()
oIdx = tensor.lvector()
o = sparse_block_gemv_ds(b.take(oIdx, axis=0), W, h, iIdx, oIdx)
go = theano.grad(o.sum(), [b, W, h])
f = theano.function([W, h, iIdx, b, oIdx], go)
W_val, h_val, iIdx_val, b_val, oIdx_val = blocksparse_data()
# just make sure that it runs correcly and all the shapes are ok.
f(W_val, h_val, iIdx_val, b_val, oIdx_val)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论