Python theano.tensor 模块,repeat() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.repeat()。
def rbf_kernel(X0):
XY = T.dot(X0, X0.transpose())
x2 = T.reshape(T.sum(T.square(X0), axis=1), (X0.shape[0], 1))
X2e = T.repeat(x2, X0.shape[0], axis=1)
H = T.sub(T.add(X2e, X2e.transpose()), 2 * XY)
V = H.flatten()
# median distance
h = T.switch(T.eq((V.shape[0] % 2), 0),
# if even vector
T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]),
# if odd vector
T.sort(V)[V.shape[0] // 2])
h = T.sqrt(0.5 * h / T.log(X0.shape[0].astype('float32') + 1.0)) / 2.
Kxy = T.exp(-H / h ** 2 / 2.0)
neighbors = T.argsort(H, axis=1)[:, 1]
return Kxy, neighbors, h
def rbf_kernel(X):
XY = T.dot(X, X.T)
x2 = T.sum(X**2, axis=1).dimshuffle(0, 'x')
X2e = T.repeat(x2, X.shape[0], axis=1)
H = X2e + X2e.T - 2. * XY
V = H.flatten()
# median distance
h = T.switch(T.eq((V.shape[0] % 2),
# if odd vector
T.sort(V)[V.shape[0] // 2])
h = T.sqrt(.5 * h / T.log(H.shape[0].astype('float32') + 1.))
# compute the rbf kernel
kxy = T.exp(-H / (h ** 2) / 2.0)
dxkxy = -T.dot(kxy, X)
sumkxy = T.sum(kxy, 'x')
dxkxy = T.add(dxkxy, T.mul(X, sumkxy)) / (h ** 2)
return kxy, dxkxy
def get_output_for(self, input, **kwargs):
a, b, c = self.scale_factor
upscaled = input
if self.mode == 'repeat':
if c > 1:
upscaled = T.extra_ops.repeat(upscaled, c, 4)
if b > 1:
upscaled = T.extra_ops.repeat(upscaled, 3)
if a > 1:
upscaled = T.extra_ops.repeat(upscaled, a, 2)
elif self.mode == 'dilate':
if c > 1 or b > 1 or a > 1:
output_shape = self.get_output_shape_for(input.shape)
upscaled = T.zeros(shape=output_shape, dtype=input.dtype)
upscaled = T.set_subtensor(
upscaled[:, :, ::a, ::b, ::c], input)
return upscaled
def tanimoto_wmap(target_in, prediction, eps=1e-8):
'''
Tanimoto distance,see: https://en.wikipedia.org/wiki/Jaccard_index#Other_deFinitions_of_Tanimoto_distance
'''
target_in = T.reshape(target_in, (target_in.shape[1], target_in.shape[2]))
target = target_in[:, :2]
wmap = T.repeat(target_in[:, 2].dimshuffle(('x', 0)), 2, axis=0).dimshuffle((1, 0))
prediction = T.reshape(prediction, (prediction.shape[1], prediction.shape[2]))
prediction = T.clip(prediction, eps, 1 - eps)
target_w = T.sum(T.sqr(target * wmap), axis=0, keepdims=True)
pred_w = T.sum(T.sqr(prediction * wmap), keepdims=True)
intersection_w = T.sum(target_w * pred_w, keepdims=True)
intersection = T.sum(target * prediction, keepdims=True)
prediction_sq = T.sum(T.sqr(prediction), keepdims=True)
target_sq = T.sum(T.sqr(target), keepdims=True)
loss = (target_w + pred_w - 2 * intersection_w) / (target_sq + prediction_sq - intersection)
return loss
def initial_states(self, batch_size):
initial_h1 = self.rnn1.initial_states(batch_size)
initial_h2 = self.rnn2.initial_states(batch_size)
initial_h3 = self.rnn3.initial_states(batch_size)
last_h1 = shared_floatx_zeros((batch_size, self.rnn_h_dim))
last_h2 = shared_floatx_zeros((batch_size, self.rnn_h_dim))
last_h3 = shared_floatx_zeros((batch_size, self.rnn_h_dim))
# Defining for all
initial_k = tensor.zeros(
(batch_size, self.attention_size), dtype=floatX)
last_k = shared_floatx_zeros((batch_size, self.attention_size))
# Trainable initial state for w. Why not for k?
initial_w = tensor.repeat(self.initial_w[None, :], batch_size, 0)
last_w = shared_floatx_zeros((batch_size, self.encoded_input_dim))
return initial_h1, last_h1, initial_h2, last_h2, initial_h3, last_h3, \
initial_w, last_w, initial_k, last_k
def _k_max_pooling(input, kmax):
pool = input.dimshuffle(0, 1, 3).flatten(ndim=3).dimshuffle(1,0,2).flatten(ndim=2).dimshuffle(1,0)
neighborsArgSorted = T.argsort(pool, axis=1)
yy = T.sort(neighborsArgSorted[:, -kmax:], axis=1).flatten()
xx = T.repeat(T.arange(neighborsArgSorted.shape[0]), kmax)
pool_kmax = pool[xx, yy]
pool_kmax_shape = T.join(0, T.as_tensor([input.shape[0], input.shape[1], input.shape[3], kmax]))
pooled_out = pool_kmax.reshape(pool_kmax_shape, ndim=4).dimshuffle(0, 3, 2)
return pooled_out
def k_max_pooling(input, kmax):
nbatches, nchannels, nwords, ndim = input.shape[0], input.shape[2], input.shape[3]
x = input.dimshuffle(0,1,3,2)
neighborsArgSorted = T.argsort(x, axis=3)
ax0 = T.repeat(T.arange(nbatches), nchannels*ndim*kmax)
ax1 = T.repeat(T.arange(nchannels), ndim * kmax).dimshuffle('x', 0)
ax1 = T.repeat(ax1, nbatches, axis=0).flatten()
ax2 = T.repeat(T.arange(ndim), kmax, axis=0).dimshuffle('x', 'x', 0)
ax2 = T.repeat(ax2, axis=1)
ax2 = T.repeat(ax2, axis=0).flatten()
ax3 = T.sort(neighborsArgSorted[:,:,-kmax:], axis=3).flatten()
pooled_out = x[ax0, ax1, ax2, ax3]
pooled_out = pooled_out.reshape((nbatches, ndim, kmax)).dimshuffle(0,2)
return pooled_out
def dynamic_k_max_pooling(input, sent_sizes, k_max_factor, k_max_final):
"""
k_max_factor -- multiplied by sentence_sizes gives the value of kmax for each sentence
"""
# Unroll input into (batch_size x nchannels x nwords) x ndim
nbatches,2)
sent_sizes = T.cast(T.ceil(sent_sizes * k_max_factor), dtype='int32')
sent_sizes = T.maximum(sent_sizes, k_max_final)
# sent_sizes_matrix = T.repeat(sent_sizes,nwords,axis=1)
sent_sizes_matrix = T.repeat(sent_sizes.dimshuffle(0, 'x'), axis=1)
idx = T.arange(nwords).dimshuffle('x', 0)
idx_matrix = T.repeat(idx, axis=0)
sent_sizes_mask = T.lt(idx_matrix, sent_sizes_matrix)[:,::-1]
neighborsArgSorted = T.argsort(x, axis=3)
neighborsArgSorted_masked = ((neighborsArgSorted + 1) * sent_sizes_mask.dimshuffle(0,'x',1)) - 1
neighborsArgSorted_masked_sorted = neighborsArgSorted_masked.sort(axis=3)
nwords_max = T.cast(T.ceil(nwords * k_max_factor), 'int32')
# print nwords_max.eval()
neighborsArgSorted_masked_sorted_clipped = neighborsArgSorted_masked_sorted[:,-nwords_max:]
ax0 = T.repeat(T.arange(nbatches), nchannels*ndim*nwords_max)
ax1 = T.repeat(T.arange(nchannels), ndim * nwords_max).dimshuffle('x', nwords_max, axis=0).flatten()
ax3 = neighborsArgSorted_masked_sorted_clipped.flatten()
pooled_out = x[ax0, nwords_max)).dimshuffle(0,2)
return pooled_out
def mean_step(self, x_t, m_t, *args):
args = iter(args)
# already computed avg
avg_past = next(args)
n_past = next(args)
if m_t.ndim >= 1:
m_t = m_t.dimshuffle(0, 'x')
# reset avg
avg_past_r = m_t * avg_past
n_past_r = m_t.T * n_past
n = n_past_r + 1.0
resized_n = T.repeat(n.T, avg_past_r.shape[1], axis=1)
avg = (avg_past_r * (resized_n - 1) + x_t) / resized_n
# Old implementation:
#avg = (avg_past_r * (n[:,None] - 1) + x_t) / n[:,None]
# return state and pooled state
return avg, n
def mean_step(self, 'x')
# reset avg
avg_past_r = m_t * avg_past
n_past_r = m_t.T * n_past
n = n_past_r + 1.
resized_n = T.repeat(n.T, n
def __init__(self, layers, size=(2, 2), json_param={}):
super().__init__(layer_index=len(layers))
self.input = layers[-1].output
self.input_shape = layers[-1].output_shape
self.size=json_param.get("size", size)
#output dim
self.output_shape = (self.input_shape[0], self.input_shape[1], self.size[1]*self.input_shape[2], self.size[0]*self.input_shape[3])
self.use_optimized = theano.sandBox.cuda.cuda_enabled
if self.use_optimized:
self.output = PoolInvOp(self.size)(self.input)
else:
self.output = tensor.repeat(tensor.repeat(self.input, self.size[1], axis=2), self.size[0], axis=3)
logging.verbose("Adding", self)
def initial_state(self, name, batch_size):
'''Return an array of suitable for representing initial state.
Parameters
----------
name : str
Name of the variable to return.
batch_size : int
Number of elements in a batch. This can be symbolic.
Returns
-------
initial : theano shared variable
A variable containing the initial state of some recurrent variable.
'''
values = theano.shared(
np.zeros((1, self.size), FLOAT),
name=self._fmt('{}0'.format(name)))
return TT.repeat(values, axis=0)
def initial_states(self, *args, **kwargs):
"""Returns the initial state depending on ``init_strategy``."""
attended = kwargs['attended']
if self.init_strategy == 'constant':
initial_state = [tensor.repeat(self.parameters[2][None,
batch_size,
0)]
elif self.init_strategy == 'last':
initial_state = self.initial_transformer.apply(
attended[0, -self.attended_dim:])
elif self.init_strategy == 'average':
initial_state = self.initial_transformer.apply(
attended[:, -self.attended_dim:].mean(0))
else:
logging.fatal("dec_init parameter %s invalid" % self.init_strategy)
return initial_state
def kmaxpooling(input,input_shape,k):
sorted_values = T.argsort(input,axis=3)
topmax_indexes = sorted_values[:,-k:]
# sort indexes so that we keep the correct order within the sentence
topmax_indexes_sorted = T.sort(topmax_indexes)
#given that topmax only gives the index of the third dimension,we need to generate the other 3 dimensions
dim0 = T.arange(0,input_shape[0]).repeat(input_shape[1]*input_shape[2]*k)
dim1 = T.arange(0,input_shape[1]).repeat(k*input_shape[2]).reshape((1,-1)).repeat(input_shape[0],axis=0).flatten()
dim2 = T.arange(0,input_shape[2]).repeat(k).reshape((1,-1)).repeat(input_shape[0]*input_shape[1],axis=0).flatten()
dim3 = topmax_indexes_sorted.flatten()
return input[dim0,dim1,dim2,dim3].reshape((input_shape[0], input_shape[1], input_shape[2], k))
def dynamic_k_max_pooling(input,2)
return pooled_out
def __init__(self, incoming, scale_factor, mode='repeat', **kwargs):
super(Upscale3DLayer, self).__init__(incoming, **kwargs)
self.scale_factor = nn.utils.as_tuple(scale_factor, 3)
if self.scale_factor[0] < 1 or self.scale_factor[1] < 1 or \
self.scale_factor[2] < 1:
raise ValueError('Scale factor must be >= 1,not {0}'.format(
self.scale_factor))
if mode not in {'repeat', 'dilate'}:
msg = "Mode must be either 'repeat' or 'dilate',not {0}"
raise ValueError(msg.format(mode))
self.mode = mode
def repeat_elements(x, rep, axis):
'''Repeat the elements of a tensor along an axis,like np.repeat.
If x has shape (s1,s2,s3) and axis=1,the output
will have shape (s1,s2 * rep,s3).
'''
return T.repeat(x, axis=axis)
def repeat(x, n):
'''Repeat a 2D tensor.
If x has shape (samples,dim) and n=2,
the output will have shape (samples,2,dim).
'''
assert x.ndim == 2
x = x.dimshuffle((0, 1))
return T.extra_ops.repeat(x, n, axis=1)
def initial_states(self, **kwargs):
return [tensor.repeat(self.initial_state_[None,
tensor.repeat(self.initial_cells[None, 0)]
def matrixify(vector, n):
"""Transform a vector or a matrix into a matrix
Repeate the vector n times in the 0 axis.
Parameters
----------
vector : array_like
Vector (or matrix) to be repreated
n : int
Number of repetition
Returns
-------
theano.tensor
A theano tensor corresponding to the n times repeated vector
Example
-------
>>>> vect1.shape
(10,)
>>>> matrixify(vect1,5).shape.eval()
(5,10)
>>>> vect2.shape
(10,15)
>>>> matrixify(vect2,10,15)
"""
return T.repeat(T.shape_padleft(vector), axis=0)
def loss_func(self, y_true, y_predict):
active_notes = T.shape_padright(y_true[:,0])
mask = T.concatenate([T.ones_like(active_notes), active_notes, T.repeat(T.ones_like(active_notes), self.output_size-2, -1)], axis=-1)
loglikelihoods = mask * T.log( 2*y_predict*y_true - y_predict - y_true + 1 + self.epsilon )
return T.neg(T.sum(loglikelihoods))
def repeat(self, x, axis):
'''Repeat the elements of a tensor along an axis,like np.repeat.
If x has shape (s1,the output
will have shape (s1,s3).
'''
return T.repeat(x, axis=axis)
def repeat_elements(x, axis):
"""Repeat the elements of a tensor along an axis,s3).
"""
# Todo: `keras_shape` inference.
return T.repeat(x, axis=axis)
def repeat_elements(x,s3).
'''
# Todo: `keras_shape` inference.
return T.repeat(x, axis=axis)
def upsample_3d(inpt, to_shape, inpt_height, inpt_width, inpt_depth):
inpt_shape = np.array([inpt_height, inpt_depth])
to_shape = np.array(to_shape)
up_factors = to_shape / inpt_shape
if to_shape[0] >= up_factors[0]*inpt_shape[0]:
inpt = T.repeat(inpt, up_factors[0], axis=3)
inpt_shape[0] *= up_factors[0]
if to_shape[1] >= up_factors[1]*inpt_shape[1]:
inpt = T.repeat(inpt, up_factors[1], axis=4)
inpt_shape[1] *= up_factors[1]
if to_shape[2] >= up_factors[2]*inpt_shape[2]:
inpt = T.repeat(inpt, up_factors[2], axis=2)
inpt_shape[2] *= up_factors[2]
while to_shape[0] >= inpt_shape[0]:
reps = np.ones(inpt_shape[0], dtype='int16')
reps[-1] = 2
inpt = T.repeat(inpt, reps, axis=3)
inpt_shape[0] += 1
while to_shape[1] >= inpt_shape[1]:
reps = np.ones(inpt_shape[1], axis=4)
inpt_shape[1] += 1
while to_shape[2] >= inpt_shape[2]:
reps = np.ones(inpt_shape[2], axis=2)
inpt_shape[2] += 1
return inpt
def simple_upsample3d(inpt, up_factor):
inpt = T.repeat(inpt, up_factor[0], axis=3)
inpt = T.repeat(inpt, up_factor[1], axis=4)
inpt = T.repeat(inpt, up_factor[2], axis=1)
#rep = [1,up_factor[2],1,up_factor[0],up_factor[1]]
#inpt = T.tile(inpt,rep,ndim=5)
return inpt
def __init__(self, inpt,
inpt_depth, n_inpt, filter_height,
filter_width, filter_depth, n_output,
transfer='identity', n_samples=None,
up_factor=(2, implementation='dnn_conv3d',
bias=False,
declare=None, name=None):
self.inpt = inpt
self.inpt_height = inpt_height
self.inpt_width = inpt_width
self.inpt_depth = inpt_depth
self.n_inpt = n_inpt
self.filter_height = filter_height
self.filter_width = filter_width
self.filter_depth = filter_depth
self.n_output = n_output
if transfer != 'identity':
warnings.warn('Transfer functions can only be used in activation layers.', DeprecationWarning)
self.transfer_id = 'identity'
self.n_samples = n_samples
self.up_factor = up_factor
self.implementation = implementation
self.bias = bias
self.mode = mode
super(Deconv, self).__init__(declare=declare, name=name)
def get_outputs_info(self, n):
outputs_info = [T.repeat(self.h0[None,...], axis=0),
T.repeat(self.c0[None, axis=0)]
return outputs_info
def __init__(self, embedding_dim=100, num_hidden_layers=2, hidden_dim=200, in_dropout_p=0.2, hidden_dropout_p=0.5, update_hyperparams={'learning_rate': 0.01}):
self.embedding_dim = embedding_dim
self.num_hidden_layers = num_hidden_layers
self.hidden_dim = hidden_dim
self.in_dropout_p = in_dropout_p
self.hidden_dropout_p = update_hyperparams
print >> sys.stderr, 'Building computation graph for discriminator...'
self.input_var = T.matrix('input')
self.input_var_extra = T.matrix('input_extra')
self.target_var = T.matrix('target')
self.cos_feats = cosine_sim(self.input_var, T.repeat(self.input_var_extra, axis=0)).reshape((-1, 1))
self.total_input = T.concatenate([self.input_var, self.cos_feats], axis=1)
self.l_in = lasagne.layers.InputLayer(shape=(None, self.embedding_dim+1), input_var=self.total_input, name='l_in')
self.l_in_dr = lasagne.layers.DropoutLayer(self.l_in, 0.2)
self.layers = [self.l_in, self.l_in_dr]
for i in xrange(self.num_hidden_layers):
l_hid = lasagne.layers.batch_norm(lasagne.layers.DenseLayer(self.layers[-1], num_units=self.hidden_dim, nonlinearity=lasagne.nonlinearities.leaky_rectify, W=lasagne.init.GlorotUniform(gain=leaky_relu_gain), name=('l_hid_%s' % i)))
l_hid_dr = lasagne.layers.DropoutLayer(l_hid, 0.5)
self.layers.append(l_hid)
self.layers.append(l_hid_dr)
self.l_preout = lasagne.layers.batch_norm(lasagne.layers.DenseLayer(self.layers[-1], num_units=1, nonlinearity=None, name='l_preout'))
self.l_out = lasagne.layers.NonlinearityLayer(self.l_preout, nonlinearity=lasagne.nonlinearities.sigmoid, name='l_out')
self.prediction = lasagne.layers.get_output(self.l_out)
self.loss = lasagne.objectives.binary_crossentropy(self.prediction, self.target_var).mean()
self.accuracy = T.eq(T.ge(self.prediction, 0.5), self.target_var).mean()
self.params = lasagne.layers.get_all_params(self.l_out, trainable=True)
self.updates = lasagne.updates.adam(self.loss, self.params, **update_hyperparams)
print >> sys.stderr, 'Compiling discriminator...'
self.train_fn = theano.function([self.input_var, self.input_var_extra, self.target_var], [self.loss, self.accuracy], updates=self.updates)
self.eval_fn = theano.function([self.input_var, self.accuracy])
def apply(self, inputs, gate_inputs, mask=None):
def step(inputs, states, state_to_gates, state_to_state):
#import ipdb
#ipdb.set_trace()
gate_values = self.gate_activation.apply(
states.dot(self.state_to_gates) + gate_inputs)
update_values = gate_values[:, :self.dim]
reset_values = gate_values[:, self.dim:]
states_reset = states * reset_values
next_states = self.activation.apply(
states_reset.dot(self.state_to_state) + inputs)
next_states = (next_states * update_values +
states * (1 - update_values))
return next_states
def step_mask(inputs, mask_input, state_to_state):
next_states = step(inputs, state_to_state)
if mask_input:
next_states = (mask_input[:, None] * next_states +
(1 - mask_input[:, None]) * states)
return next_states
if mask:
func = step_mask
sequences = [inputs, mask]
else:
func = step
sequences = [inputs, gate_inputs]
#[dict(input=inputs),dict(input=gate_inputs),dict(input=mask)]
output = tensor.repeat(self.params[2].dimshuffle('x',0), inputs.shape[1], axis=0)
states_output, _ = theano.scan(fn=func,
sequences=sequences,
outputs_info=[output],
non_sequences=[self.state_to_gates, self.state_to_state],
strict=True,
#allow_gc=False)
)
return states_output
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。