我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.flatten()。
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), 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(.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, axis=1).dimshuffle(0, 'x') dxkxy = T.add(dxkxy, T.mul(X, sumkxy)) / (h ** 2) return kxy, dxkxy
def dice_loss(y_pred, y_true, void_class, class_for_dice=1): ''' Dice loss -- works for only binary classes. y_pred is a softmax output y_true is one hot ''' smooth = 1 y_true_f = T.flatten(y_true[:, class_for_dice, :, :]) y_true_f = T.cast(y_true_f, 'int32') y_pred_f = T.flatten(y_pred[:, class_for_dice, :, :]) # remove void classes from dice if len(void_class): for i in range(len(void_class)): # get idx of non void classes and remove void classes # from y_true and y_pred idxs = T.neq(y_true_f, void_class[i]).nonzero() y_pred_f = y_pred_f[idxs] y_true_f = y_true_f[idxs] intersection = T.sum(y_true_f * y_pred_f) return -(2.*intersection+smooth) / (T.sum(y_true_f)+T.sum(y_pred_f)+smooth)
def dist_info_sym(self, obs_var, latent_var=None): # this is ment to be for one path! # now this is not doing anything! And for computing the dist_info_vars of npo_snn_rewardMI it doesn't work if latent_var is None: latent_var1 = theano.shared(np.expand_dims(self.latent_fix, axis=0)) # new fix to avoid putting the latent as an input: just take the one fixed! latent_var = TT.tile(latent_var1, [obs_var.shape[0], 1]) # generate the generalized input (append latents to obs.) if self.bilinear_integration: extended_obs_var = TT.concatenate([obs_var, latent_var, TT.flatten(obs_var[:, :, np.newaxis] * latent_var[:, np.newaxis, :], outdim=2)] , axis=1) else: extended_obs_var = TT.concatenate([obs_var, latent_var], axis=1) mean_var, log_std_var = L.get_output([self._l_mean, self._l_log_std], extended_obs_var) if self.min_std is not None: log_std_var = TT.maximum(log_std_var, np.log(self.min_std)) return dict(mean=mean_var, log_std=log_std_var)
def update_opt(self, f, target, inputs, reg_coeff): self.target = target self.reg_coeff = reg_coeff params = target.get_params(trainable=True) constraint_grads = theano.grad( f, wrt=params, disconnected_inputs='warn') xs = tuple([ext.new_tensor_like("%s x" % p.name, p) for p in params]) def Hx_plain(): Hx_plain_splits = TT.grad( TT.sum([TT.sum(g * x) for g, x in zip(constraint_grads, xs)]), wrt=params, disconnected_inputs='warn' ) return TT.concatenate([TT.flatten(s) for s in Hx_plain_splits]) self.opt_fun = ext.lazydict( f_Hx_plain=lambda: ext.compile_function( inputs=inputs + xs, outputs=Hx_plain(), log_name="f_Hx_plain", ), )
def discrim(X): current_input = dropout(X, 0.3) ### encoder ### cv1 = relu(dnn_conv(current_input, aew1, subsample=(1,1), border_mode=(1,1))) cv2 = relu(batchnorm(dnn_conv(cv1, aew2, subsample=(4,4), border_mode=(2,2)), g=aeg2, b=aeb2)) cv3 = relu(batchnorm(dnn_conv(cv2, aew3, subsample=(1,1), border_mode=(1,1)), g=aeg3, b=aeb3)) cv4 = relu(batchnorm(dnn_conv(cv3, aew4, subsample=(4,4), border_mode=(2,2)), g=aeg4, b=aeb4)) cv5 = relu(batchnorm(dnn_conv(cv4, aew5, subsample=(1,1), border_mode=(1,1)), g=aeg5, b=aeb5)) cv6 = relu(batchnorm(dnn_conv(cv5, aew6, subsample=(4,4), border_mode=(0,0)), g=aeg6, b=aeb6)) ### decoder ### dv6 = relu(batchnorm(deconv(cv6, aew6, subsample=(4,4), border_mode=(0,0)), g=aeg6t, b=aeb6t)) dv5 = relu(batchnorm(deconv(dv6, aew5, subsample=(1,1), border_mode=(1,1)), g=aeg5t, b=aeb5t)) dv4 = relu(batchnorm(deconv(dv5, aew4, subsample=(4,4), border_mode=(2,2)), g=aeg4t, b=aeb4t)) dv3 = relu(batchnorm(deconv(dv4, aew3, subsample=(1,1), border_mode=(1,1)), g=aeg3t, b=aeb3t)) dv2 = relu(batchnorm(deconv(dv3, aew2, subsample=(4,4), border_mode=(2,2)), g=aeg2t, b=aeb2t)) dv1 = tanh(deconv(dv2, aew1, subsample=(1,1), border_mode=(1,1))) rX = dv1 mse = T.sqrt(T.sum(T.abs_(T.flatten(X-rX, 2)),axis=1)) + T.sqrt(T.sum(T.flatten((X-rX)**2, 2), axis=1)) return T.flatten(cv6, 2), rX, mse
def svgd_gradient(X0): hidden, _, mse = discrim(X0) grad = -1.0 * T.grad( mse.sum(), X0) kxy, neighbors, h = rbf_kernel(hidden) #TODO coff = T.exp( - T.sum((hidden[neighbors] - hidden)**2, axis=1) / h**2 / 2.0 ) v = coff.dimshuffle(0, 'x') * (-hidden[neighbors] + hidden) / h**2 X1 = X0[neighbors] hidden1, _, _ = discrim(X1) dxkxy = T.Lop(hidden1, X1, v) #svgd_grad = (T.dot(kxy, T.flatten(grad, 2)).reshape(dxkxy.shape) + dxkxy) / T.sum(kxy, axis=1).dimshuffle(0, 'x', 'x', 'x') svgd_grad = grad + dxkxy / 2. return grad, svgd_grad, dxkxy
def discrim(X): current_input = dropout(X, 0.3) ### encoder ### cv1 = relu(dnn_conv(current_input, aew1, subsample=(1,1), border_mode=(1,1))) cv2 = relu(batchnorm(dnn_conv(cv1, aew2, subsample=(4,4), border_mode=(2,2)), g=aeg2, b=aeb2)) cv3 = relu(batchnorm(dnn_conv(cv2, aew3, subsample=(1,1), border_mode=(1,1)), g=aeg3, b=aeb3)) cv4 = relu(batchnorm(dnn_conv(cv3, aew4, subsample=(4,4), border_mode=(2,2)), g=aeg4, b=aeb4)) cv5 = relu(batchnorm(dnn_conv(cv4, aew5, subsample=(1,1), border_mode=(1,1)), g=aeg5, b=aeb5)) cv6 = relu(batchnorm(dnn_conv(cv5, aew6, subsample=(4,4), border_mode=(0,0)), g=aeg6, b=aeb6)) ### decoder ### dv6 = relu(batchnorm(deconv(cv6, aew6, subsample=(4,4), border_mode=(0,0)), g=aeg6t, b=aeb6t)) dv5 = relu(batchnorm(deconv(dv6, aew5, subsample=(1,1), border_mode=(1,1)), g=aeg5t, b=aeb5t)) dv4 = relu(batchnorm(deconv(dv5, aew4, subsample=(4,4), border_mode=(2,2)), g=aeg4t, b=aeb4t)) dv3 = relu(batchnorm(deconv(dv4, aew3, subsample=(1,1), border_mode=(1,1)), g=aeg3t, b=aeb3t)) dv2 = relu(batchnorm(deconv(dv3, aew2, subsample=(4,4), border_mode=(2,2)), g=aeg2t, b=aeb2t)) dv1 = tanh(deconv(dv2, aew1, subsample=(1,1), border_mode=(1,1))) rX = dv1 mse = T.sqrt(T.sum(T.abs_(T.flatten(X-rX, 2)),axis=1)) + T.sqrt(T.sum(T.flatten((X-rX)**2, 2), axis=1)) # L1 and L2 loss return T.flatten(cv6, 2), rX, mse
def categorical_crossentropy(logit, y, mask, length_var, need_softmax=False): logit_shp = logit.shape # (n_samples, n_timesteps_f, n_labels) # softmax, predict label prob # (n_samples * n_timesteps_f, n_labels) if need_softmax: probs = T.nnet.softmax(logit.reshape([logit_shp[0]*logit_shp[1], logit_shp[2]])) else: probs = logit.reshape([logit_shp[0]*logit_shp[1], logit_shp[2]]) # (n_samples * n_timesteps_f) y_flat = y.flatten() # clip to avoid nan loss probs = T.clip(probs, _EPSILON, 1.0 - _EPSILON) loss = lasagne.objectives.categorical_crossentropy(probs, y_flat) # (n_samples, n_timesteps_f) loss = loss.reshape((logit_shp[0], logit_shp[1])) loss = loss * mask loss = T.sum(loss, axis=1) / length_var probs = probs.reshape(logit_shp) return loss, probs
def binary_crossentropy(logit, y, mask, length_var): logit_shp = logit.shape # logit_shp[2] == 1 # n_labels is 1 # (n_samples, n_timesteps_f, n_labels) # softmax, predict label prob # (n_samples * n_timesteps_f, n_labels) probs = T.nnet.sigmoid(logit.flatten()) # (n_samples * n_timesteps_f) y_flat = y.flatten() loss = lasagne.objectives.binary_crossentropy(probs, y_flat) # (n_samples, n_timesteps_f) loss = loss.reshape((logit_shp[0], logit_shp[1])) loss = loss * mask loss = T.sum(loss, axis=1) / length_var # (n_samples, n_timesteps_f) probs = probs.reshape([logit_shp[0], logit_shp[1]]) return loss, probs
def get_output_for(self, inputs, **kwargs): input = inputs[0] input_word = T.flatten(inputs[1]) word_dropout = inputs[2] # Apply word embedding sentence_rep = self.SemMem.get_output_for([input, word_dropout]) # Apply GRU Layer gru_outs = self.GRU.get_output_for([sentence_rep]) # Extract candidate fact from GRU's output by input_word variable # resolving input with adtional word # e.g. John when to the hallway nil nil nil -> [GRU1, ... ,GRU8] -> GRU5 candidate_facts = T.reshape( gru_outs[T.arange(gru_outs.shape[0],dtype='int32'), input_word-1], (-1, input.shape[1], self.hid_state_size)) return candidate_facts
def get_parent_state(self, children_states, node_type, use_dropout: bool, iteration_number) -> tuple: layer_input = T.flatten(children_states) nn_out = self.__compute_layer_output(layer_input, node_type, use_dropout, iteration_number) encoder_input = T.flatten(T.concatenate((children_states, nn_out))) * self.__ae_noise encoding = T.tanh(T.dot(encoder_input, self.__encoder_weights[node_type])) decoded = T.tanh(T.dot(encoding, self.__decoder_weights)) decoded /= decoded.norm(2) / layer_input.norm(2) output_reconstruction = self.__compute_layer_output(decoded, node_type, use_dropout, iteration_number) reconstruction_cos = T.dot(nn_out[0], output_reconstruction[0]) children_reconstruction_cos = T.dot(decoded, layer_input) additional_objective = reconstruction_cos + children_reconstruction_cos constrain_usage_pct = T.cast(1. - T.pow(self.__hyperparameters['constrain_intro_rate'], iteration_number), theano.config.floatX) return nn_out[0], constrain_usage_pct * additional_objective
def cnn_model(X, w, w2, w3, w4, w_o, p_drop_conv, p_drop_hidden): l1a = rectify(T.nnet.conv2d(X, w, border_mode='full')) l1 = pool.pool_2d(l1a, (2, 2)) l1 = dropout(l1, p_drop_conv) l2a = rectify(T.nnet.conv2d(l1, w2)) l2 = pool.pool_2d(l2a, (2, 2)) l2 = dropout(l2, p_drop_conv) l3a = rectify(T.nnet.conv2d(l2, w3)) l3b = pool.pool_2d(l3a, (2, 2)) l3 = T.flatten(l3b, outdim=2) l3 = dropout(l3, p_drop_conv) l4 = rectify(T.dot(l3, w4)) l4 = dropout(l4, p_drop_hidden) pyx = softmax(T.dot(l4, w_o)) return l1, l2, l3, l4, pyx
def discrim(X, w1, g1, b1, w2, g2, b2, w3, g3, b3, w4, g4, b4, wy): filter_shape = (Channel[1] , Channel[0], kernal[0], kernal[0], kernal[0]) Dl1 = lrelu(batchnorm(conv(X,w1,filter_shape = filter_shape),g = g1, b = b1)) filter_shape = (Channel[2] , Channel[1], kernal[1], kernal[1], kernal[1]) Dl2 = lrelu(batchnorm(conv(Dl1, w2,filter_shape = filter_shape), g = g2, b= b2)) filter_shape = (Channel[3] , Channel[2], kernal[2], kernal[2], kernal[2]) Dl3 = lrelu(batchnorm(conv(Dl2,w3,filter_shape = filter_shape), g = g3, b= b3)) filter_shape = (Channel[4] , Channel[3], kernal[3], kernal[3], kernal[3]) Dl4 = lrelu(batchnorm(conv(Dl3,w4,filter_shape = filter_shape), g = g4, b = b4)) Dl4 = T.flatten(Dl4,2) DlY = sigmoid(T.dot(Dl4,wy)) return DlY
def encoder(X, w1, g1, b1, w2, g2, b2, w3, g3, b3, w4, g4, b4, wz): filter_shape = (Channel[1] , Channel[0], kernal[0], kernal[0], kernal[0]) Dl1 = lrelu(batchnorm(conv(X,w1,filter_shape = filter_shape),g = g1, b = b1)) filter_shape = (Channel[2] , Channel[1], kernal[1], kernal[1], kernal[1]) Dl2 = lrelu(batchnorm(conv(Dl1, w2,filter_shape = filter_shape), g = g2, b= b2)) filter_shape = (Channel[3] , Channel[2], kernal[2], kernal[2], kernal[2]) Dl3 = lrelu(batchnorm(conv(Dl2,w3,filter_shape = filter_shape), g = g3, b= b3)) filter_shape = (Channel[4] , Channel[3], kernal[3], kernal[3], kernal[3]) Dl4 = lrelu(batchnorm(conv(Dl3,w4,filter_shape = filter_shape), g = g4, b = b4)) Dl4 = T.flatten(Dl4,2) DlZ = sigmoid(T.dot(Dl4,wz)) return DlZ # def gen_Z(dist): # mu = dist[:Nz] # sigma = dist[Nz:]
def get_output(self, h, nout=None, stddev=None, reparameterize=reparam, exp_reparam=exp_reparam): h, h_shape, h_max = h.value, h.shape, h.index_max nin = np.prod(h_shape[1:], dtype=np.int) if (h_max is None) else h_max out_shape_specified = isinstance(nout, tuple) if out_shape_specified: out_shape = nout else: assert isinstance(nout, int) out_shape = nout, nout = np.prod(out_shape) nin_axis = [0] W = self.weights((nin, nout), stddev=stddev, reparameterize=reparameterize, nin_axis=nin_axis, exp_reparam=exp_reparam) if h_max is None: if h.ndim > 2: h = T.flatten(h, 2) out = T.dot(h, W) else: assert nin >= 1, 'FC: h.index_max must be >= 1; was: %s' % (nin,) assert h.ndim == 1 out = W[h] return Output(out)
def _L(x): # initialize with zeros batch_size = x.shape[0] a = T.zeros((batch_size, num_actuators, num_actuators)) # set diagonal elements batch_idx = T.extra_ops.repeat(T.arange(batch_size), num_actuators) diag_idx = T.tile(T.arange(num_actuators), batch_size) b = T.set_subtensor(a[batch_idx, diag_idx, diag_idx], T.flatten(T.exp(x[:, :num_actuators]))) # set lower triangle cols = np.concatenate([np.array(range(i), dtype=np.uint) for i in xrange(num_actuators)]) rows = np.concatenate([np.array([i]*i, dtype=np.uint) for i in xrange(num_actuators)]) cols_idx = T.tile(T.as_tensor_variable(cols), batch_size) rows_idx = T.tile(T.as_tensor_variable(rows), batch_size) batch_idx = T.extra_ops.repeat(T.arange(batch_size), len(cols)) c = T.set_subtensor(b[batch_idx, rows_idx, cols_idx], T.flatten(x[:, num_actuators:])) return c
def build_objective(model, deterministic=False, epsilon=1e-12): predictions = T.flatten(nn.layers.get_output(model.l_out)) targets = T.flatten(nn.layers.get_output(model.l_target)) dice = (2. * T.sum(targets * predictions) + epsilon) / (T.sum(predictions) + T.sum(targets) + epsilon) return -1. * dice
def build_objective(model, deterministic=False, epsilon=1e-12): predictions = T.flatten(nn.layers.get_output(model.l_out)) targets = T.flatten(nn.layers.get_output(model.l_target)) targets = T.clip(targets, 1e-6, 1.) dice = (2. * T.sum(targets * predictions) + epsilon) / (T.sum(predictions) + T.sum(targets) + epsilon) return -1. * dice
def build_objective(model, deterministic=False, epsilon=1e-12): p = nn.layers.get_output(model.l_out, deterministic=deterministic) targets = T.flatten(nn.layers.get_output(model.l_target)) p = T.clip(p, epsilon, 1.-epsilon) bce = T.nnet.binary_crossentropy(p, targets) return T.mean(bce)