提交 33d35144 authored 作者: Nicolas Ballas's avatar Nicolas Ballas 提交者: Pascal Lamblin

update test

上级 12cc6f02
...@@ -539,7 +539,6 @@ def local_conv2d_gradinputs_corrmm(node): ...@@ -539,7 +539,6 @@ def local_conv2d_gradinputs_corrmm(node):
@local_optimizer([AbstractConv2d]) @local_optimizer([AbstractConv2d])
def local_conv2d_cpu(node): def local_conv2d_cpu(node):
import pdb; pdb.set_trace()
if not isinstance(node.op, AbstractConv2d): if not isinstance(node.op, AbstractConv2d):
return None return None
...@@ -559,24 +558,29 @@ register_specialize_device(local_conv2d_cpu) ...@@ -559,24 +558,29 @@ register_specialize_device(local_conv2d_cpu)
@local_optimizer([AbstractConv2d_gradWeights]) @local_optimizer([AbstractConv2d_gradWeights])
def local_conv2d_gradweight_cpu(node): def local_conv2d_gradweight_cpu(node):
import pdb; pdb.set_trace()
## len is 4 all the time
img, topgrad, shape = node.inputs img, topgrad, shape = node.inputs
if isinstance(img.type, CudaNdarrayType) or \ if isinstance(img.type, CudaNdarrayType) or \
isinstance(topgrad.type, CudaNdarrayType): isinstance(topgrad.type, CudaNdarrayType):
return None return None
if node.op.border_mode not in ['full', 'valid']:
return None
if (node.op.border_mode == 'valid' and node.op.subsample != (1, 1)) or \
node.op.imshp is None or node.op.kshp is None: if node.op.border_mode == 'valid' and \
(node.op.subsample != (1, 1) or node.op.imshp is None or node.op.kshp is None):
# Use the gradient as defined in conv3D, because the implementation # Use the gradient as defined in conv3D, because the implementation
# by Conv is slow (about 3x slower than conv3D, and probably 10x # by Conv is slow (about 3x slower than conv3D, and probably 10x
# slower than it could be), nad incorrect when subsample > 2. # slower than it could be), nad incorrect when subsample > 2.
# build a "node", that should be equivalent to the one given by # build a "node", that should be equivalent to the one given by
# self.make_node, but using convGrad3D instead. # self.make_node, but using convGrad3D instead.
if not node.op.filter_flip:
topgrad = topgrad[:, :, ::-1, ::-1] # flip them
shuffled_img = img.dimshuffle(0, 2, 3, 'x', 1) shuffled_img = img.dimshuffle(0, 2, 3, 'x', 1)
shuffled_topgrad = topgrad.dimshuffle(0, 2, 3, 'x', 1) shuffled_topgrad = topgrad.dimshuffle(0, 2, 3, 'x', 1)
print shape
rval = convGrad3D(V=shuffled_img, rval = convGrad3D(V=shuffled_img,
d=(node.op.subsample[0], node.op.subsample[1], 1), d=(node.op.subsample[0], node.op.subsample[1], 1),
WShape=(shape[0], shape[2], shape[3], 1, shape[1]), WShape=(shape[0], shape[2], shape[3], 1, shape[1]),
...@@ -585,10 +589,11 @@ def local_conv2d_gradweight_cpu(node): ...@@ -585,10 +589,11 @@ def local_conv2d_gradweight_cpu(node):
rval = theano.tensor.addbroadcast(rval, 3) rval = theano.tensor.addbroadcast(rval, 3)
return [rval.dimshuffle(0, 4, 1, 2)] return [rval.dimshuffle(0, 4, 1, 2)]
if node.op.imshp is None or node.op.kshp is None:
return None
####### Determine gradient on kernels ######## ####### Determine gradient on kernels ########
assert len(node.op.imshp) == 4 and len(node.op.kshp) == 4 assert len(node.op.imshp) == 4 and len(node.op.kshp) == 4
print "here0", node.op.imshp[2:], node.op.kshp[2:]
import pdb; pdb.set_trace()
outshp = ConvOp.getOutputShape(node.op.imshp[2:], outshp = ConvOp.getOutputShape(node.op.imshp[2:],
node.op.kshp[2:], node.op.subsample, node.op.kshp[2:], node.op.subsample,
...@@ -596,23 +601,19 @@ def local_conv2d_gradweight_cpu(node): ...@@ -596,23 +601,19 @@ def local_conv2d_gradweight_cpu(node):
fulloutshp = ConvOp.getOutputShape(node.op.imshp[2:], fulloutshp = ConvOp.getOutputShape(node.op.imshp[2:],
node.op.kshp[2:], (1, 1), node.op.kshp[2:], (1, 1),
node.op.border_mode) node.op.border_mode)
print outshp, fulloutshp
#newimg = img.dimshuffle((1, 0, 2, 3)) newimg = img.dimshuffle((1, 0, 2, 3))
#newtopgrad = topgrad.dimshuffle((1, 0, 2, 3)) newtopgrad = topgrad.dimshuffle((1, 0, 2, 3))
newimg = img
newtopgrad = topgrad
if node.op.border_mode == 'valid': if node.op.border_mode == 'valid':
print "here1", node.op.imshp, node.op.kshp, fulloutshp
(img, filters) = (newimg, newtopgrad) (img, filters) = (newimg, newtopgrad)
kshp_logical = fulloutshp kshp_logical = fulloutshp
kshp_logical_top_aligned = False kshp_logical_top_aligned = False
imshp_logical = None imshp_logical = None
(bsize, nkern) = (node.op.imshp[0], node.op.kshp[0]) (bsize, nkern) = (node.op.imshp[1], node.op.kshp[0])
imshp = (bsize, node.op.imshp[1], node.op.imshp[2]) imshp = (node.op.imshp[0], node.op.imshp[2], node.op.imshp[3])
kshp = node.op.kshp[2:] kshp = outshp
elif node.op.border_mode == 'full': elif node.op.border_mode == 'full':
(img, filters) = (newtopgrad, newimg) (img, filters) = (newtopgrad, newimg)
kshp_logical = None kshp_logical = None
...@@ -622,25 +623,20 @@ def local_conv2d_gradweight_cpu(node): ...@@ -622,25 +623,20 @@ def local_conv2d_gradweight_cpu(node):
fulloutshp[1]) fulloutshp[1])
(bsize, nkern) = (node.op.kshp[0], node.op.imshp[1]) (bsize, nkern) = (node.op.kshp[0], node.op.imshp[1])
imshp = (node.op.imshp[0], outshp[0], outshp[1]) imshp = (node.op.imshp[0], outshp[0], outshp[1])
kshp = node.op.imshp[1:] kshp = node.op.imshp[2:]
else: else:
raise NotImplementedError( raise NotImplementedError(
'Only [full,valid] modes are currently supported.') 'Only [full,valid] modes are currently supported.')
print "here2", node.op.imshp, node.op.kshp, fulloutshp
if node.op.filter_flip: if node.op.filter_flip:
filters = filters[:, :, ::-1, ::-1] # flip them filters = filters[:, :, ::-1, ::-1] # flip them
dw = ConvOp(imshp, kshp, nkern, bsize, 1, 1, output_mode='valid', dw = ConvOp(imshp, kshp, nkern, bsize, 1, 1, output_mode='valid',
unroll_batch=None, unroll_kern=None, unroll_patch=None, unroll_batch=None, unroll_kern=None, unroll_patch=None,
imshp_logical=imshp_logical, imshp_logical=imshp_logical,
kshp_logical=kshp_logical, kshp_logical=kshp_logical,
kshp_logical_top_aligned=kshp_logical_top_aligned, kshp_logical_top_aligned=kshp_logical_top_aligned,
direction_hint='bprop weights') direction_hint='bprop weights')
#dw = ConvOp(output_mode='valid')
res = dw(img, filters) res = dw(img, filters)
print "here3", node.op.imshp, node.op.kshp, fulloutshp
res = res.dimshuffle((1, 0, 2, 3)) res = res.dimshuffle((1, 0, 2, 3))
return [res] return [res]
register_specialize_device(local_conv2d_gradweight_cpu) register_specialize_device(local_conv2d_gradweight_cpu)
...@@ -649,53 +645,50 @@ register_specialize_device(local_conv2d_gradweight_cpu) ...@@ -649,53 +645,50 @@ register_specialize_device(local_conv2d_gradweight_cpu)
@local_optimizer([AbstractConv2d_gradInputs]) @local_optimizer([AbstractConv2d_gradInputs])
def local_conv2d_gradinputs_cpu(node): def local_conv2d_gradinputs_cpu(node):
import pdb; pdb.set_trace()
kern, topgrad, shape = node.inputs kern, topgrad, shape = node.inputs
if isinstance(kern.type, CudaNdarrayType) or \ if isinstance(kern.type, CudaNdarrayType) or \
isinstance(topgrad.type, CudaNdarrayType): isinstance(topgrad.type, CudaNdarrayType):
return None return None
print "here4a", node.op.imshp, node.op.kshp if node.op.border_mode not in ['full', 'valid']:
return None
if node.op.border_mode == 'valid' and node.op.subsample != (1, 1): ### Conv 3d implementation, needed when subsample > 2
# Use the gradient as defined in conv3D, because the implementation if node.op.border_mode == 'valid' and \
# by Conv is slow (about 3x slower than conv3D, and probably 10x (node.op.subsample != (1, 1) or node.op.imshp is None or node.op.kshp is None):
# slower than it could be), nad incorrect when subsample > 2. if node.op.filter_flip:
# build a "node", that should be equivalent to the one given by kern = kern[:, :, ::-1, ::-1]
# self.make_node, but using convGrad3D instead.
shuffled_kern = kern.dimshuffle(0, 2, 3, 'x', 1) shuffled_kern = kern.dimshuffle(0, 2, 3, 'x', 1)
shuffled_topgrad = topgrad.dimshuffle(0, 2, 3, 'x', 1) shuffled_topgrad = topgrad.dimshuffle(0, 2, 3, 'x', 1)
b = T.zeros((kern.shape[1])) b = theano.tensor.zeros_like(shuffled_kern[0, 0, 0, 0, :])
rval = ConvTransp3D(W=shuffled_kern, b=b, rval = convTransp3D(W=shuffled_kern, b=b,
d=(op.subsample[0], op.subsample[1], 1), d=(node.op.subsample[0], node.op.subsample[1], 1),
H=shuffled_topgrad, H=shuffled_topgrad,
RShape=(shape[0], shape[1], 1)) RShape=(shape[2], shape[3], 1))
rval = theano.tensor.addbroadcast(rval, 3)
return [rval.dimshuffle(0, 4, 1, 2)] return [rval.dimshuffle(0, 4, 1, 2)]
####### Determine gradient on inputs ######## ### Conv2d Implementation
if node.op.imshp is None or node.op.kshp is None:
return None
mode = 'valid' mode = 'valid'
if not node.op.border_mode == 'full': if not node.op.border_mode == 'full':
mode = 'full' mode = 'full'
filters = kern.dimshuffle((1, 0, 2, 3)) filters = kern.dimshuffle((1, 0, 2, 3))
if node.op.filter_flip:
filters = filters[:, :, ::-1, ::-1]
outshp = ConvOp.getOutputShape(node.op.imshp[2:], outshp = ConvOp.getOutputShape(node.op.imshp[2:],
node.op.kshp[2:], node.op.subsample, node.op.kshp[2:], node.op.subsample,
node.op.border_mode) node.op.border_mode)
fulloutshp = ConvOp.getOutputShape(node.op.imshp[2:], fulloutshp = ConvOp.getOutputShape(node.op.imshp[2:],
node.op.kshp[2:], (1, 1), node.op.kshp[2:], (1, 1),
node.op.border_mode) node.op.border_mode)
nkern = node.op.kshp[1] nkern = node.op.imshp[1]
imshp = (nkern, outshp[0], outshp[1]) imshp = (node.op.kshp[0], outshp[0], outshp[1])
imshp_logical = (nkern, fulloutshp[0], fulloutshp[1]) imshp_logical = (node.op.kshp[0], fulloutshp[0], fulloutshp[1])
if node.op.filter_flip:
filters = filters[:, :, ::-1, ::-1]
print "here4", imshp, node.op.kshp, nkern
din = ConvOp(imshp, din = ConvOp(imshp,
node.op.kshp[2:], node.op.kshp[2:],
nkern, nkern,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论