提交 03e77233 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #3095 from harlouci/flake8_v4

flake8 for tensor/nnet/nnet.py
from __future__ import print_function from __future__ import print_function
import numpy as N
from six.moves import xrange from six.moves import xrange
import theano import theano
from theano.tensor import basic as T from theano.tensor import basic as T
import numpy as N # from util import strutil
#from util import strutil
from theano.tensor.blas_headers import blas_header_text, blas_header_version from theano.tensor.blas_headers import blas_header_text, blas_header_version
from theano.tensor.blas import ldflags from theano.tensor.blas import ldflags
from theano.misc import strutil from theano.misc import strutil
...@@ -72,26 +74,28 @@ class Conv3D(theano.Op): ...@@ -72,26 +74,28 @@ class Conv3D(theano.Op):
def grad(self, inputs, output_gradients): def grad(self, inputs, output_gradients):
V, W, b, d = inputs V, W, b, d = inputs
dCdH , = output_gradients dCdH, = output_gradients
# make all of these ops support broadcasting of scalar b to vector b and eplace the zeros_like in all their grads # make all of these ops support broadcasting of scalar b to vector b and eplace the zeros_like in all their grads
# print dCdH.broadcastable # print dCdH.broadcastable
# print "dCdH.broadcastable" # print "dCdH.broadcastable"
# quit(-1) # quit(-1)
#dCdH = printing.Print("dCdH = ",["shape"]) # dCdH = printing.Print("dCdH = ",["shape"])
# Make sure the broadcasting pattern of the gradient is the the same # Make sure the broadcasting pattern of the gradient is the the same
# as the initial variable # as the initial variable
dCdV = ConvTransp3D.convTransp3D(W, T.zeros_like(V[0, 0, 0, 0, :]), d, dCdH, V.shape[1:4]) dCdV = theano.tensor.nnet.convTransp3D(
W, T.zeros_like(V[0, 0, 0, 0, :]), d, dCdH, V.shape[1:4])
dCdV = T.patternbroadcast(dCdV, V.broadcastable) dCdV = T.patternbroadcast(dCdV, V.broadcastable)
WShape = W.shape WShape = W.shape
dCdW = ConvGrad3D.convGrad3D(V, d, WShape, dCdH) dCdW = theano.tensor.nnet.convGrad3D(V, d, WShape, dCdH)
dCdW = T.patternbroadcast(dCdW, W.broadcastable) dCdW = T.patternbroadcast(dCdW, W.broadcastable)
dCdb = T.sum(dCdH, axis=(0, 1, 2, 3)) dCdb = T.sum(dCdH, axis=(0, 1, 2, 3))
dCdb = T.patternbroadcast(dCdb, b.broadcastable) dCdb = T.patternbroadcast(dCdb, b.broadcastable)
dCdd = grad_undefined(self, 3, inputs[3], dCdd = grad_undefined(
"The gradient of Conv3D with respect to the convolution" +\ self, 3, inputs[3],
" stride is undefined because Conv3D is only defined for" +\ "The gradient of Conv3D with respect to the convolution"
" integer strides.") " stride is undefined because Conv3D is only defined for"
" integer strides.")
if 'name' in dir(dCdH) and dCdH.name is not None: if 'name' in dir(dCdH) and dCdH.name is not None:
dCdH_name = dCdH.name dCdH_name = dCdH.name
...@@ -113,11 +117,13 @@ class Conv3D(theano.Op): ...@@ -113,11 +117,13 @@ class Conv3D(theano.Op):
else: else:
b_name = 'anon_b' b_name = 'anon_b'
dCdV.name = 'Conv3D_dCdV(dCdH='+dCdH_name+',V='+V_name+')' dCdV.name = 'Conv3D_dCdV(dCdH=' + dCdH_name + ',V=' + V_name + ')'
dCdW.name = 'Conv3D_dCdW(dCdH='+dCdH_name+',V='+V_name+',W='+W_name+')' dCdW.name = ('Conv3D_dCdW(dCdH=' + dCdH_name + ',V=' + V_name +
dCdb.name = 'Conv3D_dCdb(dCdH='+dCdH_name+',V='+V_name+',W='+W_name+',b='+b_name+')' ',W=' + W_name + ')')
dCdb.name = ('Conv3D_dCdb(dCdH=' + dCdH_name + ',V=' + V_name +
',W=' + W_name + ',b=' + b_name + ')')
return [ dCdV, dCdW, dCdb, dCdd ] return [dCdV, dCdW, dCdb, dCdd]
def perform(self, node, inputs, output_storage): def perform(self, node, inputs, output_storage):
V, W, b, d = inputs V, W, b, d = inputs
...@@ -144,7 +150,7 @@ class Conv3D(theano.Op): ...@@ -144,7 +150,7 @@ class Conv3D(theano.Op):
output_width = T.floor((vidWidth - filterWidth) // dc) + 1 output_width = T.floor((vidWidth - filterWidth) // dc) + 1
output_dur = T.floor((vidDur - filterDur) // dt) + 1 output_dur = T.floor((vidDur - filterDur) // dt) + 1
rval = (batch_size, output_height, output_width, output_dur, output_channels ) rval = (batch_size, output_height, output_width, output_dur, output_channels)
return [rval] return [rval]
...@@ -155,7 +161,7 @@ class Conv3D(theano.Op): ...@@ -155,7 +161,7 @@ class Conv3D(theano.Op):
return ldflags() return ldflags()
def c_compile_args(self): def c_compile_args(self):
flags = ldflags(libs=False, flags=True) flags = ldflags(libs=False, flags=True)
return flags return flags
def c_lib_dirs(self): def c_lib_dirs(self):
...@@ -170,7 +176,7 @@ class Conv3D(theano.Op): ...@@ -170,7 +176,7 @@ class Conv3D(theano.Op):
H = outputs[0] H = outputs[0]
codeSource = """ codeSource = """
///////////// < code generated by Conv3D > ///////////// < code generated by Conv3D >
//printf("\t\t\t\tConv3D c code\\n"); //printf("\t\t\t\tConv3D c code\\n");
...@@ -320,13 +326,13 @@ class Conv3D(theano.Op): ...@@ -320,13 +326,13 @@ class Conv3D(theano.Op):
VV, WV, bv, dv = node.inputs VV, WV, bv, dv = node.inputs
HV = node.outputs[0] HV = node.outputs[0]
if (theano.config.blas.ldflags and if (theano.config.blas.ldflags and
VV.dtype == WV.dtype and HV.dtype == VV.dtype): VV.dtype == WV.dtype and HV.dtype == VV.dtype):
if VV.dtype == 'float64': if VV.dtype == 'float64':
gemv = 'dgemv_' gemv = 'dgemv_'
elif VV.dtype == 'float32': elif VV.dtype == 'float32':
gemv = 'sgemv_' gemv = 'sgemv_'
else: else:
raise Exception('Unrecognized dtype for convolution '+V.value.dtype) raise Exception('Unrecognized dtype for convolution ' + V.value.dtype)
codeSource += """ codeSource += """
if (inputChannels > 20 && outputChannels > 20 && ws4 == sizeof(ELEM_AT(%(W)s,0))) if (inputChannels > 20 && outputChannels > 20 && ws4 == sizeof(ELEM_AT(%(W)s,0)))
...@@ -571,7 +577,7 @@ def computeH(V, W, b, d): ...@@ -571,7 +577,7 @@ def computeH(V, W, b, d):
outputChannels = W.shape[0] outputChannels = W.shape[0]
inputChannels = V.shape[4] inputChannels = V.shape[4]
if W.shape[4] != inputChannels: if W.shape[4] != inputChannels:
raise Exception("W.shape[4] = "+str(W.shape[4])+" but inputChannels = "+str(inputChannels)) raise Exception("W.shape[4] = " + str(W.shape[4]) + " but inputChannels = " + str(inputChannels))
filterHeight = W.shape[1] filterHeight = W.shape[1]
filterWidth = W.shape[2] filterWidth = W.shape[2]
filterDur = W.shape[3] filterDur = W.shape[3]
...@@ -586,12 +592,12 @@ def computeH(V, W, b, d): ...@@ -586,12 +592,12 @@ def computeH(V, W, b, d):
assert dy > 0 assert dy > 0
assert dt > 0 assert dt > 0
outputHeight = int( (vidHeight - filterHeight) / dx )+1 outputHeight = int((vidHeight - filterHeight) / dx) + 1
outputWidth = int( (vidWidth - filterWidth) / dy )+1 outputWidth = int((vidWidth - filterWidth) / dy) + 1
outputDur = int( (vidDur - filterDur) / dt ) + 1 outputDur = int((vidDur - filterDur) / dt) + 1
H = N.zeros( (batchSize, outputHeight, H = N.zeros((batchSize, outputHeight,
outputWidth, outputDur, outputChannels ), dtype=V.dtype ) outputWidth, outputDur, outputChannels), dtype=V.dtype)
# H[i,j,x,y,t] = b_j + sum_k sum_l sum_m sum_z W[j,z,k,l,m] V[i,z, dx*x+k,dy*y+l,dt*t+m] # H[i,j,x,y,t] = b_j + sum_k sum_l sum_m sum_z W[j,z,k,l,m] V[i,z, dx*x+k,dy*y+l,dt*t+m]
for i in xrange(0, H.shape[0]): for i in xrange(0, H.shape[0]):
...@@ -610,12 +616,8 @@ def computeH(V, W, b, d): ...@@ -610,12 +616,8 @@ def computeH(V, W, b, d):
# if (i,j,x,y,t) == (0,0,0,0,0): # if (i,j,x,y,t) == (0,0,0,0,0):
# print (( W[j,z,k,l,m] , V[i,z,d[0]*x+k,d[1]*y+l,d[2]*t+m] ), (k,l,m) ) # print (( W[j,z,k,l,m] , V[i,z,d[0]*x+k,d[1]*y+l,d[2]*t+m] ), (k,l,m) )
w = W[j, k, l, m, z] w = W[j, k, l, m, z]
v = V[i, d[0]*x+k, d[1]*y+l, d[2]*t+m, z] v = V[i, d[0] * x + k, d[1] * y + l, d[2] * t + m, z]
# if i == 0 and x == 0 and y == 0 and t == 0 and j == 0: # if i == 0 and x == 0 and y == 0 and t == 0 and j == 0:
# print 'setting H[0] += '+str(w*v)+' W['+str((j,z,k,l,m))+']='+str(w)+' V['+str((i,d[0]*x+k,d[1]*y+l,d[2]*t+m,z))+']='+str(v) # print 'setting H[0] += '+str(w*v)+' W['+str((j,z,k,l,m))+']='+str(w)+' V['+str((i,d[0]*x+k,d[1]*y+l,d[2]*t+m,z))+']='+str(v)
H[i, x, y, t, j] += w * v H[i, x, y, t, j] += w * v
return H return H
from . import ConvGrad3D
from . import ConvTransp3D
from six.moves import xrange
import numpy as N
import theano import theano
from theano.tensor import basic as T from theano.tensor import basic as T
from theano.misc import strutil from theano.misc import strutil
import numpy as N
from six.moves import xrange
from theano.gradient import grad_undefined from theano.gradient import grad_undefined
from theano.gradient import DisconnectedType from theano.gradient import DisconnectedType
...@@ -23,11 +25,15 @@ class ConvGrad3D(theano.Op): ...@@ -23,11 +25,15 @@ class ConvGrad3D(theano.Op):
WShape_ = T.as_tensor_variable(WShape) WShape_ = T.as_tensor_variable(WShape)
dCdH_ = T.as_tensor_variable(dCdH) dCdH_ = T.as_tensor_variable(dCdH)
return theano.Apply(self, inputs=[V_, d_, WShape_, dCdH_], outputs=[ T.TensorType(V_.dtype, (False, False, False, False, False))() ] ) return theano.Apply(self,
inputs=[V_, d_, WShape_, dCdH_],
outputs=[T.TensorType(
V_.dtype,
(False, False, False, False, False))()])
def infer_shape(self, node, input_shapes): def infer_shape(self, node, input_shapes):
V, d, W_shape, dCdH = node.inputs V, d, W_shape, dCdH = node.inputs
return [ ( W_shape[0], W_shape[1], W_shape[2], W_shape[3], W_shape[4] ) ] return [(W_shape[0], W_shape[1], W_shape[2], W_shape[3], W_shape[4])]
def connection_pattern(self, node): def connection_pattern(self, node):
...@@ -38,12 +44,12 @@ class ConvGrad3D(theano.Op): ...@@ -38,12 +44,12 @@ class ConvGrad3D(theano.Op):
dLdA, = output_gradients dLdA, = output_gradients
z = T.zeros_like(C[0, 0, 0, 0, :]) z = T.zeros_like(C[0, 0, 0, 0, :])
dLdC = convTransp3D(dLdA, z, d, B, C.shape[1:4]) dLdC = theano.tensor.nnet.convTransp3D(dLdA, z, d, B, C.shape[1:4])
# d actually does affect the outputs, so it's not disconnected # d actually does affect the outputs, so it's not disconnected
dLdd = grad_undefined(self, 1, d) dLdd = grad_undefined(self, 1, d)
# The shape of the weights doesn't affect the output elements # The shape of the weights doesn't affect the output elements
dLdWShape = DisconnectedType()() dLdWShape = DisconnectedType()()
dLdB = conv3D(C, dLdA, T.zeros_like(B[0, 0, 0, 0, :]), d) dLdB = theano.tensor.nnet.conv3D(C, dLdA, T.zeros_like(B[0, 0, 0, 0, :]), d)
return [dLdC, dLdd, dLdWShape, dLdB] return [dLdC, dLdd, dLdWShape, dLdB]
...@@ -54,15 +60,10 @@ class ConvGrad3D(theano.Op): ...@@ -54,15 +60,10 @@ class ConvGrad3D(theano.Op):
# partial C / partial W[j,z,k,l,m] = sum_i sum_p sum_q sum_r (partial C /partial H[i,j,p,q,r] ) * V[i,z,dr*p+k,dc*q+l,dt*r+m] # partial C / partial W[j,z,k,l,m] = sum_i sum_p sum_q sum_r (partial C /partial H[i,j,p,q,r] ) * V[i,z,dr*p+k,dc*q+l,dt*r+m]
batchSize = dCdH.shape[0] batchSize = dCdH.shape[0]
outputFilters = dCdH.shape[4]
outputHeight = dCdH.shape[1] outputHeight = dCdH.shape[1]
outputWidth = dCdH.shape[2] outputWidth = dCdH.shape[2]
outputDur = dCdH.shape[3] outputDur = dCdH.shape[3]
assert V.shape[0] == batchSize assert V.shape[0] == batchSize
inputFilters = V.shape[4]
inputHeight = V.shape[1]
inputWidth = V.shape[2]
inputDur = V.shape[3]
dr, dc, dt = d dr, dc, dt = d
dCdW = N.zeros(WShape, dtype=V.dtype) dCdW = N.zeros(WShape, dtype=V.dtype)
...@@ -78,7 +79,10 @@ class ConvGrad3D(theano.Op): ...@@ -78,7 +79,10 @@ class ConvGrad3D(theano.Op):
for r in xrange(0, outputDur): for r in xrange(0, outputDur):
for j in xrange(0, WShape[0]): for j in xrange(0, WShape[0]):
for z in xrange(0, WShape[4]): for z in xrange(0, WShape[4]):
dCdW[j, k, l, m, z] += dCdH[i, p, q, r, j] * V[i, dr*p+k, dc*q+l, dt*r+m, z] dCdW[j, k, l, m, z] += (
dCdH[i, p, q, r, j] *
V[i, dr * p + k, dc * q + l,
dt * r + m, z])
output_storage[0][0] = dCdW output_storage[0][0] = dCdW
...@@ -272,6 +276,3 @@ class ConvGrad3D(theano.Op): ...@@ -272,6 +276,3 @@ class ConvGrad3D(theano.Op):
convGrad3D = ConvGrad3D() convGrad3D = ConvGrad3D()
from theano.tensor.nnet.Conv3D import conv3D
from theano.tensor.nnet.ConvTransp3D import convTransp3D
from __future__ import print_function from __future__ import print_function
import numpy as N import numpy as N
from six.moves import xrange from six.moves import xrange
import theano
from theano.tensor import basic as T from theano.tensor import basic as T
from theano.misc import strutil from theano.misc import strutil
import theano
from theano.gradient import grad_undefined from theano.gradient import grad_undefined
from theano.gradient import DisconnectedType from theano.gradient import DisconnectedType
...@@ -31,12 +33,15 @@ class ConvTransp3D(theano.Op): ...@@ -31,12 +33,15 @@ class ConvTransp3D(theano.Op):
else: else:
RShape_ = T.as_tensor_variable([-1, -1, -1]) RShape_ = T.as_tensor_variable([-1, -1, -1])
return theano.Apply(self, inputs=[W_, b_, d_, H_, RShape_], outputs=[ T.TensorType(H_.dtype, (False, False, False, False, False))() ] ) return theano.Apply(self,
inputs=[W_, b_, d_, H_, RShape_],
outputs=[T.TensorType(H_.dtype,
(False, False, False, False, False))()])
def infer_shape(self, node, input_shapes): def infer_shape(self, node, input_shapes):
W, b, d, H, RShape = node.inputs W, b, d, H, RShape = node.inputs
W_shape, b_shape, d_shape, H_shape, RShape_shape = input_shapes W_shape, b_shape, d_shape, H_shape, RShape_shape = input_shapes
return [(H_shape[0], RShape[0], RShape[1], RShape[2], W_shape[4])] return [(H_shape[0], RShape[0], RShape[1], RShape[2], W_shape[4])]
def connection_pattern(self, node): def connection_pattern(self, node):
return [[True], [True], [True], [True], [False]] return [[True], [True], [True], [True], [False]]
...@@ -44,9 +49,9 @@ class ConvTransp3D(theano.Op): ...@@ -44,9 +49,9 @@ class ConvTransp3D(theano.Op):
def grad(self, inputs, output_gradients): def grad(self, inputs, output_gradients):
W, b, d, H, RShape = inputs W, b, d, H, RShape = inputs
dCdR, = output_gradients dCdR, = output_gradients
dCdH = conv3D(dCdR, W, T.zeros_like(H[0, 0, 0, 0, :]), d) dCdH = theano.tensor.nnet.conv3D(dCdR, W, T.zeros_like(H[0, 0, 0, 0, :]), d)
WShape = W.shape WShape = W.shape
dCdW = convGrad3D(dCdR, d, WShape, H) dCdW = theano.tensor.nnet.convGrad3D(dCdR, d, WShape, H)
dCdb = T.sum(dCdR, axis=(0, 1, 2, 3)) dCdb = T.sum(dCdR, axis=(0, 1, 2, 3))
# not differentiable, since d affects the output elements # not differentiable, since d affects the output elements
dCdd = grad_undefined(self, 2, d) dCdd = grad_undefined(self, 2, d)
...@@ -73,11 +78,13 @@ class ConvTransp3D(theano.Op): ...@@ -73,11 +78,13 @@ class ConvTransp3D(theano.Op):
else: else:
b_name = 'anon_b' b_name = 'anon_b'
dCdW.name = 'ConvTransp3D_dCdW.H='+H_name+',dCdR='+dCdR_name+',W='+W_name dCdW.name = ('ConvTransp3D_dCdW.H=' + H_name + ',dCdR=' + dCdR_name +
dCdb.name = 'ConvTransp3D_dCdb.H='+H_name+',dCdR='+dCdR_name+',W='+W_name+',b='+b_name ',W=' + W_name)
dCdb.name = ('ConvTransp3D_dCdb.H=' + H_name + ',dCdR=' + dCdR_name +
',W=' + W_name + ',b=' + b_name)
dCdH.name = 'ConvTransp3D_dCdH.H=' + H_name + ',dCdR=' + dCdR_name dCdH.name = 'ConvTransp3D_dCdH.H=' + H_name + ',dCdR=' + dCdR_name
return [dCdW, dCdb, dCdd, dCdH, dCdRShape] return [dCdW, dCdb, dCdd, dCdH, dCdRShape]
def perform(self, node, inputs, output_storage): def perform(self, node, inputs, output_storage):
W, b, d, H, RShape = inputs W, b, d, H, RShape = inputs
...@@ -335,7 +342,7 @@ def computeR(W, b, d, H, Rshape=None): ...@@ -335,7 +342,7 @@ def computeR(W, b, d, H, Rshape=None):
assert len(b.shape) == 1 assert len(b.shape) == 1
assert len(d) == 3 assert len(d) == 3
outputChannels, filterHeight, filterWidth, filterDur, \ outputChannels, filterHeight, filterWidth, filterDur, \
inputChannels = W.shape inputChannels = W.shape
batchSize, outputHeight, outputWidth, outputDur, \ batchSize, outputHeight, outputWidth, outputDur, \
outputChannelsAgain = H.shape outputChannelsAgain = H.shape
...@@ -367,7 +374,7 @@ def computeR(W, b, d, H, Rshape=None): ...@@ -367,7 +374,7 @@ def computeR(W, b, d, H, Rshape=None):
# print "video size: "+str((videoHeight, videoWidth, videoDur)) # print "video size: "+str((videoHeight, videoWidth, videoDur))
R = N.zeros((batchSize, videoHeight, R = N.zeros((batchSize, videoHeight,
videoWidth, videoDur, inputChannels), dtype=H.dtype) videoWidth, videoDur, inputChannels), dtype=H.dtype)
# R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc] # R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc]
for i in xrange(0, batchSize): for i in xrange(0, batchSize):
...@@ -404,8 +411,8 @@ def computeR(W, b, d, H, Rshape=None): ...@@ -404,8 +411,8 @@ def computeR(W, b, d, H, Rshape=None):
if tk < 0: if tk < 0:
break break
R[ R[i, r, c, t, j] += N.dot(
i, r, c, t, j] += N.dot(W[:, rk, ck, tk, j], H[i, rc, cc, tc, :] ) W[:, rk, ck, tk, j], H[i, rc, cc, tc, :])
tc += 1 tc += 1
"" # close loop over tc "" # close loop over tc
...@@ -421,7 +428,3 @@ def computeR(W, b, d, H, Rshape=None): ...@@ -421,7 +428,3 @@ def computeR(W, b, d, H, Rshape=None):
"" # close loop over i "" # close loop over i
return R return R
from theano.tensor.nnet.Conv3D import conv3D
from theano.tensor.nnet.ConvGrad3D import convGrad3D
差异被折叠。
...@@ -194,13 +194,13 @@ def conv3d(signals, filters, ...@@ -194,13 +194,13 @@ def conv3d(signals, filters,
_signals_shape_5d[2], _signals_shape_5d[2],
_signals_shape_5d[3], _signals_shape_5d[3],
_signals_shape_5d[4], _signals_shape_5d[4],
) )
_filters_shape_4d = ( _filters_shape_4d = (
_filters_shape_5d[0] * _filters_shape_5d[1], _filters_shape_5d[0] * _filters_shape_5d[1],
_filters_shape_5d[2], _filters_shape_5d[2],
_filters_shape_5d[3], _filters_shape_5d[3],
_filters_shape_5d[4], _filters_shape_5d[4],
) )
if border_mode[1] != border_mode[2]: if border_mode[1] != border_mode[2]:
raise NotImplementedError('height and width bordermodes must match') raise NotImplementedError('height and width bordermodes must match')
...@@ -228,7 +228,7 @@ def conv3d(signals, filters, ...@@ -228,7 +228,7 @@ def conv3d(signals, filters,
_filters_shape_5d[1], # Tf _filters_shape_5d[1], # Tf
_signals_shape_5d[3] - _filters_shape_5d[3] + 1, _signals_shape_5d[3] - _filters_shape_5d[3] + 1,
_signals_shape_5d[4] - _filters_shape_5d[4] + 1, _signals_shape_5d[4] - _filters_shape_5d[4] + 1,
)) ))
elif border_mode[1] == 'full': elif border_mode[1] == 'full':
out_tmp = out_4d.reshape(( out_tmp = out_4d.reshape((
_signals_shape_5d[0], # Ns _signals_shape_5d[0], # Ns
...@@ -237,7 +237,7 @@ def conv3d(signals, filters, ...@@ -237,7 +237,7 @@ def conv3d(signals, filters,
_filters_shape_5d[1], # Tf _filters_shape_5d[1], # Tf
_signals_shape_5d[3] + _filters_shape_5d[3] - 1, _signals_shape_5d[3] + _filters_shape_5d[3] - 1,
_signals_shape_5d[4] + _filters_shape_5d[4] - 1, _signals_shape_5d[4] + _filters_shape_5d[4] - 1,
)) ))
elif border_mode[1] == 'same': elif border_mode[1] == 'same':
raise NotImplementedError() raise NotImplementedError()
else: else:
...@@ -246,15 +246,15 @@ def conv3d(signals, filters, ...@@ -246,15 +246,15 @@ def conv3d(signals, filters,
# now sum out along the Tf to get the output # now sum out along the Tf to get the output
# but we have to sum on a diagonal through the Tf and Ts submatrix. # but we have to sum on a diagonal through the Tf and Ts submatrix.
if border_mode[0] == 'valid': if border_mode[0] == 'valid':
if _filters_shape_5d[1]!=1: if _filters_shape_5d[1] != 1:
out_5d = diagonal_subtensor(out_tmp, 1, 3).sum(axis=3) out_5d = diagonal_subtensor(out_tmp, 1, 3).sum(axis=3)
else: # for Tf==1, no sum along Tf, the Ts-axis of the output is unchanged! else: # for Tf==1, no sum along Tf, the Ts-axis of the output is unchanged!
out_5d = out_tmp.reshape(( out_5d = out_tmp.reshape((
_signals_shape_5d[0], _signals_shape_5d[0],
_signals_shape_5d[1], _signals_shape_5d[1],
_filters_shape_5d[0], _filters_shape_5d[0],
_signals_shape_5d[3] - _filters_shape_5d[3] + 1, _signals_shape_5d[3] - _filters_shape_5d[3] + 1,
_signals_shape_5d[4] - _filters_shape_5d[4] + 1, _signals_shape_5d[4] - _filters_shape_5d[4] + 1,
)) ))
elif border_mode[0] in ('full', 'same'): elif border_mode[0] in ('full', 'same'):
raise NotImplementedError('sequence border mode', border_mode[0]) raise NotImplementedError('sequence border mode', border_mode[0])
...@@ -316,7 +316,7 @@ if cuda.cuda_available: ...@@ -316,7 +316,7 @@ if cuda.cuda_available:
def local_inplace_DiagonalSubtensor(node): def local_inplace_DiagonalSubtensor(node):
""" also work for IncDiagonalSubtensor """ """ also work for IncDiagonalSubtensor """
if (isinstance(node.op, (DiagonalSubtensor, IncDiagonalSubtensor)) and if (isinstance(node.op, (DiagonalSubtensor, IncDiagonalSubtensor)) and
not node.op.inplace): not node.op.inplace):
new_op = node.op.__class__(inplace=True) new_op = node.op.__class__(inplace=True)
new_node = new_op(*node.inputs) new_node = new_op(*node.inputs)
return [new_node] return [new_node]
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
TODO: implement Images2Neibs.infer_shape() methods TODO: implement Images2Neibs.infer_shape() methods
""" """
from six.moves import xrange
import numpy
import theano import theano
from theano import Op, Apply from theano import Op, Apply
import theano.tensor as T import theano.tensor as T
from theano.gradient import grad_not_implemented from theano.gradient import grad_not_implemented
from theano.gradient import grad_undefined from theano.gradient import grad_undefined
import numpy
class Images2Neibs(Op): class Images2Neibs(Op):
...@@ -206,7 +206,7 @@ class Images2Neibs(Op): ...@@ -206,7 +206,7 @@ class Images2Neibs(Op):
z_col = j + d * i z_col = j + d * i
z[0][z_row, z_col] = ten4[n, s, ten4_2, ten4_3] z[0][z_row, z_col] = ten4[n, s, ten4_2, ten4_3]
def infer_shape(self, node, input_shape): def infer_shape(self, node, input_shape):
in_shape = input_shape[0] in_shape = input_shape[0]
c, d = node.inputs[1] c, d = node.inputs[1]
...@@ -223,7 +223,7 @@ class Images2Neibs(Op): ...@@ -223,7 +223,7 @@ class Images2Neibs(Op):
z_dim0 = grid_c * grid_d * in_shape[1] * in_shape[0] z_dim0 = grid_c * grid_d * in_shape[1] * in_shape[0]
z_dim1 = c * d z_dim1 = c * d
return [(z_dim0, z_dim1)] return [(z_dim0, z_dim1)]
def c_code(self, node, name, inp, out, sub): def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp ten4, neib_shape, neib_step = inp
z, = out z, = out
...@@ -417,21 +417,21 @@ class Images2Neibs(Op): ...@@ -417,21 +417,21 @@ class Images2Neibs(Op):
def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'): def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'):
""" """
Function :func:`images2neibs <theano.sandbox.neighbours.images2neibs>` Function :func:`images2neibs <theano.sandbox.neighbours.images2neibs>`
allows to apply a sliding window operation to a tensor containing allows to apply a sliding window operation to a tensor containing
images images
or other two-dimensional objects. or other two-dimensional objects.
The sliding window operation loops The sliding window operation loops
over points in input data and stores a rectangular neighbourhood of over points in input data and stores a rectangular neighbourhood of
each point. each point.
It is possible to assign a step of selecting patches (parameter It is possible to assign a step of selecting patches (parameter
`neib_step`). `neib_step`).
:param ten4: A 4-dimensional tensor which represents :param ten4: A 4-dimensional tensor which represents
a list of lists of images.a list of lists of images. a list of lists of images.a list of lists of images.
It should have shape (list 1 dim, list 2 dim, It should have shape (list 1 dim, list 2 dim,
row, col). The first two dimensions can be row, col). The first two dimensions can be
useful to store different channels and batches. useful to store different channels and batches.
:type ten4: A 4d tensor-like. :type ten4: A 4d tensor-like.
:param neib_shape: A tuple containing two :param neib_shape: A tuple containing two
...@@ -442,20 +442,20 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'): ...@@ -442,20 +442,20 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'):
:type neib_shape: A 1d tensor-like of 2 values. :type neib_shape: A 1d tensor-like of 2 values.
:param neib_step: (dr,dc) where dr is the number of rows to :param neib_step: (dr,dc) where dr is the number of rows to
skip between patch and dc is the number of skip between patch and dc is the number of
columns. The parameter should be a tuple of two elements: columns. The parameter should be a tuple of two elements:
number number
of rows and number of columns to skip each iteration. of rows and number of columns to skip each iteration.
Basically, when the step is 1, the neighbourhood of every Basically, when the step is 1, the neighbourhood of every
first element is taken and every possible rectangular first element is taken and every possible rectangular
subset is returned. By default it is equal to subset is returned. By default it is equal to
`neib_shape` in other words, the `neib_shape` in other words, the
patches are disjoint. When the step is greater than patches are disjoint. When the step is greater than
`neib_shape`, some elements are omitted. When None, this `neib_shape`, some elements are omitted. When None, this
is the same as is the same as
neib_shape(patch are disjoint) neib_shape(patch are disjoint)
.. note:: Currently the step size should be chosen in the way that the .. note:: Currently the step size should be chosen in the way that the
corresponding dimension :math:`i` (width or height) is equal to corresponding dimension :math:`i` (width or height) is equal to
:math:`n * step\_size_i + neib\_shape_i` for some :math:`n` :math:`n * step\_size_i + neib\_shape_i` for some :math:`n`
:type neib_step: A 1d tensor-like of 2 values. :type neib_step: A 1d tensor-like of 2 values.
:param mode: :param mode:
...@@ -489,29 +489,29 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'): ...@@ -489,29 +489,29 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'):
= flattened version of ten4[i,j,l:l+r,k:k+c] = flattened version of ten4[i,j,l:l+r,k:k+c]
idx += 1 idx += 1
.. note:: The operation isn't necessarily implemented internally with .. note:: The operation isn't necessarily implemented internally with
these for loops, they're just the easiest way to describe the these for loops, they're just the easiest way to describe the
output pattern. output pattern.
Example: Example:
.. code-block:: python .. code-block:: python
# Defining variables # Defining variables
images = T.tensor4('images') images = T.tensor4('images')
neibs = images2neibs(images, neib_shape=(5, 5)) neibs = images2neibs(images, neib_shape=(5, 5))
# Constructing theano function # Constructing theano function
window_function = theano.function([images], neibs) window_function = theano.function([images], neibs)
# Input tensor (one image 10x10) # Input tensor (one image 10x10)
im_val = np.arange(100.).reshape((1, 1, 10, 10)) im_val = np.arange(100.).reshape((1, 1, 10, 10))
# Function application # Function application
neibs_val = window_function(im_val) neibs_val = window_function(im_val)
.. note:: The underlying code will construct a 2D tensor of disjoint .. note:: The underlying code will construct a 2D tensor of disjoint
patches 5x5. The output has shape 4x25. patches 5x5. The output has shape 4x25.
""" """
return Images2Neibs(mode)(ten4, neib_shape, neib_step) return Images2Neibs(mode)(ten4, neib_shape, neib_step)
...@@ -524,25 +524,24 @@ def neibs2images(neibs, neib_shape, original_shape, mode='valid'): ...@@ -524,25 +524,24 @@ def neibs2images(neibs, neib_shape, original_shape, mode='valid'):
the output of :func:`images2neibs <theano.sandbox.neigbours.neibs2images>` the output of :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`
and reconstructs its input. and reconstructs its input.
:param neibs: matrix like the one obtained by :param neibs: matrix like the one obtained by
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>` :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`
:param neib_shape: `neib_shape` that was used in :param neib_shape: `neib_shape` that was used in
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>` :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`
:param original_shape: original shape of the 4d tensor given to :param original_shape: original shape of the 4d tensor given to
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>` :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`
:return: Reconstructs the input of :return: Reconstructs the input of
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>`, :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`,
a 4d tensor of shape `original_shape`. a 4d tensor of shape `original_shape`.
.. note:: Currently, the function doesn't support tensors created with .. note:: Currently, the function doesn't support tensors created with
`neib_step` different from default value. This means that it may be `neib_step` different from default value. This means that it may be
impossible to compute the gradient of a variable gained by impossible to compute the gradient of a variable gained by
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>` w.r.t. :func:`images2neibs <theano.sandbox.neigbours.neibs2images>` w.r.t.
its inputs in this case, because it uses its inputs in this case, because it uses
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>` for :func:`images2neibs <theano.sandbox.neigbours.neibs2images>` for
gradient computation. gradient computation.
Example, which uses a tensor gained in example for Example, which uses a tensor gained in example for
:func:`images2neibs <theano.sandbox.neigbours.neibs2images>`: :func:`images2neibs <theano.sandbox.neigbours.neibs2images>`:
......
差异被折叠。
...@@ -7,7 +7,6 @@ from __future__ import print_function ...@@ -7,7 +7,6 @@ from __future__ import print_function
import warnings import warnings
import numpy import numpy
from six.moves import xrange
import theano import theano
from theano import config, gof, printing, scalar from theano import config, gof, printing, scalar
...@@ -92,7 +91,7 @@ class ScalarSigmoid(scalar.UnaryScalarOp): ...@@ -92,7 +91,7 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
x, = inp x, = inp
z, = out z, = out
if (not theano.config.lib.amdlibm or if (not theano.config.lib.amdlibm or
node.inputs[0].dtype != node.outputs[0].dtype): node.inputs[0].dtype != node.outputs[0].dtype):
raise theano.gof.utils.MethodNotDefined() raise theano.gof.utils.MethodNotDefined()
dtype = node.inputs[0].dtype dtype = node.inputs[0].dtype
if dtype == 'float32' and self.amd_float32 is not None: if dtype == 'float32' and self.amd_float32 is not None:
...@@ -129,9 +128,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp): ...@@ -129,9 +128,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
""" """
This method was used to generate the graph: sigmoid_prec.png in the doc This method was used to generate the graph: sigmoid_prec.png in the doc
""" """
import matplotlib
data = numpy.arange(-15, 15, .1) data = numpy.arange(-15, 15, .1)
val = 1/(1+numpy.exp(-data)) val = 1 / (1 + numpy.exp(-data))
def hard_sigmoid(x): def hard_sigmoid(x):
return theano.tensor.nnet.hard_sigmoid(x) return theano.tensor.nnet.hard_sigmoid(x)
...@@ -164,10 +162,10 @@ scalar_sigmoid = ScalarSigmoid(scalar.upgrade_to_float, name='scalar_sigmoid') ...@@ -164,10 +162,10 @@ scalar_sigmoid = ScalarSigmoid(scalar.upgrade_to_float, name='scalar_sigmoid')
sigmoid = elemwise.Elemwise(scalar_sigmoid, name='sigmoid') sigmoid = elemwise.Elemwise(scalar_sigmoid, name='sigmoid')
sigmoid_inplace = elemwise.Elemwise( sigmoid_inplace = elemwise.Elemwise(
ScalarSigmoid(scalar.transfer_type(0)), ScalarSigmoid(scalar.transfer_type(0)),
inplace_pattern={0: 0}, inplace_pattern={0: 0},
name='sigmoid_inplace', name='sigmoid_inplace',
) )
pprint.assign(sigmoid, printing.FunctionPrinter('sigmoid')) pprint.assign(sigmoid, printing.FunctionPrinter('sigmoid'))
...@@ -240,7 +238,7 @@ pprint.assign(ultra_fast_sigmoid, ...@@ -240,7 +238,7 @@ pprint.assign(ultra_fast_sigmoid,
printing.FunctionPrinter('ultra_fast_sigmoid')) printing.FunctionPrinter('ultra_fast_sigmoid'))
#@opt.register_uncanonicalize # @opt.register_uncanonicalize
@gof.local_optimizer([sigmoid]) @gof.local_optimizer([sigmoid])
def local_ultra_fast_sigmoid(node): def local_ultra_fast_sigmoid(node):
""" """
...@@ -290,7 +288,7 @@ def hard_sigmoid(x): ...@@ -290,7 +288,7 @@ def hard_sigmoid(x):
return x return x
#@opt.register_uncanonicalize # @opt.register_uncanonicalize
@gof.local_optimizer([sigmoid]) @gof.local_optimizer([sigmoid])
def local_hard_sigmoid(node): def local_hard_sigmoid(node):
if (isinstance(node.op, tensor.Elemwise) and if (isinstance(node.op, tensor.Elemwise) and
...@@ -412,7 +410,7 @@ def is_1pexp(t): ...@@ -412,7 +410,7 @@ def is_1pexp(t):
""" """
if t.owner and t.owner.op == tensor.add: if t.owner and t.owner.op == tensor.add:
scalars, scalar_inputs, nonconsts = \ scalars, scalar_inputs, nonconsts = \
opt.scalarconsts_rest(t.owner.inputs) opt.scalarconsts_rest(t.owner.inputs)
# scalar_inputs are potentially dimshuffled and fill'd scalars # scalar_inputs are potentially dimshuffled and fill'd scalars
if len(nonconsts) == 1: if len(nonconsts) == 1:
maybe_exp = nonconsts[0] maybe_exp = nonconsts[0]
...@@ -439,11 +437,12 @@ def is_1pexp(t): ...@@ -439,11 +437,12 @@ def is_1pexp(t):
return None return None
AddConfigVar('warn.identify_1pexp_bug', AddConfigVar(
'Warn if Theano versions prior to 7987b51 (2011-12-18) could have ' 'warn.identify_1pexp_bug',
'yielded a wrong result due to a bug in the is_1pexp function', 'Warn if Theano versions prior to 7987b51 (2011-12-18) could have '
BoolParam(theano.configdefaults.warn_default('0.4.1')), 'yielded a wrong result due to a bug in the is_1pexp function',
in_c_key=False) BoolParam(theano.configdefaults.warn_default('0.4.1')),
in_c_key=False)
def is_exp(var): def is_exp(var):
...@@ -778,9 +777,9 @@ def perform_sigm_times_exp(tree, exp_x=None, exp_minus_x=None, sigm_x=None, ...@@ -778,9 +777,9 @@ def perform_sigm_times_exp(tree, exp_x=None, exp_minus_x=None, sigm_x=None,
rval = False rval = False
for sub_idx, sub_tree in enumerate(inputs): for sub_idx, sub_tree in enumerate(inputs):
rval |= perform_sigm_times_exp( rval |= perform_sigm_times_exp(
tree=sub_tree, parent=tree, child_idx=sub_idx, tree=sub_tree, parent=tree, child_idx=sub_idx,
exp_x=exp_x, exp_minus_x=exp_minus_x, sigm_x=sigm_x, exp_x=exp_x, exp_minus_x=exp_minus_x, sigm_x=sigm_x,
sigm_minus_x=sigm_minus_x, full_tree=full_tree) sigm_minus_x=sigm_minus_x, full_tree=full_tree)
return rval return rval
else: else:
# Reached a leaf: if it is an exponential or a sigmoid, then we # Reached a leaf: if it is an exponential or a sigmoid, then we
...@@ -867,15 +866,15 @@ def local_inv_1_plus_exp(node): ...@@ -867,15 +866,15 @@ def local_inv_1_plus_exp(node):
inv_arg = node.inputs[0] inv_arg = node.inputs[0]
if inv_arg.owner and inv_arg.owner.op == tensor.add: if inv_arg.owner and inv_arg.owner.op == tensor.add:
scalars, scalar_inputs, nonconsts = \ scalars, scalar_inputs, nonconsts = \
opt.scalarconsts_rest(inv_arg.owner.inputs) opt.scalarconsts_rest(inv_arg.owner.inputs)
# scalar_inputs are potentially dimshuffled and fill'd scalars # scalar_inputs are potentially dimshuffled and fill'd scalars
if len(nonconsts) == 1: if len(nonconsts) == 1:
if nonconsts[0].owner and nonconsts[0].owner.op == tensor.exp: if nonconsts[0].owner and nonconsts[0].owner.op == tensor.exp:
if scalars and numpy.allclose(numpy.sum(scalars), 1): if scalars and numpy.allclose(numpy.sum(scalars), 1):
return opt._fill_chain( return opt._fill_chain(
sigmoid( sigmoid(
tensor.neg(nonconsts[0].owner.inputs[0])), tensor.neg(nonconsts[0].owner.inputs[0])),
scalar_inputs) scalar_inputs)
# Registration is below, and conditional. # Registration is below, and conditional.
...@@ -892,7 +891,7 @@ def local_1msigmoid(node): ...@@ -892,7 +891,7 @@ def local_1msigmoid(node):
if sub_r.owner and sub_r.owner.op == sigmoid: if sub_r.owner and sub_r.owner.op == sigmoid:
try: try:
val_l = opt.get_scalar_constant_value(sub_l) val_l = opt.get_scalar_constant_value(sub_l)
except Exception as e: except Exception:
return return
if numpy.allclose(numpy.sum(val_l), 1): if numpy.allclose(numpy.sum(val_l), 1):
return [sigmoid(-sub_r.owner.inputs[0])] return [sigmoid(-sub_r.owner.inputs[0])]
...@@ -921,7 +920,6 @@ if 0: ...@@ -921,7 +920,6 @@ if 0:
print(sigm_canonicalize(node)) print(sigm_canonicalize(node))
def sigm_canonicalize(node): def sigm_canonicalize(node):
add = tensor.add
mul = tensor.mul mul = tensor.mul
div = tensor.true_div div = tensor.true_div
......
...@@ -88,15 +88,7 @@ whitelist_flake8 = [ ...@@ -88,15 +88,7 @@ whitelist_flake8 = [
"tensor/signal/conv.py", "tensor/signal/conv.py",
"tensor/signal/tests/test_conv.py", "tensor/signal/tests/test_conv.py",
"tensor/signal/tests/test_downsample.py", "tensor/signal/tests/test_downsample.py",
"tensor/nnet/nnet.py",
"tensor/nnet/Conv3D.py",
"tensor/nnet/__init__.py", "tensor/nnet/__init__.py",
"tensor/nnet/ConvTransp3D.py",
"tensor/nnet/sigm.py",
"tensor/nnet/ConvGrad3D.py",
"tensor/nnet/conv3d2d.py",
"tensor/nnet/conv.py",
"tensor/nnet/neighbours.py",
"tensor/nnet/tests/test_conv.py", "tensor/nnet/tests/test_conv.py",
"tensor/nnet/tests/test_neighbours.py", "tensor/nnet/tests/test_neighbours.py",
"tensor/nnet/tests/test_nnet.py", "tensor/nnet/tests/test_nnet.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论