本文整理汇总了Python中theano.tensor.nnet.conv2d函数的典型用法代码示例。如果您正苦于以下问题:Python conv2d函数的具体用法?Python conv2d怎么用?Python conv2d使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了conv2d函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _backward_negative_z
def _backward_negative_z(inputs, weights, normed_relevances, bias=None):
inputs_plus = inputs * T.gt(inputs, 0)
weights_plus = weights * T.gt(weights, 0)
inputs_minus = inputs * T.lt(inputs, 0)
weights_minus = weights * T.lt(weights, 0)
# Compute weights+ * inputs- and weights- * inputs+
negative_part_a = conv2d(
normed_relevances, weights_plus.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full"
)
negative_part_a *= inputs_minus
negative_part_b = conv2d(
normed_relevances, weights_minus.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full"
)
negative_part_b *= inputs_plus
together = negative_part_a + negative_part_b
if bias is not None:
bias_negative = bias * T.lt(bias, 0)
bias_relevance = bias_negative.dimshuffle("x", 0, "x", "x") * normed_relevances
# Divide bias by weight size before convolving back
# mean across channel, 0, 1 dims (hope this is correct?)
fraction_bias = bias_relevance / T.prod(weights.shape[1:]).astype(theano.config.floatX)
bias_rel_in = conv2d(
fraction_bias, T.ones_like(weights).dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full"
)
together += bias_rel_in
return together
示例2: relevance_conv_z
def relevance_conv_z(out_relevances, inputs, weights, bias=None):
norms_for_relevances = conv2d(inputs, weights)
if bias is not None:
norms_for_relevances += bias.dimshuffle("x", 0, "x", "x")
# stabilize
# prevent division by 0 and division by small numbers
eps = 1e-4
norms_for_relevances += T.sgn(norms_for_relevances) * eps
norms_for_relevances += T.eq(norms_for_relevances, 0) * eps
normed_relevances = out_relevances / norms_for_relevances
# upconv
in_relevances = conv2d(normed_relevances, weights.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full")
in_relevances_proper = in_relevances * inputs
if bias is not None:
bias_relevance = bias.dimshuffle("x", 0, "x", "x") * normed_relevances
# Divide bias by weight size before convolving back
# mean across channel, 0, 1 dims (hope this is correct?)
fraction_bias = bias_relevance / T.prod(weights.shape[1:]).astype(theano.config.floatX)
bias_rel_in = conv2d(
fraction_bias, T.ones_like(weights).dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full"
)
in_relevances_proper += bias_rel_in
return in_relevances_proper
示例3: convolutional_model
def convolutional_model(X, w_1, w_2, w_3, w_4, w_5, w_6, p_1, p_2, p_3, p_4, p_5):
l1 = dropout(T.tanh( max_pool_2d(T.maximum(conv2d(X, w_1, border_mode='full'),0.), (2, 2),ignore_border=True) + b_1.dimshuffle('x', 0, 'x', 'x') ), p_1)
l2 = dropout(T.tanh( max_pool_2d(T.maximum(conv2d(l1, w_2), 0.), (2, 2),ignore_border=True) + b_2.dimshuffle('x', 0, 'x', 'x') ), p_2)
l3 = dropout(T.flatten(T.tanh( max_pool_2d(T.maximum(conv2d(l2, w_3), 0.), (2, 2),ignore_border=True) + b_3.dimshuffle('x', 0, 'x', 'x') ), outdim=2), p_3)# flatten to switch back to 1d layers
l4 = dropout(T.maximum(T.dot(l3, w_4), 0.), p_4)
l5 = dropout(T.maximum(T.dot(l4, w_5), 0.), p_5)
return T.dot(l5, w_6)
示例4: lecun_lcn
def lecun_lcn(self, X, kernel_size=7, threshold = 1e-4, use_divisor=False):
"""
Yann LeCun's local contrast normalization
Orginal code in Theano by: Guillaume Desjardins
"""
filter_shape = (1, 1, kernel_size, kernel_size)
filters = gaussian_filter(kernel_size).reshape(filter_shape)
filters = shared(_asarray(filters, dtype=floatX), borrow=True)
convout = conv2d(X, filters=filters, filter_shape=filter_shape,
border_mode='full')
# For each pixel, remove mean of kernel_sizexkernel_size neighborhood
mid = int(floor(kernel_size/2.))
new_X = X - convout[:,:,mid:-mid,mid:-mid]
if use_divisor:
# Scale down norm of kernel_sizexkernel_size patch
sum_sqr_XX = conv2d(T.sqr(T.abs_(X)), filters=filters,
filter_shape=filter_shape, border_mode='full')
denom = T.sqrt(sum_sqr_XX[:,:,mid:-mid,mid:-mid])
per_img_mean = denom.mean(axis=[2,3])
divisor = T.largest(per_img_mean.dimshuffle(0,1,'x','x'), denom)
divisor = T.maximum(divisor, threshold)
new_X /= divisor
return new_X#T.cast(new_X, floatX)
示例5: relevance_conv_z_plus
def relevance_conv_z_plus(out_relevances, inputs, weights, bias=None):
if bias is not None:
log.warning("Bias not respected for conv z_plus")
# hack for negative inputs
# inputs = T.abs_(inputs)
weights_plus = weights * T.gt(weights, 0)
norms_for_relevances = conv2d(inputs, weights_plus)
# prevent division by 0...
# adds 1 to every entry that is 0 -> sets 0s to 1
relevances_are_0 = T.eq(norms_for_relevances, 0)
norms_for_relevances += relevances_are_0 * 1
normed_relevances = out_relevances / norms_for_relevances
# upconv
in_relevances = conv2d(normed_relevances, weights_plus.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full")
in_relevances_proper = in_relevances * inputs
# Correct for those parts where all inputs of a relevance were
# zero, spread relevance equally them
pool_ones = T.ones(weights_plus.shape, dtype=np.float32)
# mean across channel, 0, 1 dims (hope this is correct?)
pool_fractions = pool_ones / T.prod(weights_plus.shape[1:]).astype(theano.config.floatX)
in_relevances_from_0 = conv2d(
out_relevances * relevances_are_0, pool_fractions.dimshuffle(1, 0, 2, 3), subsample=(1, 1), border_mode="full"
)
in_relevances_proper += in_relevances_from_0
return in_relevances_proper
示例6: theano_kernel_derivative
def theano_kernel_derivative(imshp,kshp,featshp,stride=1):
features = T.tensor4(dtype=theano.config.floatX)
kernel = T.tensor4(dtype=theano.config.floatX)
image = T.tensor4(dtype=theano.config.floatX)
# Need to transpose first two dimensions of kernel, and reverse index kernel image dims (for correlation)
kernel_rotated = T.transpose(kernel[:,:,::-1,::-1],axes=[1,0,2,3])
featshp_logical = (featshp[0],featshp[1],featshp[2]*stride,featshp[3]*stride)
kshp_rotated = (kshp[1], kshp[0], kshp[2], kshp[3])
image_estimate = conv2d(features,kernel_rotated,border_mode='full',
image_shape=featshp,filter_shape=kshp_rotated,
imshp_logical=featshp_logical[1:],kshp_logical=kshp[2:])
image_error = image - image_estimate
image_error_rot = T.transpose(image_error,[1,0,2,3])[:,:,::-1,::-1]
imshp_rot = (imshp[1],imshp[0],imshp[2],imshp[3])
featshp_rot = (featshp[1],featshp[0],featshp[2],featshp[3])
features_rot = T.transpose(features,[1,0,2,3])
featshp_rot_logical = (featshp_rot[0],featshp_rot[1],featshp_rot[2]*stride,featshp_rot[3]*stride)
kernel_grad_rot = -conv2d(image_error_rot,features_rot,
image_shape=imshp_rot,filter_shape=featshp_rot,
imshp_logical=imshp_rot[1:],kshp_logical=featshp_rot_logical[2:])
kernel_grad = T.transpose(kernel_grad_rot,[1,0,2,3])
return function(inputs=[image,features,kernel],outputs=kernel_grad)
示例7: model
def model(X, params, featMaps, pieces, pDropConv, pDropHidden):
lnum = 0 # conv: (32, 32) pool: (16, 16)
layer = conv2d(X, params[lnum][0], border_mode='half') + \
params[lnum][1].dimshuffle('x', 0, 'x', 'x')
layer = maxout(layer, featMaps[lnum], pieces[lnum])
layer = pool_2d(layer, (2, 2), st=(2, 2), ignore_border=False, mode='max')
layer = basicUtils.dropout(layer, pDropConv)
lnum += 1 # conv: (16, 16) pool: (8, 8)
layer = conv2d(layer, params[lnum][0], border_mode='half') + \
params[lnum][1].dimshuffle('x', 0, 'x', 'x')
layer = maxout(layer, featMaps[lnum], pieces[lnum])
layer = pool_2d(layer, (2, 2), st=(2, 2), ignore_border=False, mode='max')
layer = basicUtils.dropout(layer, pDropConv)
lnum += 1 # conv: (8, 8) pool: (4, 4)
layer = conv2d(layer, params[lnum][0], border_mode='half') + \
params[lnum][1].dimshuffle('x', 0, 'x', 'x')
layer = maxout(layer, featMaps[lnum], pieces[lnum])
layer = pool_2d(layer, (2, 2), st=(2, 2), ignore_border=False, mode='max')
layer = basicUtils.dropout(layer, pDropConv)
lnum += 1
layer = T.flatten(layer, outdim=2)
layer = T.dot(layer, params[lnum][0]) + params[lnum][1].dimshuffle('x', 0)
layer = relu(layer, alpha=0)
layer = basicUtils.dropout(layer, pDropHidden)
lnum += 1
layer = T.dot(layer, params[lnum][0]) + params[lnum][1].dimshuffle('x', 0)
layer = relu(layer, alpha=0)
layer = basicUtils.dropout(layer, pDropHidden)
lnum += 1
return softmax(T.dot(layer, params[lnum][0]) + params[lnum][1].dimshuffle('x', 0)) # 如果使用nnet中的softmax训练产生NAN
示例8: relevance_conv_a_b_abs
def relevance_conv_a_b_abs(inputs, weights, out_relevances, a, b, bias=None):
assert a is not None
assert b is not None
assert a - b == 1
weights_plus = weights * T.gt(weights, 0)
weights_neg = weights * T.lt(weights, 0)
plus_norm = conv2d(T.abs_(inputs), weights_plus)
# stabilize, prevent division by 0
eps = 1e-4
plus_norm += T.eq(plus_norm, 0) * eps
plus_rel_normed = out_relevances / plus_norm
in_rel_plus = conv2d(plus_rel_normed, weights_plus.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full")
in_rel_plus *= T.abs_(inputs)
# minuses to get positive outputs, since will be subtracted
# at end of function
neg_norm = -conv2d(T.abs_(inputs), weights_neg)
neg_norm += T.eq(neg_norm, 0) * eps
neg_rel_normed = out_relevances / neg_norm
in_rel_neg = -conv2d(neg_rel_normed, weights_neg.dimshuffle(1, 0, 2, 3)[:, :, ::-1, ::-1], border_mode="full")
in_rel_neg *= T.abs_(inputs)
in_relevance = a * in_rel_plus - b * in_rel_neg
return in_relevance
示例9: metaOp1
def metaOp1(i, j, X, w1, w2, b1, b2):
# (n,1,r,c)**(16,1,3,3)=(n,16,r,c)
hiddens = conv2d(X[:, j, :, :, :], w1[i, j, :, :, :, :], border_mode='half') + b1[i, j, :, :, :, :]
hiddens = relu(hiddens, alpha=0)
# (n,16,r,c)**(1,16,1,1)=(n,1,r,c)
outputs = conv2d(hiddens, w2[i, j, :, :, :, :], border_mode='valid') + b2[i, j, :, :, :, :]
outputs = relu(outputs, alpha=0)
return outputs
示例10: metaOp
def metaOp(i, j, X, w1, w2, b1, b2):
# (n,1,r,c)**(16,1,3,3)=(n,16,r,c)
hiddens = conv2d(X[:, j, :, :, :], w1[i, j, :, :, :, :], border_mode='half') + b1[i, j, :, :, :, :]
hiddens = relu(hiddens, alpha=0)
# 在元操作中就需要包含relu激活
# return conv2d(hiddens, w2[i, j, :, :, :, :], border_mode='valid') + b2[i, j, :, :, :, :]
# (n,16,r,c)**(1,16,1,1)=(n,1,r,c)
outputs = conv2d(hiddens, w2[i, j, :, :, :, :], border_mode='valid') + b2[i, j, :, :, :, :]
return T.nnet.relu(outputs)
示例11: connect_through
def connect_through(self, alm1_in=None, Pl=None, Ll=None):
""" connect_through
@note Note that I made connect_through a separate class method, separate from the automatic initialization,
because you can then make changes to the "layer units" or "nodes" before "connecting the layers"
"""
if alm1_in is not None:
self.alm1 = alm1_in
self.Pl = Pl
alm1 = self.alm1
c = self.c
C_lm1,C_l = self.C_ls
Wl = self.Wl
filter_shape = (C_l,C_lm1)+Wl
assert len(filter_shape) == (2+len(Wl))
# convolve input feature maps with filters
if Ll is not None:
batch_size = alm1.shape[0] # This is m, number of input examples
# image_shape = (batch_size,C_lm1)+Ll
image_shape = (None,C_lm1)+Ll
conv_out = conv2d(
input=alm1,
filters=c,
filter_shape=filter_shape,
input_shape=image_shape)
else:
conv_out = conv2d(
input=alm1,
filters=c,
filter_shape=filter_shape)
# pool each feature map individually, using maxpooling
if Pl is not None:
pooled_out = pool.pool_2d(
input=conv_out,
ws=Pl,
ignore_border=True)
# add bias term
if self.psi is None:
self.al = pooled_out + self.b.dimshuffle('x',0,'x','x')
else:
self.al = self.psi( pooled_out + self.b.dimshuffle('x',0,'x','x') )
else:
# add bias term
if self.psi is None:
self.al = conv_out + self.b.dimshuffle('x',0,'x','x')
else:
self.al = self.psi( conv_out + self.b.dimshuffle('x',0,'x','x') )
return self.al
示例12: CNN
def CNN(x,c_l1,c_l2,f_l1,f_l2):
conv1=tensor.nnet.relu(conv2d(x,c_l1)) #default stride=1 --subsample=(1,1)
pool1=pool_2d(conv1,(2,2),st=(2,2),ignore_border=True) #default maxpool
conv2=tensor.nnet.relu(conv2d(pool1,c_l2))
pool2=pool_2d(conv2,(2,2),st=(2,2),ignore_border=True)
fpool2=tensor.flatten(pool2,outdim=2)
full1=tensor.nnet.relu(tensor.dot(fpool2,f_l1))
pyx=tensor.nnet.sigmoid(tensor.dot(full1,f_l2))
return c_l1, c_l2, f_l1, f_l2, pyx
示例13: predict_custom_image
def predict_custom_image(params, testImgFilename='own_0.png', activation= activation_convmlp, testImgFilenameDir = '../data/custom/'):
test_img_value = filter(str.isdigit, testImgFilename)
test_img = fli.processImg(testImgFilenameDir, testImgFilename)
nkerns = [20, 50]
batch_size = 1
poolsize = (2, 2)
layer0_input = test_img.reshape((batch_size, 1, 28, 28)).astype(numpy.float32)
conv_out_0 = conv2d(
input=layer0_input,
filters=params[6],
input_shape=(batch_size, 1, 28, 28),
filter_shape=(nkerns[0], 1, 5, 5)
)
# downsample each feature map individually, using maxpooling
pooled_out_0 = downsample.max_pool_2d(
input=conv_out_0,
ds=poolsize,
ignore_border=True
)
output_0 = activation(pooled_out_0 + params[7].dimshuffle('x', 0, 'x', 'x'))
conv_out_1 = conv2d(
input=output_0,
filters=params[4],
input_shape=(batch_size, nkerns[0], 12, 12),
filter_shape=(nkerns[1], nkerns[0], 5, 5),
)
# downsample each feature map individually, using maxpooling
pooled_out_1 = downsample.max_pool_2d(
input=conv_out_1,
ds=poolsize,
ignore_border=True
)
output_1 = activation(pooled_out_1 + params[5].dimshuffle('x', 0, 'x', 'x'))
output_2 = activation(T.dot(output_1.flatten(2), params[2]) + params[3])
final_output = T.dot(output_2, params[0]) + params[1]
p_y_given_x = T.nnet.softmax(final_output)
y_pred = T.argmax(p_y_given_x, axis=1)
testfunc = theano.function([], [y_pred[0]])
prediction = testfunc()[0]
correct = (int(test_img_value) == prediction)
print('The prediction ' + str(testfunc()[0]) + ' for ' + testImgFilename + ' is ' + str(correct) + '.')
return correct
示例14: _forward_negative_z
def _forward_negative_z(inputs, weights, bias=None):
inputs_plus = inputs * T.gt(inputs, 0)
weights_plus = weights * T.gt(weights, 0)
inputs_minus = inputs * T.lt(inputs, 0)
weights_minus = weights * T.lt(weights, 0)
negative_part_a = conv2d(inputs_plus, weights_minus)
negative_part_b = conv2d(inputs_minus, weights_plus)
together = negative_part_a + negative_part_b
if bias is not None:
bias_negative = bias * T.lt(bias, 0)
together += bias_negative.dimshuffle("x", 0, "x", "x")
return together
示例15: conv2d_test_sets
def conv2d_test_sets():
def _convert(input, kernel, output, kwargs):
return [theano.shared(floatX(input)), floatX(kernel), output, kwargs]
input = np.random.random((3, 1, 16, 16))
kernel = np.random.random((16, 1, 3, 3))
output = conv2d(input, kernel).eval()
yield _convert(input, kernel, output, {})
input = np.random.random((3, 3, 16, 16))
kernel = np.random.random((16, 3, 3, 3))
output = conv2d(input, kernel).eval()
yield _convert(input, kernel, output, {})