本文整理汇总了Python中pylearn2.linear.matrixmul.MatrixMul类的典型用法代码示例。如果您正苦于以下问题:Python MatrixMul类的具体用法?Python MatrixMul怎么用?Python MatrixMul使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MatrixMul类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_input_space
def set_input_space(self, space):
""" Note: this resets parameters! """
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
self.output_space = VectorSpace(self.dim)
rng = self.dbm.rng
if self.irange is not None:
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.dim)) * \
(rng.uniform(0.,1., (self.input_dim, self.dim))
< self.include_prob)
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.dim))
W *= self.sparse_stdev
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W ,= self.transformer.get_params()
assert W.name is not None
示例2: set_input_space
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if self.fprop_code==True:
self.output_space = VectorSpace(self.dim)
else:
self.output_space = VectorSpace(self.input_dim)
rng = self.mlp.rng
W = rng.randn(self.input_dim, self.dim)
self.W = sharedX(W.T, self.layer_name + '_W')
self.transformer = MatrixMul(self.W)
self.W, = self.transformer.get_params()
b = np.zeros((self.input_dim,))
self.b = sharedX(b, self.layer_name + '_b') # We need both to pass input_dim valid
X = .001 * rng.randn(self.batch_size, self.dim)
self.X = sharedX(X, self.layer_name + '_X')
self._params = [self.W, self.b, self.X]
self.state_below = T.zeros((self.batch_size, self.input_dim))
示例3: set_input_space
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
self.output_space = VectorSpace(self.dim)
rng = self.mlp.rng
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.dim))
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W, = self.transformer.get_params()
assert W.name is not None
示例4: set_input_space
def set_input_space(self, space):
""" Note: this resets parameters! """
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if not (self.detector_layer_dim % self.pool_size == 0):
raise ValueError("detector_layer_dim = %d, pool_size = %d. Should be divisible but remainder is %d" %
(self.detector_layer_dim, self.pool_size, self.detector_layer_dim % self.pool_size))
self.h_space = VectorSpace(self.detector_layer_dim)
self.pool_layer_dim = self.detector_layer_dim / self.pool_size
self.output_space = VectorSpace(self.pool_layer_dim)
rng = self.mlp.rng
if self.irange is not None:
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.detector_layer_dim)) * \
(rng.uniform(0.,1., (self.input_dim, self.detector_layer_dim))
< self.include_prob)
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.detector_layer_dim))
def mask_rejects(idx, i):
if self.mask_weights is None:
return False
return self.mask_weights[idx, i] == 0.
for i in xrange(self.detector_layer_dim):
assert self.sparse_init <= self.input_dim
for j in xrange(self.sparse_init):
idx = rng.randint(0, self.input_dim)
while W[idx, i] != 0 or mask_rejects(idx, i):
idx = rng.randint(0, self.input_dim)
W[idx, i] = rng.randn()
W *= self.sparse_stdev
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W ,= self.transformer.get_params()
assert W.name is not None
if self.mask_weights is not None:
expected_shape = (self.input_dim, self.detector_layer_dim)
if expected_shape != self.mask_weights.shape:
raise ValueError("Expected mask with shape "+str(expected_shape)+" but got "+str(self.mask_weights.shape))
self.mask = sharedX(self.mask_weights)
示例5: __init__
class Adam:
def __init__(self, batch_size, alpha, irange):
self.alpha = alpha
self.visible_layer = GaussianConvolutionalVisLayer(rows = 32,cols = 32, channels = 3, init_beta =1., init_mu = 0.)
self.hidden_layers = [ Softmax(n_classes = 10,
irange = .01) ]
rng = np.random.RandomState([2012,8,20])
self.W = MatrixMul( sharedX( rng.uniform(-irange, irange, (108,1600))))
#make_random_conv2D(irange = .05, input_space = self.visible_layer.get_input_space(),
# output_space = Conv2DSpace([27,27],1600),
# kernel_shape = (6,6),
# batch_size = batch_size)
self.batch_size = batch_size
self.hidden_layers[0].dbm = self
self.hidden_layers[0].set_input_space(Conv2DSpace([2,2],3200))
def get_params(self):
return set(self.hidden_layers[0].get_params()).union(self.W.get_params())
def mf(self, X):
patches = cifar10neighbs(X,(6,6))
patches -= patches.mean(axis=1).dimshuffle(0,'x')
patches /= T.sqrt(T.sqr(patches).sum(axis=1)+10.0).dimshuffle(0,'x')
Z = self.W.lmul(patches)
#Z = Print('Z',attrs=['min','mean','max'])(Z)
Z = T.concatenate((Z,-Z),axis=1)
Z = multichannel_neibs2imgs(Z, self.batch_size, 27, 27, 3200, 1, 1)
Z = Z.dimshuffle(0,3,1,2)
p = max_pool_2d(Z,(14,14),False)
p = p.dimshuffle(0,1,2,3)
p = T.maximum(p - self.alpha, 0.)
#p = Print('p',attrs=['min','mean','max'])(p)
y = self.hidden_layers[0].mf_update(state_below = p, state_above = None)
return [ Z, y ]
def get_weights_topo(self):
outp, inp, rows, cols = range(4)
raw = self.W._filters.get_value()
return np.transpose(raw,(outp,rows,cols,inp))
示例6: set_input_space
def set_input_space(self, space):
""" Note: this resets parameters! """
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if not (self.detector_layer_dim % self.pool_size == 0):
raise ValueError("detector_layer_dim = %d, pool_size = %d. Should be divisible but remainder is %d" %
(self.detector_layer_dim, self.pool_size, self.detector_layer_dim % self.pool_size))
self.h_space = VectorSpace(self.detector_layer_dim)
self.pool_layer_dim = self.detector_layer_dim / self.pool_size
self.output_space = VectorSpace(self.pool_layer_dim)
rng = self.dbm.rng
if self.irange is not None:
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.detector_layer_dim)) * \
(rng.uniform(0.,1., (self.input_dim, self.detector_layer_dim))
< self.include_prob)
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.detector_layer_dim))
for i in xrange(self.detector_layer_dim):
for j in xrange(self.sparse_init):
idx = rng.randint(0, self.input_dim)
while W[idx, i] != 0:
idx = rng.randint(0, self.input_dim)
W[idx, i] = rng.randn()
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W ,= self.transformer.get_params()
assert W.name is not None
示例7: set_input_space
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if self.fprop_code==True:
self.output_space = VectorSpace(self.dim)
else:
self.output_space = VectorSpace(self.input_dim)
rng = self.mlp.rng
W = rng.randn(self.input_dim, self.dim)
self.W = sharedX(W.T, self.layer_name + '_W')
self.transformer = MatrixMul(self.W)
self.W, = self.transformer.get_params()
b = np.zeros((self.input_dim,))
self.b = sharedX(b, self.layer_name + '_b') # We need both to pass input_dim valid
X = .001 * rng.randn(self.batch_size, self.dim)
self.X = sharedX(X, self.layer_name + '_X')
S = rng.normal(0, .001, size=(self.batch_size, self.input_dim))
self.S = sharedX(S, self.layer_name + '_S')
self._params = [self.W, self.b]
#self.state_below = T.zeros((self.batch_size, self.input_dim))
cost = self.get_local_cost()
self.opt = top.Optimizer(self.X, cost,
method='rmsprop',
learning_rate=self.lr, momentum=.9)
self._reconstruction = theano.function([], T.dot(self.X, self.W))
示例8: set_input_space
def set_input_space(self, space):
"""
Tells the layer to use the specified input space.
This resets parameters! The weight matrix is initialized with the
size needed to receive input from this space.
Parameters
----------
space : Space
The Space that the input will lie in.
"""
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if not (0 == ((self.detector_layer_dim - self.pool_size) %
self.pool_stride)):
if self.pool_stride == self.pool_size:
raise ValueError("detector_layer_dim = %d, pool_size = %d. "
"Should be divisible but remainder is %d" %
(self.detector_layer_dim,
self.pool_size,
self.detector_layer_dim % self.pool_size))
raise ValueError()
self.h_space = VectorSpace(self.detector_layer_dim)
self.pool_layer_dim = ((self.detector_layer_dim - self.pool_size) /
self.pool_stride + 1)
self.output_space = VectorSpace(self.pool_layer_dim)
rng = self.mlp.rng
if self.irange is not None:
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.detector_layer_dim)) * \
(rng.uniform(0., 1., (self.input_dim, self.detector_layer_dim))
< self.include_prob)
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.detector_layer_dim))
def mask_rejects(idx, i):
if self.mask_weights is None:
return False
return self.mask_weights[idx, i] == 0.
for i in xrange(self.detector_layer_dim):
assert self.sparse_init <= self.input_dim
for j in xrange(self.sparse_init):
idx = rng.randint(0, self.input_dim)
while W[idx, i] != 0 or mask_rejects(idx, i):
idx = rng.randint(0, self.input_dim)
W[idx, i] = rng.randn()
W *= self.sparse_stdev
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W, = self.transformer.get_params()
assert W.name is not None
if not hasattr(self, 'randomize_pools'):
self.randomize_pools = False
if self.randomize_pools:
permute = np.zeros((self.detector_layer_dim,
self.detector_layer_dim))
for j in xrange(self.detector_layer_dim):
i = rng.randint(self.detector_layer_dim)
permute[i, j] = 1
self.permute = sharedX(permute)
if self.mask_weights is not None:
expected_shape = (self.input_dim, self.detector_layer_dim)
if expected_shape != self.mask_weights.shape:
raise ValueError("Expected mask with shape " +
str(expected_shape) +
" but got " +
str(self.mask_weights.shape))
self.mask = sharedX(self.mask_weights)
示例9: RectifiedLinear
class RectifiedLinear(Layer):
"""
WRITEME
"""
def __init__(self,
dim,
layer_name,
irange = None,
istdev = None,
sparse_init = None,
sparse_stdev = 1.,
include_prob = 1.0,
init_bias = 0.,
W_lr_scale = None,
b_lr_scale = None,
mask_weights = None,
left_slope = 0.0,
copy_input = 0,
max_row_norm = None):
"""
include_prob: probability of including a weight element in the set
of weights initialized to U(-irange, irange). If not included
it is initialized to 0.
"""
self.__dict__.update(locals())
del self.self
self.b = sharedX( np.zeros((self.dim,)) + init_bias, name = layer_name + '_b')
def get_lr_scalers(self):
if not hasattr(self, 'W_lr_scale'):
self.W_lr_scale = None
if not hasattr(self, 'b_lr_scale'):
self.b_lr_scale = None
rval = OrderedDict()
if self.W_lr_scale is not None:
W, = self.transformer.get_params()
rval[W] = self.W_lr_scale
if self.b_lr_scale is not None:
rval[self.b] = self.b_lr_scale
return rval
def set_input_space(self, space):
""" Note: this resets parameters! """
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
self.output_space = VectorSpace(self.dim + self.copy_input * self.input_dim)
rng = self.mlp.rng
if self.irange is not None:
assert self.istdev is None
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.dim)) * \
(rng.uniform(0.,1., (self.input_dim, self.dim))
< self.include_prob)
elif self.istdev is not None:
assert self.sparse_init is None
W = rng.randn(self.input_dim, self.dim) * self.istdev
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.dim))
def mask_rejects(idx, i):
if self.mask_weights is None:
return False
return self.mask_weights[idx, i] == 0.
for i in xrange(self.dim):
assert self.sparse_init <= self.input_dim
for j in xrange(self.sparse_init):
idx = rng.randint(0, self.input_dim)
while W[idx, i] != 0 or mask_rejects(idx, i):
idx = rng.randint(0, self.input_dim)
W[idx, i] = rng.randn()
W *= self.sparse_stdev
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W ,= self.transformer.get_params()
#.........这里部分代码省略.........
示例10: SparseCodingLayer
class SparseCodingLayer(Linear):
def __init__(self, batch_size, fprop_code=True, lr=.01, n_steps=10, truncate=-1, *args, **kwargs):
'''
Parameters for the optimization/feedforward operation:
lr : learning rate
n_steps : number of steps or uptades of the hidden code
truncate: truncate the gradient after this number (default -1 which means do not truncate)
'''
super(SparseCodingLayer, self).__init__(*args, **kwargs)
self.batch_size = batch_size
self.fprop_code = fprop_code
self.n_steps = n_steps
self.truncate = truncate
self.lr = lr
self._scan_updates = OrderedDict()
@wraps(Linear.set_input_space)
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if self.fprop_code==True:
self.output_space = VectorSpace(self.dim)
else:
self.output_space = VectorSpace(self.input_dim)
rng = self.mlp.rng
W = rng.randn(self.input_dim, self.dim)
self.W = sharedX(W.T, self.layer_name + '_W')
self.transformer = MatrixMul(self.W)
self.W, = self.transformer.get_params()
b = np.zeros((self.input_dim,))
self.b = sharedX(b, self.layer_name + '_b') # We need both to pass input_dim valid
X = .001 * rng.randn(self.batch_size, self.dim)
self.X = sharedX(X, self.layer_name + '_X')
self._params = [self.W, self.b, self.X]
self.state_below = T.zeros((self.batch_size, self.input_dim))
def _renormW(self):
A = self.W.get_value(borrow=True)
A = np.dot(A.T, np.diag(1./np.sqrt(np.sum(A**2, axis=1)))).T
self.W.set_value( A )
def get_local_cost(self,state_below):
er = T.sqr(state_below - T.dot(self.X, self.W)).sum()
l1 = T.sqrt(T.sqr(self.X) + 1e-6).sum()
return er + .1 * l1
def get_sparse_code(self, state_below):
def _optimization_step(Xt, accum, vt, S):
'''
Note that this is the RMSprop update.
Thus, we running gradient updates inside scan (the dream)
TODO: put this a better place.
I tried to make if a method of self, but I'm not sure how to tell
theano.scan that the first argument of the function is a non_sequence
'''
rho = .9
momentum = .9
lr = self.lr
Y = T.dot(Xt, self.W) #+ self.b
err = (S - Y) ** 2
l1 = T.sqrt(Xt**2 + 1e-6)
cost = err.sum() + .1 * l1.sum()
gX = T.grad(cost, Xt)
new_accum = rho * accum + (1-rho) * gX**2
v = momentum * vt - lr * gX / T.sqrt(new_accum + 1e-8)
X = Xt + momentum * v - lr * gX / T.sqrt(new_accum + 1e-8)
return [X, new_accum, v]
# Renorm W
self._renormW()
rng = self.mlp.rng
#X = rng.randn(self.batch_size, self.dim)
#self.X = sharedX(X, 'SparseCodingLinear_X')
'''
accum = T.zeros_like(self.X)
vt = T.zeros_like(self.X)
[Xfinal,_,_], updates = theano.scan(fn=_optimization_step,
outputs_info=[self.X, accum, vt],
non_sequences=[state_below],
n_steps=self.n_steps, truncate_gradient=self.truncate)
self._scan_updates.update(updates)
self.Xout = Xfinal[-1]
#.........这里部分代码省略.........
示例11: SoftmaxPool
#.........这里部分代码省略.........
(self.detector_layer_dim, self.pool_size, self.detector_layer_dim % self.pool_size))
self.h_space = VectorSpace(self.detector_layer_dim)
self.pool_layer_dim = self.detector_layer_dim / self.pool_size
self.output_space = VectorSpace(self.pool_layer_dim)
rng = self.mlp.rng
if self.irange is not None:
assert self.sparse_init is None
W = rng.uniform(-self.irange,
self.irange,
(self.input_dim, self.detector_layer_dim)) * \
(rng.uniform(0.,1., (self.input_dim, self.detector_layer_dim))
< self.include_prob)
else:
assert self.sparse_init is not None
W = np.zeros((self.input_dim, self.detector_layer_dim))
def mask_rejects(idx, i):
if self.mask_weights is None:
return False
return self.mask_weights[idx, i] == 0.
for i in xrange(self.detector_layer_dim):
assert self.sparse_init <= self.input_dim
for j in xrange(self.sparse_init):
idx = rng.randint(0, self.input_dim)
while W[idx, i] != 0 or mask_rejects(idx, i):
idx = rng.randint(0, self.input_dim)
W[idx, i] = rng.randn()
W *= self.sparse_stdev
W = sharedX(W)
W.name = self.layer_name + '_W'
self.transformer = MatrixMul(W)
W ,= self.transformer.get_params()
assert W.name is not None
if self.mask_weights is not None:
expected_shape = (self.input_dim, self.detector_layer_dim)
if expected_shape != self.mask_weights.shape:
raise ValueError("Expected mask with shape "+str(expected_shape)+" but got "+str(self.mask_weights.shape))
self.mask = sharedX(self.mask_weights)
def censor_updates(self, updates):
# Patch old pickle files
if not hasattr(self, 'mask_weights'):
self.mask_weights = None
if self.mask_weights is not None:
W ,= self.transformer.get_params()
if W in updates:
updates[W] = updates[W] * self.mask
def get_params(self):
assert self.b.name is not None
W ,= self.transformer.get_params()
assert W.name is not None
rval = self.transformer.get_params()
assert not isinstance(rval, set)
rval = list(rval)
assert self.b not in rval
rval.append(self.b)
return rval
示例12: CpuConvMaxout
#.........这里部分代码省略.........
input_space=self.input_space,
output_space=self.detector_space,
kernel_shape=self.kernel_shape,
batch_size=self.mlp.batch_size,
subsample=self.kernel_stride,
border_mode=self.border_mode,
rng=rng)
W, = self.transformer.get_params()
W.name = 'W'
self.b = sharedX(np.zeros(((self.num_pieces*self.output_channels),)) + self.init_bias)
self.b.name = 'b'
print 'Input shape: ', self.input_space.shape
print 'Detector space: ', self.detector_space.shape
assert self.pool_type in ['max', 'mean']
dummy_batch_size = self.mlp.batch_size
if dummy_batch_size is None:
dummy_batch_size = 2
dummy_detector = sharedX(
self.detector_space.get_origin_batch(dummy_batch_size))
#dummy_p = dummy_p.eval()
self.output_space = Conv2DSpace(shape=[1, 1],
num_channels=self.output_channels,
axes=('b', 'c', 0, 1))
W = rng.uniform(-self.irange,self.irange,(426, (self.num_pieces*self.output_channels)))
W = sharedX(W)
W.name = self.layer_name + "_w"
self.transformer = MatrixMul(W)
print 'Output space: ', self.output_space.shape
@wraps(Layer.censor_updates)
def censor_updates(self, updates):
"""
.. todo::
WRITEME
"""
if self.max_kernel_norm is not None:
W, = self.transformer.get_params()
if W in updates:
updated_W = updates[W]
row_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=(1)))
desired_norms = T.clip(row_norms, 0, self.max_kernel_norm)
scales = desired_norms / (1e-7 + row_norms)
updates[W] = updated_W * scales.dimshuffle(0, 'x')
@wraps(Layer.get_params)
def get_params(self):
"""
.. todo::
WRITEME
"""
assert self.b.name is not None
W, = self.transformer.get_params()
assert W.name is not None
rval = self.transformer.get_params()
assert not isinstance(rval, set)
示例13: set_input_space
def set_input_space(self, space):
self.input_space = space
if not isinstance(space, Conv2DSpace):
raise BadInputSpaceError("ConvRectifiedLinear.set_input_space "
"expected a Conv2DSpace, got " +
str(space) + " of type " +
str(type(space)))
rng = self.mlp.rng
if self.border_mode == 'valid':
output_shape = [(self.input_space.shape[0]-self.kernel_shape[0]) /
self.kernel_stride[0] + 1,
(self.input_space.shape[1]-self.kernel_shape[1]) /
self.kernel_stride[1] + 1]
elif self.border_mode == 'full':
output_shape = [(self.input_space.shape[0]+self.kernel_shape[0]) /
self.kernel_stride[0] - 1,
(self.input_space.shape[1]+self.kernel_shape[1]) /
self.kernel_stride[1] - 1]
self.detector_space = Conv2DSpace(shape=output_shape,
num_channels=self.output_channels,
axes=('b', 'c', 0, 1))
if self.irange is not None:
assert self.sparse_init is None
self.transformer = conv2d.make_random_conv2D(
irange=self.irange,
input_space=self.input_space,
output_space=self.detector_space,
kernel_shape=self.kernel_shape,
batch_size=self.mlp.batch_size,
subsample=self.kernel_stride,
border_mode=self.border_mode,
rng=rng)
elif self.sparse_init is not None:
self.transformer = conv2d.make_sparse_random_conv2D(
num_nonzero=self.sparse_init,
input_space=self.input_space,
output_space=self.detector_space,
kernel_shape=self.kernel_shape,
batch_size=self.mlp.batch_size,
subsample=self.kernel_stride,
border_mode=self.border_mode,
rng=rng)
W, = self.transformer.get_params()
W.name = 'W'
self.b = sharedX(np.zeros(((self.num_pieces*self.output_channels),)) + self.init_bias)
self.b.name = 'b'
print 'Input shape: ', self.input_space.shape
print 'Detector space: ', self.detector_space.shape
assert self.pool_type in ['max', 'mean']
dummy_batch_size = self.mlp.batch_size
if dummy_batch_size is None:
dummy_batch_size = 2
dummy_detector = sharedX(
self.detector_space.get_origin_batch(dummy_batch_size))
#dummy_p = dummy_p.eval()
self.output_space = Conv2DSpace(shape=[1, 1],
num_channels=self.output_channels,
axes=('b', 'c', 0, 1))
W = rng.uniform(-self.irange,self.irange,(426, (self.num_pieces*self.output_channels)))
W = sharedX(W)
W.name = self.layer_name + "_w"
self.transformer = MatrixMul(W)
print 'Output space: ', self.output_space.shape
示例14: SparseCodingLayer
class SparseCodingLayer(Linear):
def __init__(self, batch_size, fprop_code=True, lr=.01, n_steps=10, lbda=0, top_most=False,
nonlinearity=RectifierConvNonlinearity(),*args, **kwargs):
'''
Compiled version: the sparse code is calulated using 'top' and is not just simbolic.
Parameters for the optimization/feedforward operation:
lr : learning rate
n_steps : number of steps or uptades of the hidden code
truncate: truncate the gradient after this number (default -1 which means do not truncate)
'''
super(SparseCodingLayer, self).__init__(*args, **kwargs)
self.batch_size = batch_size
self.fprop_code = fprop_code
self.n_steps = n_steps
self.lr = lr
self.lbda = lbda
self.top_most = top_most
self.nonlin = nonlinearity
@wraps(Linear.set_input_space)
def set_input_space(self, space):
self.input_space = space
if isinstance(space, VectorSpace):
self.requires_reformat = False
self.input_dim = space.dim
else:
self.requires_reformat = True
self.input_dim = space.get_total_dimension()
self.desired_space = VectorSpace(self.input_dim)
if self.fprop_code==True:
self.output_space = VectorSpace(self.dim)
else:
self.output_space = VectorSpace(self.input_dim)
rng = self.mlp.rng
W = rng.randn(self.input_dim, self.dim)
self.W = sharedX(W.T, self.layer_name + '_W')
self.transformer = MatrixMul(self.W)
self.W, = self.transformer.get_params()
b = np.zeros((self.input_dim,))
self.b = sharedX(b, self.layer_name + '_b') # We need both to pass input_dim valid
X = .001 * rng.randn(self.batch_size, self.dim)
self.X = sharedX(X, self.layer_name + '_X')
S = rng.normal(0, .001, size=(self.batch_size, self.input_dim))
self.S = sharedX(S, self.layer_name + '_S')
self._params = [self.W, self.b]
#self.state_below = T.zeros((self.batch_size, self.input_dim))
cost = self.get_local_cost()
self.opt = top.Optimizer(self.X, cost,
method='rmsprop',
learning_rate=self.lr, momentum=.9)
self._reconstruction = theano.function([], T.dot(self.X, self.W))
def get_local_cost(self):
er = T.sqr(self.S - T.dot(self.X, self.W)).sum()
l1 = T.sqrt(T.sqr(self.X) + 1e-6).sum()
top_down = self.get_top_down_flow()
return er + .1 * l1 + top_down
def update_top_state(self, state_above=None):
if self.lbda is not 0:
assert state_above is not None
self.top_flow.set_value(state_above)
def get_nonlin_output(self):
return self.nonlin(self.X)
def get_top_down_flow(self):
if self.lbda == 0:
rval = 0.
elif self.top_flow == True:
rval = (self.lbda * (self.top_flow - self.X)**2).sum()
else:
out = self.get_nonlin_output()
rval = (self.lbda * (self.top_flow - out)**2).sum()
return rval
def _renormW(self):
A = self.W.get_value(borrow=True)
A = np.dot(A.T, np.diag(1./np.sqrt(np.sum(A**2, axis=1)))).T
self.W.set_value( A )
def get_reconstruction(self):
return self._reconstruction()
def get_sparse_code(self, state_below):
# Renorm W
self._renormW()
if hasattr(state_below, 'get_value'):
#print '!!!! state_below does have get_value'
self.S.set_value(state_below.get_value(borrow=True))
#.........这里部分代码省略.........
示例15: __init__
def __init__(self, nvis = None, nhid = None,
vis_space = None,
hid_space = None,
transformer = None,
irange=0.5, rng=None, init_bias_vis = None,
init_bias_vis_marginals = None, init_bias_hid=0.0,
base_lr = 1e-3, anneal_start = None, nchains = 100, sml_gibbs_steps = 1,
random_patches_src = None,
monitor_reconstruction = False):
"""
Construct an RBM object.
Parameters
----------
nvis : int
Number of visible units in the model.
(Specifying this implies that the model acts on a vector,
i.e. it sets vis_space = pylearn2.space.VectorSpace(nvis) )
nhid : int
Number of hidden units in the model.
(Specifying this implies that the model acts on a vector)
vis_space:
A pylearn2.space.Space object describing what kind of vector
space the RBM acts on. Don't specify if you used nvis / hid
hid_space:
A pylearn2.space.Space object describing what kind of vector
space the RBM's hidden units live in. Don't specify if you used
nvis / nhid
init_bias_vis_marginals: either None, or a Dataset to use to initialize
the visible biases to the inverse sigmoid of the data marginals
irange : float, optional
The size of the initial interval around 0 for weights.
rng : RandomState object or seed
NumPy RandomState object to use when initializing parameters
of the model, or (integer) seed to use to create one.
init_bias_vis : array_like, optional
Initial value of the visible biases, broadcasted as necessary.
init_bias_hid : array_like, optional
initial value of the hidden biases, broadcasted as necessary.
monitor_reconstruction : if True, will request a monitoring channel to monitor
reconstruction error
random_patches_src: Either None, or a Dataset from which to draw random patches
in order to initialize the weights. Patches will be multiplied by irange
Parameters for default SML learning rule:
base_lr : the base learning rate
anneal_start : number of steps after which to start annealing on a 1/t schedule
nchains: number of negative chains
sml_gibbs_steps: number of gibbs steps to take per update
"""
Model.__init__(self)
Block.__init__(self)
if init_bias_vis_marginals is not None:
assert init_bias_vis is None
X = init_bias_vis_marginals.X
assert X.min() >= 0.0
assert X.max() <= 1.0
marginals = X.mean(axis=0)
#rescale the marginals a bit to avoid NaNs
init_bias_vis = inverse_sigmoid_numpy(.01 + .98 * marginals)
if init_bias_vis is None:
init_bias_vis = 0.0
if rng is None:
# TODO: global rng configuration stuff.
rng = numpy.random.RandomState(1001)
self.rng = rng
if vis_space is None:
#if we don't specify things in terms of spaces and a transformer,
#assume dense matrix multiplication and work off of nvis, nhid
assert hid_space is None
assert transformer is None or isinstance(transformer,MatrixMul)
assert nvis is not None
assert nhid is not None
if transformer is None:
if random_patches_src is None:
W = rng.uniform(-irange, irange, (nvis, nhid))
else:
if hasattr(random_patches_src, '__array__'):
W = irange * random_patches_src.T
assert W.shape == (nvis, nhid)
else:
#assert type(irange) == type(0.01)
#assert irange == 0.01
W = irange * random_patches_src.get_batch_design(nhid).T
self.transformer = MatrixMul( sharedX(
W,
name='W',
#.........这里部分代码省略.........