提交 28e99288 authored 作者: Nicolas Ballas's avatar Nicolas Ballas 提交者: Pascal Lamblin

make gradInputs and gradWeight work

上级 6b422288
...@@ -254,7 +254,9 @@ class AbstractConv2d_gradWeights(BaseAbstractConv2d): ...@@ -254,7 +254,9 @@ class AbstractConv2d_gradWeights(BaseAbstractConv2d):
img.broadcastable[0], img.broadcastable[0],
False, False] False, False]
output = img.type.__class__(dtype=img.type.dtype, output = img.type.__class__(dtype=img.type.dtype,
broadcastable=broadcastable) broadcastable=broadcastable)()
output.owner = None
#print output.type.owner
return Apply(self, [img, topgrad] + height_width, [output]) return Apply(self, [img, topgrad] + height_width, [output])
def perform(self, node, inp, out_): def perform(self, node, inp, out_):
...@@ -313,11 +315,11 @@ class AbstractConv2d_gradInputs(BaseAbstractConv2d): ...@@ -313,11 +315,11 @@ class AbstractConv2d_gradInputs(BaseAbstractConv2d):
kern.type.broadcastable[1], kern.type.broadcastable[1],
False, False] False, False]
output = kern.type.__class__(dtype=kern.type.dtype, output = kern.type.__class__(dtype=kern.type.dtype,
broadcastable=broadcastable) broadcastable=broadcastable)()
return Apply(self, [kern, topgrad] + height_width, [output]) return Apply(self, [kern, topgrad] + height_width, [output])
def perform(self, node, nodename, inp, out_, sub): def perform(self, node, nodename, inp, out_):
raise NotImplementedError('AbstractConv2d_gradWeight theano optimization failed') raise NotImplementedError('AbstractConv2d_gradWeight theano optimization failed')
def grad(self, inp, grads): def grad(self, inp, grads):
...@@ -510,7 +512,11 @@ register_specialize_device(local_conv2d_corrmm) ...@@ -510,7 +512,11 @@ register_specialize_device(local_conv2d_corrmm)
@local_optimizer([AbstractConv2d_gradWeights]) @local_optimizer([AbstractConv2d_gradWeights])
def local_conv2d_gradweight_corrmm(node): def local_conv2d_gradweight_corrmm(node):
img, topgrad, shape = node.inputs if len(node.inputs) == 3:
img, topgrad, shape = node.inputs
else:
img, topgrad = node.inputs
shape = None
if not isinstance(img.type, CudaNdarrayType) or \ if not isinstance(img.type, CudaNdarrayType) or \
not isinstance(topgrad.type, CudaNdarrayType): not isinstance(topgrad.type, CudaNdarrayType):
return None return None
...@@ -522,9 +528,12 @@ register_specialize_device(local_conv2d_gradweight_corrmm) ...@@ -522,9 +528,12 @@ register_specialize_device(local_conv2d_gradweight_corrmm)
@local_optimizer([AbstractConv2d_gradInputs]) @local_optimizer([AbstractConv2d_gradInputs])
def local_conv2d_gradinputs_corrmm(node): def local_conv2d_gradinputs_corrmm(node):
if len(node.inputs) == 3:
kern, topgrad, shape = node.inputs kern, topgrad, shape = node.inputs
if not isinstance(img.type, CudaNdarrayType) or \ else:
kern, topgrad = node.inputs
shape = None
if not isinstance(kern.type, CudaNdarrayType) or \
not isinstance(topgrad.type, CudaNdarrayType): not isinstance(topgrad.type, CudaNdarrayType):
return None return None
rval = GpuCorrMM_gradInputs(border_mode=node.op.border_mode, rval = GpuCorrMM_gradInputs(border_mode=node.op.border_mode,
......
...@@ -8,7 +8,10 @@ from theano.tests import unittest_tools as utt ...@@ -8,7 +8,10 @@ from theano.tests import unittest_tools as utt
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import theano.tensor.nnet.conv as conv_ref import theano.tensor.nnet.conv as conv_ref
import theano.tensor.nnet.abstract_conv2d as conv import theano.tensor.nnet.abstract_conv2d as conv
from theano.sandbox.cuda import float32_shared_constructor as shared from theano.sandbox.cuda import float32_shared_constructor as shared
from theano.sandbox.cuda.tests.test_conv_cuda_ndarray import py_conv
if theano.config.mode == 'FAST_COMPILE': if theano.config.mode == 'FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu') mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
...@@ -46,10 +49,63 @@ class TestConv2d(unittest.TestCase): ...@@ -46,10 +49,63 @@ class TestConv2d(unittest.TestCase):
res = f() res = f()
print res_ref.shape, res.shape print res_ref.shape, res.shape
utt.assert_allclose(res_ref, res) utt.assert_allclose(res_ref, res)
if verify_grad: # if verify_grad:
utt.verify_grad(conv.AbstractConv2d(border_mode="valid", # utt.verify_grad(conv.AbstractConv2d(border_mode="valid",
subsample=subsample), # subsample=subsample),
[inputs_val, filters_val]) # [inputs_val, filters_val])
def run_gradweight(self,
inputs_shape,
filters_shape,
subsample=(1, 1),
verify_grad=True,
mode=mode_with_gpu):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val.transpose((1, 0, 2, 3)))
filters = shared(filters_val.transpose((1, 0, 2, 3))[:,:,::-1,::-1])
c = conv.AbstractConv2d_gradWeights(border_mode="valid",
subsample=subsample)
c = c(inputs, filters)
f = theano.function([], c, mode)
res_ref = py_conv(inputs_val, filters_val, 'valid', subsample)
res = numpy.array(f()).transpose((1, 0, 2, 3))
utt.assert_allclose(res_ref, res)
# if verify_grad:
# utt.verify_grad(conv.AbstractConv2d(border_mode="valid",
# subsample=subsample),
# [inputs_val, filters_val])
def run_gradinput(self,
inputs_shape,
filters_shape,
subsample=(1, 1),
verify_grad=True,
mode=mode_with_gpu):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val.transpose(1, 0, 2, 3))
c = conv.AbstractConv2d_gradInputs(border_mode="valid",
subsample=subsample)
c = c(filters, inputs)
f = theano.function([], c, mode)
res_ref = py_conv(inputs_val, filters_val, 'full', subsample)
res = numpy.array(f()) #.transpose((1, 0, 2, 3))
print "2, ", res_ref.shape, res.shape
utt.assert_allclose(res_ref, res)
# if verify_grad:
# utt.verify_grad(conv.AbstractConv2d(border_mode="valid",
# subsample=subsample),
# [inputs_val, filters_val])
...@@ -75,10 +131,13 @@ class TestConv2d(unittest.TestCase): ...@@ -75,10 +131,13 @@ class TestConv2d(unittest.TestCase):
self.run_conv(inputs_shape=(16, 1, 2, 2), self.run_conv(inputs_shape=(16, 1, 2, 2),
filters_shape=(10, 1, 2, 2), filters_shape=(10, 1, 2, 2),
verify_grad=False, mode=mode) verify_grad=False, mode=mode)
self.run_conv(inputs_shape=(16, 1, 2, 2), self.run_gradweight(inputs_shape=(16, 1, 2, 2),
filters_shape=(10, 1, 2, 2), filters_shape=(10, 1, 2, 2),
subsample=(1, 1), verify_grad=False, mode=mode)
verify_grad=True,mode=mode) self.run_gradinput(inputs_shape=(1, 1, 2, 2),
filters_shape=(10, 1, 2, 2),
verify_grad=False, mode=mode)
# self.run_conv(inputs_shape=(16, 1, 8, 8), # self.run_conv(inputs_shape=(16, 1, 8, 8),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论