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), axis=0, keepdims=True)
intersection_w = T.sum(target_w * pred_w, axis=0, keepdims=True)
intersection = T.sum(target * prediction, axis=0, keepdims=True)
prediction_sq = T.sum(T.sqr(prediction), axis=0, keepdims=True)
target_sq = T.sum(T.sqr(target), axis=0, keepdims=True)
loss = (target_w + pred_w - 2 * intersection_w) / (target_sq + prediction_sq - intersection)
return loss
python类reshape()的实例源码
def unpack_params(self, hyper):
t_ind = 0
a = hyper[0];t_ind+=1
b = hyper[1];t_ind+=1
c = hyper[2];t_ind+=1
l_f = hyper[t_ind:t_ind+self.D*self.S];t_ind+=self.D*self.S
l_F = TT.reshape(l_f, (self.D, self.S))
r_f = hyper[t_ind:t_ind+self.M*self.S];t_ind+=self.M*self.S
r_F = TT.reshape(r_f, (self.M, self.S))
F = l_F.dot(r_F.T)
l_p = hyper[t_ind:t_ind+self.S];t_ind+=self.S
l_P = TT.reshape(l_p, (1, self.S))
p = hyper[t_ind:t_ind+self.M];t_ind+=self.M
P = TT.reshape(p, (1, self.M))
l_FC = l_P-TT.mean(l_F, 0)[None, :]
FC = P-TT.mean(F, 0)[None, :]
return a, b, c, l_F, F, l_FC, FC
def forward(self, data, stable_version=False):
"""input has each row as data vector; output also does so"""
count = 1
for bias, weight, pre_w, post_w in zip(self.biases, self.weights, self.pre_w, self.post_w):
size = pre_w[0].shape[0]
zeros_pre_w = T.zeros((size + 4, size + 4))
zeros_post_w = T.zeros((size + 4, size + 4))
pre_w_padding = T.set_subtensor(zeros_pre_w[2: size + 2, 2: size + 2], pre_w[0])
post_w_padding_T = T.set_subtensor(zeros_post_w[2: size + 2, 2: size + 2], post_w[0])
pre, updt = scan(process_pre_post_w, sequences=[pre_w_padding, zeros_pre_w])
post_T, updt = scan(process_pre_post_w, sequences=[post_w_padding_T, zeros_post_w])
pre, post_T = pre[2:size + 2, :], post_T[2:size + 2, :]
ori_shape = data.shape
data = T.reshape(data, (ori_shape[0], pre_w[0].shape[0], pre_w[0].shape[0]))
product, updt = scan(lambda x, A, B: T.dot(T.dot(A, x), B), sequences=data, non_sequences=[pre, post_T.T])
data = T.reshape(product, ori_shape)
if count < self.num_layers - 1:
data = T.nnet.relu(T.dot(data, weight) + bias)
elif not stable_version:
data = T.nnet.softmax(T.dot(data, weight) + bias)
else:
data = log_softmax(T.dot(data, weight) + bias)
count += 1
return data
def _step_batch(self, x_t, mask, h_t_1, w, u, b):
"""
step function of forward in batch version
:param x_t: (batch, in)
:param mask: (batch, )
:param h_t_1: (batch, hidden)
:param w: (hidden, in)
:param u: (hidden, hidden)
:param b: (hidden)
:return: (batch, hidden)
"""
# (batch, in) (in, hidden) -> (batch, hidden)
h_t_1 = T.reshape(h_t_1, (h_t_1.shape[0], 8, 8))
x_t = T.reshape(x_t, (x_t.shape[0], 8, 8))
x_t = x_t / x_t.norm(2, axis=1)[:, None, :]
h_t = self.act.activate(T.dot(x_t, w.T) + T.dot(h_t_1, u.T) + b)
h_t = h_t / h_t.norm(2, axis=1)[:, None, :]
h_t_1 = T.reshape(h_t_1, (h_t_1.shape[0], 64))
h_t = T.reshape(h_t, (h_t.shape[0], 64))
# (batch, hidden) * (batch, None) + (batch, hidden) * (batch, None) -> (batch, hidden)
return h_t * mask[:, None] + h_t_1 * (1 - mask[:, None])
def model_baseline(s_x_, s_pdpo_):
'''very simple logistic regression model'''
global g_mdl, g_dataset
s_bsize = T.shape(s_x_)[0]
idim, odim = reduce(int.__mul__, g_dataset.imsize), len(g_dataset.label_map)
return T.nnet.softmax(
g_mdl.op_dropout(g_mdl.lyr_linear(
'm', T.reshape(s_x_, (s_bsize,idim)),
idim, odim), s_pdpo_))
def lyr_sconv_gen(
name_, s_x_,
idim_, odim_,
**kwargs_):
'''
quick & dirty implementation of fxnn convolution layer
'''
global g_mdl
dilation = kwargs_.get('dilation_')
if dilation is None:
dilation = 1
init_scale = kwargs_.get('init_scale_')
bias = kwargs_.get('bias_')
op_conv = partial(
T.nnet.conv2d,
border_mode='half',
filter_dilation = (dilation, dilation))
ir = 0.5/sqrt(idim_*5+odim_)
s_dims = T.shape(s_x_)
s_x = T.reshape(s_x_, (s_dims[0]*idim_, 1, s_dims[2], s_dims[3]))
s_x1 = T.reshape(op_conv(
s_x, g_sconv_ker,
filter_shape=(2, 1, 1, 3), **kwargs_),
(s_dims[0]*idim_*2, 1, s_dims[2], s_dims[3]))
s_x2 = T.reshape(op_conv(
s_x1, g_sconv_ker.transpose(0,1,3,2),
filter_shape=(2, 1, 3, 1),
), (s_dims[0], idim_*4, s_dims[2], s_dims[3]))
s_y = T.join(1, s_x2, s_x_)
return g_mdl.lyr_conv(
name_, s_y, idim_*5, odim_, fsize_=1, init_scale_=ir, **kwargs_);
def compute_loss(output, num_samples, num_entries=6, gamma=500.0):
"""Compute the loss of a dataset, given the output of the DSSM.
Args:
output (:class:`lasagne.layers.Layer`): the output of the DSSM
num_samples (int): the number of samples in the dataset
num_entries (int): the number of compared papers in the DSSM structure
gamma (float): the coefficient applied in the softmax of the similarities
Returns:
theano.tensor.TensorType: the loss of the dataset
"""
assert (num_entries > 2)
assert (num_samples > 0)
# Post-NN operations to compute the loss
# First, we extract the first output of each bundle
mask = np.zeros(num_entries * num_samples)
mask[::num_entries] = 1
unmask = np.ones(num_entries * num_samples) - mask
cited = T.extra_ops.compress(mask, output, axis=0)
odocs = T.extra_ops.compress(unmask, output, axis=0)
# We duplicate each row 'x' num_entries-1 times
cited = T.extra_ops.repeat(cited, num_entries-1, axis=0)
# Then we compute element-wise product of x with each y, for each bundle
sims = T.sum(cited * odocs, axis=1)
# We reshape the similarities
sims = T.reshape(sims, (num_samples, num_entries-1))
sims = gamma * sims
# We take the softmax of each row
probs = T.nnet.softmax(sims)
# We compute the loss as the sum of element on the first column
loss_mask = np.zeros(num_entries-1)
loss_mask[0] = 1
loss = T.extra_ops.compress(loss_mask, probs, axis=1)
return -T.log(T.prod(loss))
def jacobian_h_x(self, inputs):
h, act_grad = self.act_grads(inputs)
jacobian = self.hidden.W * act_grad.dimshuffle(0, 'x', 1)
return (h, T.reshape(jacobian, newshape=(self.nhid, self.nvis)))
def compute_jacobian_h_x(self, inputs):
inputs = theano.shared(inputs.flatten())
h = self.encode(inputs)
# see later
# h = h.faltten()
# inputs = inputs.flatten()
# inputs = T.reshape(inputs, newshape=(self.nvis))
J = theano.gradient.jacobian(h, inputs)
return h, J
def flatten(W):
"""
Get the flattened version of this weight matrix
:param W:
:return: W with D,O
"""
if W.ndim==4:
W = W.reshape(W.shape[0],-1)
W = W.T
return W
def get_conv_xy_all(layer, deterministic=True):
w_np = layer.W.get_value()
w = layer.W
if layer.flip_filters:
w = w[:, :, ::-1, ::-1]
input_layer = layer.input_layer
if layer.pad == 'same':
input_layer = L.PadLayer(layer.input_layer,
width=np.array(w_np.shape[2:])//2,
batch_ndim=2)
input_shape = L.get_output_shape(input_layer)
output_shape = L.get_output_shape(layer)
max_x = input_shape[2] - w_np.shape[2]+1
max_y = input_shape[3] - w_np.shape[3]+1
#print("input_shape shape: ", input_shape)
#print("output_shape shape: ", output_shape,np.prod(output_shape[2:]))
#print("pad: \"%s\""%layer.pad)
#print(" stride: " ,layer.stride)
#print("max_x %d max_y %d"%(max_x,max_y))
x_orig = L.get_output(input_layer, deterministic=True)
x = theano.tensor.nnet.neighbours.images2neibs(x_orig,
neib_shape=layer.filter_size,
neib_step=layer.stride,
mode='valid')
x = T.reshape(x, (x_orig.shape[0], -1,
np.prod(output_shape[2:]), np.prod(w_np.shape[2:])))
x = T.transpose(x, (0, 2, 1, 3))
x = T.reshape(x, (-1, T.prod(x.shape[2:])))
w = T.flatten(w, outdim=2).T # D,O
y = T.dot(x, w) # N,O
if layer.b is not None:
y += T.shape_padaxis(layer.b, axis=0)
return x, y
def get_output_for(self, inputs, **kwargs):
coefs = inputs[-1]
output = TT.zeros_like(inputs[0])
for i, input_arr in enumerate(inputs[:-1]):
output += input_arr * coefs[:, i].reshape((-1, 1))
return output
def get_output_for(self, all_obs_var, **kwargs):
# n_batch = all_obs_var.shape[:-1]
# out = TT.tile(self.output_var, (n_batch, 1))
# out = TT.tile(self.output_var, TT.concatenate([n_batch, [1]]))
# return out
ndim = all_obs_var.ndim
reshaped_cnt = TT.reshape(self.output_var, (1,) * (ndim - 1) + self.output_var.get_value().shape)
tile_arg = TT.concatenate([all_obs_var.shape[:-1], [1]])
tiled = TT.tile(reshaped_cnt, tile_arg, ndim=ndim)
return tiled
def output(self, x, a):
x = T.reshape(x, (-1, self.n_inputs, self.height, self.width))
return T.tanh(conv2d(x, self.W) + self.b.dimshuffle('x', 0, 'x', 'x'))
def normalize_batch_in_training(x, gamma, beta,
reduction_axes, epsilon=0.0001):
'''Computes mean and std for batch then apply batch_normalization on batch.
'''
dev = theano.config.device
use_cudnn = ndim(x) < 5 and reduction_axes == [0, 2, 3] and (dev.startswith('cuda') or dev.startswith('gpu'))
if use_cudnn:
broadcast_beta = beta.dimshuffle('x', 0, 'x', 'x')
broadcast_gamma = gamma.dimshuffle('x', 0, 'x', 'x')
try:
normed, mean, stdinv = theano.sandbox.cuda.dnn.dnn_batch_normalization_train(
x, broadcast_gamma, broadcast_beta, 'spatial', epsilon)
var = T.inv(stdinv ** 2)
return normed, T.flatten(mean), T.flatten(var)
except AttributeError:
pass
var = x.var(reduction_axes)
mean = x.mean(reduction_axes)
target_shape = []
for axis in range(ndim(x)):
if axis in reduction_axes:
target_shape.append(1)
else:
target_shape.append(x.shape[axis])
target_shape = T.stack(*target_shape)
broadcast_mean = T.reshape(mean, target_shape)
broadcast_var = T.reshape(var, target_shape)
broadcast_beta = T.reshape(beta, target_shape)
broadcast_gamma = T.reshape(gamma, target_shape)
normed = batch_normalization(x, broadcast_mean, broadcast_var,
broadcast_beta, broadcast_gamma,
epsilon)
return normed, mean, var
def reshape(x, shape):
return T.reshape(x, shape)
def batch_flatten(x):
'''Turn a n-D tensor into a 2D tensor where
the first dimension is conserved.
'''
x = T.reshape(x, (x.shape[0], T.prod(x.shape) // x.shape[0]))
return x
def squeeze(x, axis):
'''Remove a 1-dimension from the tensor at index "axis".
'''
shape = list(x.shape)
shape.pop(axis)
return T.reshape(x, tuple(shape))
def sparse_categorical_crossentropy(output, target, from_logits=False):
target = T.cast(T.flatten(target), 'int32')
target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])
target = reshape(target, shape(output))
return categorical_crossentropy(output, target, from_logits)
def get_output_for(self, input, **kwargs):
ndim = input.ndim
reshaped_param = TT.reshape(self.param, (1,) * (ndim - 1) + (self.num_units,))
tile_arg = TT.concatenate([input.shape[:-1], [1]])
tiled = TT.tile(reshaped_param, tile_arg, ndim=ndim)
return tiled