Python numpy.log() Examples
The following are 30
code examples of numpy.log().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ mx.metric.check_label_shapes(labels, preds) for label, pred in zip(labels, preds): label = label.asnumpy() pred = pred.asnumpy() pred = np.column_stack((1 - pred, pred)) label = label.ravel() num_examples = pred.shape[0] assert label.shape[0] == num_examples, (label.shape[0], num_examples) prob = pred[np.arange(num_examples, dtype=np.int64), np.int64(label)] self.sum_metric += (-np.log(prob + self.eps)).sum() self.num_inst += num_examples
Example #2
Source File: common.py From numpynet with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, choice="sigmoid"): """ :param choice: Which activation function you want, must be in self.available """ if choice not in self.available: msg = "Choice of activation (" + choice + ") not available!" log.out.error(msg) raise ValueError(msg) elif choice == "tanh": self.function = self._tanh elif choice == "tanhpos": self.function = self._tanhpos elif choice == "sigmoid": self.function = self._sigmoid elif choice == "softplus": self.function = self._softplus elif choice == "relu": self.function = self._relu elif choice == "leakyrelu": self.function = self._leakyrelu
Example #3
Source File: dataset.py From Deep_VoiceChanger with MIT License | 6 votes |
def wave2input_image(wave, window, pos=0, pad=0): wave_image = np.hstack([wave[pos+i*sride:pos+(i+pad*2)*sride+dif].reshape(height+pad*2, sride) for i in range(256//sride)])[:,:254] wave_image *= window spectrum_image = np.fft.fft(wave_image, axis=1) input_image = np.abs(spectrum_image[:,:128].reshape(1, height+pad*2, 128), dtype=np.float32) np.clip(input_image, 1000, None, out=input_image) np.log(input_image, out=input_image) input_image += bias input_image /= scale if np.max(input_image) > 0.95: print('input image max bigger than 0.95', np.max(input_image)) if np.min(input_image) < 0.05: print('input image min smaller than 0.05', np.min(input_image)) return input_image
Example #4
Source File: gaussian_moments.py From DOTA_models with Apache License 2.0 | 6 votes |
def _compute_eps(log_moments, delta): """Compute epsilon for given log_moments and delta. Args: log_moments: the log moments of privacy loss, in the form of pairs of (moment_order, log_moment) delta: the target delta. Returns: epsilon """ min_eps = float("inf") for moment_order, log_moment in log_moments: if moment_order == 0: continue if math.isinf(log_moment) or math.isnan(log_moment): sys.stderr.write("The %d-th order is inf or Nan\n" % moment_order) continue min_eps = min(min_eps, (log_moment - math.log(delta)) / moment_order) return min_eps
Example #5
Source File: test_loss.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_bce_loss(): N = 20 data = mx.random.uniform(-1, 1, shape=(N, 20)) label = mx.nd.array(np.random.randint(2, size=(N,)), dtype='float32') data_iter = mx.io.NDArrayIter(data, label, batch_size=10, label_name='label') output = get_net(1) l = mx.symbol.Variable('label') Loss = gluon.loss.SigmoidBinaryCrossEntropyLoss() loss = Loss(output, l) loss = mx.sym.make_loss(loss) mod = mx.mod.Module(loss, data_names=('data',), label_names=('label',)) mod.fit(data_iter, num_epoch=200, optimizer_params={'learning_rate': 0.01}, eval_metric=mx.metric.Loss(), optimizer='adam', initializer=mx.init.Xavier(magnitude=2)) assert mod.score(data_iter, eval_metric=mx.metric.Loss())[0][1] < 0.01 # Test against npy data = mx.random.uniform(-5, 5, shape=(10,)) label = mx.random.uniform(0, 1, shape=(10,)) mx_bce_loss = Loss(data, label).asnumpy() prob_npy = 1.0 / (1.0 + np.exp(-data.asnumpy())) label_npy = label.asnumpy() npy_bce_loss = - label_npy * np.log(prob_npy) - (1 - label_npy) * np.log(1 - prob_npy) assert_almost_equal(mx_bce_loss, npy_bce_loss, rtol=1e-4, atol=1e-5)
Example #6
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ labels, preds = check_label_shapes(labels, preds, True) for label, pred in zip(labels, preds): label = label.asnumpy() pred = pred.asnumpy() label = label.ravel() assert label.shape[0] == pred.shape[0] prob = pred[numpy.arange(label.shape[0]), numpy.int64(label)] self.sum_metric += (-numpy.log(prob + self.eps)).sum() self.num_inst += label.shape[0]
Example #7
Source File: 5_nueral_network.py From deep-learning-note with MIT License | 6 votes |
def cost0(params, input_size, hidden_size, num_labels, X, y, learning_rate): m = X.shape[0] X = np.matrix(X) y = np.matrix(y) # reshape the parameter array into parameter matrices for each layer theta1 = np.matrix(np.reshape(params[:hidden_size * (input_size + 1)], (hidden_size, (input_size + 1)))) theta2 = np.matrix(np.reshape(params[hidden_size * (input_size + 1):], (num_labels, (hidden_size + 1)))) # run the feed-forward pass a1, z2, a2, z3, h = forward_propagate(X, theta1, theta2) # compute the cost J = 0 for i in range(m): first_term = np.multiply(-y[i,:], np.log(h[i,:])) second_term = np.multiply((1 - y[i,:]), np.log(1 - h[i,:])) J += np.sum(first_term - second_term) J = J / m return J
Example #8
Source File: 5_nueral_network.py From deep-learning-note with MIT License | 6 votes |
def cost(params, input_size, hidden_size, num_labels, X, y, learning_rate): m = X.shape[0] X = np.matrix(X) y = np.matrix(y) # reshape the parameter array into parameter matrices for each layer theta1 = np.matrix(np.reshape(params[:hidden_size * (input_size + 1)], (hidden_size, (input_size + 1)))) theta2 = np.matrix(np.reshape(params[hidden_size * (input_size + 1):], (num_labels, (hidden_size + 1)))) # run the feed-forward pass a1, z2, a2, z3, h = forward_propagate(X, theta1, theta2) # compute the cost J = 0 for i in range(m): first_term = np.multiply(-y[i,:], np.log(h[i,:])) second_term = np.multiply((1 - y[i,:]), np.log(1 - h[i,:])) J += np.sum(first_term - second_term) J = J / m # add the cost regularization term J += (float(learning_rate) / (2 * m)) * (np.sum(np.power(theta1[:,1:], 2)) + np.sum(np.power(theta2[:,1:], 2))) return J
Example #9
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def update(self, labels, preds): pred = preds[self.pred.index('rpn_cls_prob')] label = labels[self.label.index('rpn_label')] # label (b, p) label = label.asnumpy().astype('int32').reshape((-1)) # pred (b, c, p) or (b, c, h, w) --> (b, p, c) --> (b*p, c) pred = pred.asnumpy().reshape((pred.shape[0], pred.shape[1], -1)).transpose((0, 2, 1)) pred = pred.reshape((label.shape[0], -1)) # filter with keep_inds keep_inds = np.where(label != -1)[0] label = label[keep_inds] cls = pred[keep_inds, label] cls += 1e-14 cls_loss = -1 * np.log(cls) cls_loss = np.sum(cls_loss) self.sum_metric += cls_loss self.num_inst += label.shape[0]
Example #10
Source File: utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def logging_config(name=None, level=logging.DEBUG, console_level=logging.DEBUG): if name is None: name = inspect.stack()[1][1].split('.')[0] folder = os.path.join(os.getcwd(), name) if not os.path.exists(folder): os.makedirs(folder) logpath = os.path.join(folder, name + ".log") print("All Logs will be saved to %s" %logpath) logging.root.setLevel(level) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') logfile = logging.FileHandler(logpath) logfile.setLevel(level) logfile.setFormatter(formatter) logging.root.addHandler(logfile) #TODO Update logging patterns in other files logconsole = logging.StreamHandler() logconsole.setLevel(console_level) logconsole.setFormatter(formatter) logging.root.addHandler(logconsole) return folder
Example #11
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
def apply_cmap(zs, cmap, vmin=None, vmax=None, unit=None, logrescale=False): ''' apply_cmap(z, cmap) applies the given cmap to the values in z; if vmin and/or vmax are passed, they are used to scale z. Note that this function can automatically rescale data into log-space if the colormap is a neuropythy log-space colormap such as log_eccentricity. To enable this behaviour use the optional argument logrescale=True. ''' zs = pimms.mag(zs) if unit is None else pimms.mag(zs, unit) zs = np.asarray(zs, dtype='float') if pimms.is_str(cmap): cmap = matplotlib.cm.get_cmap(cmap) if logrescale: if vmin is None: vmin = np.log(np.nanmin(zs)) if vmax is None: vmax = np.log(np.nanmax(zs)) mn = np.exp(vmin) u = zdivide(nanlog(zs + mn) - vmin, vmax - vmin, null=np.nan) else: if vmin is None: vmin = np.nanmin(zs) if vmax is None: vmax = np.nanmax(zs) u = zdivide(zs - vmin, vmax - vmin, null=np.nan) u[np.isnan(u)] = -np.inf return cmap(u)
Example #12
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def update(self, labels, preds): """ Implementation of updating metrics """ # get generated multi label from network cls_prob = preds[0].asnumpy() loc_loss = preds[1].asnumpy() cls_label = preds[2].asnumpy() valid_count = np.sum(cls_label >= 0) # overall accuracy & object accuracy label = cls_label.flatten() mask = np.where(label >= 0)[0] indices = np.int64(label[mask]) prob = cls_prob.transpose((0, 2, 1)).reshape((-1, cls_prob.shape[1])) prob = prob[mask, indices] self.sum_metric[0] += (-np.log(prob + self.eps)).sum() self.num_inst[0] += valid_count # smoothl1loss self.sum_metric[1] += np.sum(loc_loss) self.num_inst[1] += valid_count
Example #13
Source File: rnn_cell_demo.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def Perplexity(label, pred): """ Calculates prediction perplexity Args: label (mx.nd.array): labels array pred (mx.nd.array): prediction array Returns: float: calculated perplexity """ # collapse the time, batch dimension label = label.reshape((-1,)) pred = pred.reshape((-1, pred.shape[-1])) loss = 0. for i in range(pred.shape[0]): loss += -np.log(max(1e-10, pred[i][int(label[i])])) return np.exp(loss / label.size)
Example #14
Source File: retinotopy.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
def to_logeccen(ecc, vmin=0, vmax=90, offset=0.75): ''' to_logeccen(ecc) yields a rescaled log-space version of the eccentricity value (or values) ecc, which are extracted in degrees. to_logeccen(xy_matrix) rescales all the (x,y) points in the given matrix to have lox-spaced eccentricity values. to_logeccen is the inverse of from_logeccen. ''' if pimms.is_matrix(ecc): xy = np.asarray(pimms.mag(ecc, 'deg')) trq = xy.shape[0] != 2 xy = np.transpose(xy) if trq else np.asarray(xy) ecc = np.sqrt(np.sum(xy**2, axis=0)) esc = to_logeccen(ecc, vmin=vmin, vmax=vmax, offset=offset) ecc = zinv(ecc) xy = xy * [ecc,ecc] * [esc,esc] return xy.T if trq else xy else: (ecc,vmin,vmax,offset) = [np.asarray(pimms.mag(u, 'deg')) for u in (ecc,vmin,vmax,offset)] log_ecc = np.log(ecc + offset) (vmin, vmax) = [np.log(u + offset) for u in (vmin, vmax)] return (log_ecc - vmin) / (vmax - vmin)
Example #15
Source File: test_metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_perplexity(): pred = mx.nd.array([[0.8, 0.2], [0.2, 0.8], [0, 1.]]) label = mx.nd.array([0, 1, 1]) p = pred.asnumpy()[np.arange(label.size), label.asnumpy().astype('int32')] perplexity_expected = np.exp(-np.log(p).sum()/label.size) metric = mx.metric.create('perplexity', -1) metric.update([label], [pred]) _, perplexity = metric.get() assert perplexity == perplexity_expected
Example #16
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ assert len(labels) == len(preds) loss = 0. num = 0 for label, pred in zip(labels, preds): assert label.size == pred.size/pred.shape[-1], \ "shape mismatch: %s vs. %s"%(label.shape, pred.shape) label = label.as_in_context(pred.context).reshape((label.size,)) pred = ndarray.pick(pred, label.astype(dtype='int32'), axis=self.axis) if self.ignore_label is not None: ignore = (label == self.ignore_label).astype(pred.dtype) num -= ndarray.sum(ignore).asscalar() pred = pred*(1-ignore) + ignore loss -= ndarray.sum(ndarray.log(ndarray.maximum(1e-10, pred))).asscalar() num += pred.size self.sum_metric += loss self.num_inst += num
Example #17
Source File: real_nvp_utils.py From DOTA_models with Apache License 2.0 | 5 votes |
def standard_normal_ll(input_): """Log-likelihood of standard Gaussian distribution.""" res = -.5 * (tf.square(input_) + numpy.log(2. * numpy.pi)) return res
Example #18
Source File: test_metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_nll_loss(): metric = mx.metric.create('nll_loss') pred = mx.nd.array([[0.2, 0.3, 0.5], [0.6, 0.1, 0.3]]) label = mx.nd.array([2, 1]) metric.update([label], [pred]) _, loss = metric.get() expected_loss = -(np.log(pred[0][2].asscalar()) + np.log(pred[1][1].asscalar())) / 2 assert loss == expected_loss
Example #19
Source File: dec.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def setup(self, X, num_centers, alpha, save_to='dec_model'): sep = X.shape[0]*9//10 X_train = X[:sep] X_val = X[sep:] ae_model = AutoEncoderModel(self.xpu, [X.shape[1],500,500,2000,10], pt_dropout=0.2) if not os.path.exists(save_to+'_pt.arg'): ae_model.layerwise_pretrain(X_train, 256, 50000, 'sgd', l_rate=0.1, decay=0.0, lr_scheduler=mx.misc.FactorScheduler(20000,0.1)) ae_model.finetune(X_train, 256, 100000, 'sgd', l_rate=0.1, decay=0.0, lr_scheduler=mx.misc.FactorScheduler(20000,0.1)) ae_model.save(save_to+'_pt.arg') logging.log(logging.INFO, "Autoencoder Training error: %f"%ae_model.eval(X_train)) logging.log(logging.INFO, "Autoencoder Validation error: %f"%ae_model.eval(X_val)) else: ae_model.load(save_to+'_pt.arg') self.ae_model = ae_model self.dec_op = DECModel.DECLoss(num_centers, alpha) label = mx.sym.Variable('label') self.feature = self.ae_model.encoder self.loss = self.dec_op(data=self.ae_model.encoder, label=label, name='dec') self.args.update({k:v for k,v in self.ae_model.args.items() if k in self.ae_model.encoder.list_arguments()}) self.args['dec_mu'] = mx.nd.empty((num_centers, self.ae_model.dims[-1]), ctx=self.xpu) self.args_grad.update({k: mx.nd.empty(v.shape, ctx=self.xpu) for k,v in self.args.items()}) self.args_mult.update({k: k.endswith('bias') and 2.0 or 1.0 for k in self.args}) self.num_centers = num_centers
Example #20
Source File: gaussian_moments.py From DOTA_models with Apache License 2.0 | 5 votes |
def compute_b(sigma, q, lmbd, verbose=False): mu0, _, mu = distributions(sigma, q) b_lambda_fn = lambda z: mu0(z) * np.power(cropped_ratio(mu0(z), mu(z)), lmbd) b_lambda = integral_inf(b_lambda_fn) m = sigma ** 2 * (np.log((2. - q) / (1. - q)) + 1. / (2 * sigma ** 2)) b_fn = lambda z: (np.power(mu0(z) / mu(z), lmbd) - np.power(mu(-z) / mu0(z), lmbd)) if verbose: print "M =", m print "f(-M) = {} f(M) = {}".format(b_fn(-m), b_fn(m)) assert b_fn(-m) < 0 and b_fn(m) < 0 b_lambda_int1_fn = lambda z: (mu0(z) * np.power(cropped_ratio(mu0(z), mu(z)), lmbd)) b_lambda_int2_fn = lambda z: (mu0(z) * np.power(cropped_ratio(mu(z), mu0(z)), lmbd)) b_int1 = integral_bounded(b_lambda_int1_fn, -m, m) b_int2 = integral_bounded(b_lambda_int2_fn, -m, m) a_lambda_m1 = compute_a(sigma, q, lmbd - 1) b_bound = a_lambda_m1 + b_int1 - b_int2 if verbose: print "B: by numerical integration", b_lambda print "B must be no more than ", b_bound print b_lambda, b_bound return _to_np_float64(b_lambda) ########################### # MULTIPRECISION ROUTINES # ###########################
Example #21
Source File: stt_metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def ctc_loss(label, prob, remainder, seq_length, batch_size, num_gpu=1, big_num=1e10): label_ = [0, 0] prob[prob < 1 / big_num] = 1 / big_num log_prob = np.log(prob) l = len(label) for i in range(l): label_.append(int(label[i])) label_.append(0) l_ = 2 * l + 1 a = np.full((seq_length, l_ + 1), -big_num) a[0][1] = log_prob[remainder][0] a[0][2] = log_prob[remainder][label_[2]] for i in range(1, seq_length): row = i * int(batch_size / num_gpu) + remainder a[i][1] = a[i - 1][1] + log_prob[row][0] a[i][2] = np.logaddexp(a[i - 1][2], a[i - 1][1]) + log_prob[row][label_[2]] for j in range(3, l_ + 1): a[i][j] = np.logaddexp(a[i - 1][j], a[i - 1][j - 1]) if label_[j] != 0 and label_[j] != label_[j - 2]: a[i][j] = np.logaddexp(a[i][j], a[i - 1][j - 2]) a[i][j] += log_prob[row][label_[j]] return -np.logaddexp(a[seq_length - 1][l_], a[seq_length - 1][l_ - 1]) # label is done with remove_blank # pred is got from pred_best
Example #22
Source File: stt_metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def update(self, labels, preds): check_label_shapes(labels, preds) if self.is_logging: log = LogUtil().getlogger() labelUtil = LabelUtil.getInstance() self.batch_loss = 0. for label, pred in zip(labels, preds): label = label.asnumpy() pred = pred.asnumpy() seq_length = len(pred) / int(int(self.batch_size) / int(self.num_gpu)) for i in range(int(int(self.batch_size) / int(self.num_gpu))): l = remove_blank(label[i]) p = [] for k in range(int(seq_length)): p.append(np.argmax(pred[k * int(int(self.batch_size) / int(self.num_gpu)) + i])) p = pred_best(p) l_distance = levenshtein_distance(l, p) self.total_n_label += len(l) self.total_l_dist += l_distance this_cer = float(l_distance) / float(len(l)) if self.is_logging: log.info("label: %s " % (labelUtil.convert_num_to_word(l))) log.info("pred : %s , cer: %f (distance: %d/ label length: %d)" % ( labelUtil.convert_num_to_word(p), this_cer, l_distance, len(l))) self.num_inst += 1 self.sum_metric += this_cer if self.is_epoch_end: loss = ctc_loss(l, pred, i, int(seq_length), int(self.batch_size), int(self.num_gpu)) self.batch_loss += loss if self.is_logging: log.info("loss: %f " % loss) self.total_ctc_loss += self.batch_loss
Example #23
Source File: gaussian_moments.py From DOTA_models with Apache License 2.0 | 5 votes |
def compute_b_mp(sigma, q, lmbd, verbose=False): lmbd_int = int(math.ceil(lmbd)) if lmbd_int == 0: return 1.0 mu0, _, mu = distributions_mp(sigma, q) b_lambda_fn = lambda z: mu0(z) * (mu0(z) / mu(z)) ** lmbd_int b_lambda = integral_inf_mp(b_lambda_fn) m = sigma ** 2 * (mp.log((2 - q) / (1 - q)) + 1 / (2 * (sigma ** 2))) b_fn = lambda z: ((mu0(z) / mu(z)) ** lmbd_int - (mu(-z) / mu0(z)) ** lmbd_int) if verbose: print "M =", m print "f(-M) = {} f(M) = {}".format(b_fn(-m), b_fn(m)) assert b_fn(-m) < 0 and b_fn(m) < 0 b_lambda_int1_fn = lambda z: mu0(z) * (mu0(z) / mu(z)) ** lmbd_int b_lambda_int2_fn = lambda z: mu0(z) * (mu(z) / mu0(z)) ** lmbd_int b_int1 = integral_bounded_mp(b_lambda_int1_fn, -m, m) b_int2 = integral_bounded_mp(b_lambda_int2_fn, -m, m) a_lambda_m1 = compute_a_mp(sigma, q, lmbd - 1) b_bound = a_lambda_m1 + b_int1 - b_int2 if verbose: print "B by numerical integration", b_lambda print "B must be no more than ", b_bound assert b_lambda < b_bound + 1e-5 return _to_np_float64(b_lambda)
Example #24
Source File: LogisticRegression.py From fuku-ml with MIT License | 5 votes |
def error_function(self, x, y, W): # need refector ''' Error function to calculate error: cross entropy error ''' error = np.log(1 + np.exp((-1) * y * np.inner(x, W))) return error
Example #25
Source File: metric.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def __init__(self, eps=1e-12, name='log-loss', output_names=None, label_names=None): super(LogLossMetric, self).__init__( name, eps=eps, output_names=output_names, label_names=label_names) self.eps = eps
Example #26
Source File: gru_bucketing.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def Perplexity(label, pred): label = label.T.reshape((-1,)) loss = 0. for i in range(pred.shape[0]): loss += -np.log(max(1e-10, pred[i][int(label[i])])) return np.exp(loss / label.size)
Example #27
Source File: rnn_cell_demo.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def Perplexity(label, pred): # TODO(tofix): we make a transpose of label here, because when # using the RNN cell, we called swap axis to the data. label = label.T.reshape((-1,)) loss = 0. for i in range(pred.shape[0]): loss += -np.log(max(1e-10, pred[i][int(label[i])])) return np.exp(loss / label.size)
Example #28
Source File: lstm_bucketing.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def Perplexity(label, pred): label = label.T.reshape((-1,)) loss = 0. for i in range(pred.shape[0]): loss += -np.log(max(1e-10, pred[i][int(label[i])])) return np.exp(loss / label.size)
Example #29
Source File: Collection.py From fullrmc with GNU Affero General Public License v3.0 | 5 votes |
def gaussian(x, center=0, FWHM=1, normalize=True, check=True): """ Compute the normal distribution or gaussian distribution of a given vector. The probability density of the gaussian distribution is: :math:`f(x,\\mu,\\sigma) = \\frac{1}{\\sigma\\sqrt{2\\pi}} e^{\\frac{-(x-\\mu)^{2}}{2\\sigma^2}}` Where:\n * :math:`\\mu` is the center of the gaussian, it is the mean or expectation of the distribution it is called the distribution's median or mode. * :math:`\\sigma` is its standard deviation. * :math:`FWHM=2\\sqrt{2 ln 2} \\sigma` is the Full Width at Half Maximum of the gaussian. :Parameters: #. x (numpy.ndarray): The vector to compute the gaussian #. center (number): The center of the gaussian. #. FWHM (number): The Full Width at Half Maximum of the gaussian. #. normalize(boolean): Whether to normalize the generated gaussian by :math:`\\frac{1}{\\sigma\\sqrt{2\\pi}}` so the integral is equal to 1. #. check (boolean): whether to check arguments before generating vectors. """ if check: assert is_number(center), LOGGER.error("center must be a number") center = FLOAT_TYPE(center) assert is_number(FWHM), LOGGER.error("FWHM must be a number") FWHM = FLOAT_TYPE(FWHM) assert FWHM>0, LOGGER.error("FWHM must be bigger than 0") assert isinstance(normalize, bool), LOGGER.error("normalize must be boolean") sigma = FWHM/(2.*np.sqrt(2*np.log(2))) expKernel = ((x-center)**2) / (-2*sigma**2) exp = np.exp(expKernel) scaleFactor = 1. if normalize: scaleFactor /= sigma*np.sqrt(2*np.pi) return (scaleFactor * exp).astype(FLOAT_TYPE)
Example #30
Source File: common.py From numpynet with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _softplus(x, deriv=False): """ The soft-plus function and its derivative """ y = np.log(1.0 + (np.exp(x))) if deriv: return 1.0 / (1.0 + np.exp(-x)) return y