def prob_model2(bigram, lang, param): if bigram not in bigrams_list: value = np.exp(0) else: tup = (bigram, lang) pos_l = label_list.index(lang) * len(character_bigram_vocab) pos_k = bigrams_list.index(bigram) pos = pos_l + pos_k if tup in model2_features: value = param[pos] * model2_features[tup] else: value = np.float64(0) value = np.exp(value) lower = 0 for key in character_vocab: try: new_bigram = bigram[0] + key tup = (new_bigram, lang) pos_k = bigrams_list.index(new_bigram) pos = pos_l + pos_k if tup in model2_features: new_value = param[pos] * model2_features[tup] else: new_value = np.float64(0) new_value = np.exp(new_value) lower += new_value except: continue if lower == 0: prob = 1 else: prob = value / lower return prob
def get_step_size_function(self, x, d): def f(l): return self.__call__(x + l * d) _derivative = autograd.grad(f) _derivative2 = autograd.grad(_derivative) f.derivative = lambda l: _derivative(np.float64(l)) f.derivative2 = lambda l: _derivative2(np.float64(l)) f.domain = lambda l, gamma: self.domain(x, l, d, gamma) return f
def dictionaries_model2(): bigrams_list = [] model2_features = {} for key in character_bigram_vocab: bigrams_list.append(key) for label in label_list: for key in character_bigram_vocab: tup = (key, label) tup2 = (key[0], label) if tup in label_bigram_vocab: value = label_bigram_vocab[tup] / (np.float64( label_unigram_vocab[tup2])) model2_features[tup] = value else: model2_features[tup] = (np.float64(0)) return (model2_features, bigrams_list)
def condition(args, to_shape=None): """Condition n-d args for PyCO2SYS. If NumPy can broadcast the args together, they are a valid combination, and they will be combined following NumPy broadcasting rules. All array-like args will be broadcast into the same shape. Any scalar args will be left as scalars. """ try: # check all args can be broadcast together args = {k: v for k, v in args.items() if v is not None} args_broadcast = broadcast1024(*args.values()) if to_shape is not None: try: # check args can be broadcast to to_shape, if provided broadcast1024(np.ones(to_shape), np.ones(args_broadcast.shape)) args_broadcast_shape = to_shape except ValueError: print("PyCO2SYS error: args are not broadcastable to to_shape.") return else: args_broadcast_shape = args_broadcast.shape # Broadcast the non-scalar args to a consistent shape args_conditioned = { k: np.broadcast_to(v, args_broadcast_shape) if not np.isscalar(v) else v for k, v in args.items() } # Convert to float, where needed args_conditioned = { k: np.float64(v) if k in input_floats else v for k, v in args_conditioned.items() } except ValueError: print("PyCO2SYS error: input shapes cannot be broadcast together.") return return args_conditioned
def model1(param): N = len(words_label_list) V = len(words_vocab) J = 0 for i in range(0, len(words_label_list)): if words_label_list[i] in words_label_vocab: upper = np.float64(words_label_vocab[words_label_list[i]]) else: upper = np.float64(0) if words_list[i] in words_vocab: lower = np.float64(words_vocab[words_list[i]]) else: lower = np.float64(0) prob = (upper + np.exp(param)) / ((lower + (np.exp(param) * V))) J += np.log(prob) J = J / N return -J
def Error_function(W_b, V_init, u_init, target, output_nrns, horizon): W = W_b[:W_b.shape[0] - 1, :].astype(np.float64) b = W_b[-1, :].astype(np.float64) V = V_init.astype(np.float64) u = u_init.astype(np.float64) dt = np.float64(0.2) alpha = np.float64(0.004) beta = np.float64(0.005) E = np.float64(0) for i in range(horizon): V_new = V + dt * ((fr_fun(V) @ W).flatten() + b - u) u_new = u + dt * alpha * (beta * V - u) V = V_new u = u_new s = fr_fun(V) E += np.sum((s[output_nrns] - target[i, output_nrns])**2) return E
def saving_dictionaries(): character_bigram_features = {} label_bigram_features = {} V = len(character_vocab) for key in character_bigram_vocab: upper = np.float64(character_bigram_vocab[key]) lower = np.float64(character_vocab[key[0]]) prob = (upper + 1) / (np.float64(lower + V)) character_bigram_features[key] = prob for label in label_list: for key in character_bigram_vocab: tup = (key, label) if tup in label_bigram_vocab: value = label_bigram_vocab[tup] / (np.float64( character_bigram_vocab[key])) label_bigram_features[tup] = value else: label_bigram_features[tup] = (np.float64(0)) return (character_bigram_features, label_bigram_features)
def model2(param): N = len(r_list) J = 0 total = np.float64(sum(lang_count.values())) for i in r_list: lang = words_label_list[i][0] word = words_label_list[i][1] prob = lang_count[lang] / total for j in range(0, len(word) - 1): bigram = word[j] + word[j + 1] prob_bi = prob_model2(bigram, lang, param) prob = prob * prob_bi if prob > 1: print prob J += np.log(prob) J = J / N return -J
def __init__(self, model: Union[IModel, IDifferentiable], target: np.array, beta: np.float64 = np.float64(1)) -> None: """ Negative Lower Confidence Bound acquisition function for target vector estimation. For more information see: Efficient Bayesian Optimization for Target Vector Estimation Uhrenholt, Anders K. and Jensen, Bjorn S. 2019, AISTATS :param model: model that is used to compute the improvement. :param target: target to be estimated. :param beta: Exploration / exploitation parameter. """ self.model = model self.target = target self.k = target.shape[-1] self.beta = beta self.grad_fun = None
def adam(alp, b1, b2, model, init): eps = 1.0e-8 m = 0 u = 0 t = 0 tol = 10 gradi = grad(model) x = agnp.float64(init) accf = model(x) while (tol > 1.0e-3) | (accf > 1e-5): t += 1 g = gradi(x) m = b1 * m + (1 - b1) * g u = b2 * u + (1 - b2) * g**2 a_t = alp * agnp.sqrt(1 - b2**t) / (1 - b1**t) xn = x - a_t * m / (agnp.sqrt(u) + accf * eps) tol = agnp.linalg.norm(xn - x) x = xn accf = model(x) return (x, accf)
def prediction_model2(filename, label_list, param): total = np.float64(sum(lang_count.values())) true_labels = [] words = [] predicted_labels = [] with open(filename) as data_file: data = json.load(data_file) counter = 0 for item in data: for i in range(0, len(data[item])): lang = data[item][i]['lang'] true_labels.append(lang) text = data[item][i]['text'] words.append(text) counter += 1 if counter > 200: break for item in words: maximum = 0 predict = "" for label in label_list: prob = lang_count[label] / total for j in range(0, len(item) - 1): bigram = item[j] + item[j + 1] prob_bi = prob_model2(bigram, label, param) prob = prob * prob_bi if prob > 1: print prob if prob > maximum: maximum = prob predict = label print predict predicted_labels.append(predict) return (true_labels, predicted_labels)
def apply(self, structures, mixture_coefs=None): """Apply constraints using given structure(s). Compute negative log likelhood for each constraint using the given structure. Parameters ---------- structures : array or autograd SequenceBox or list of structures 3D chromatin structure(s) for which to compute the constraint. Returns ------- dict Dictionary of constraint names and negative log likelihoods. """ if len(self.lambdas) == 0 or sum(self.lambdas.values()) == 0: return {} if mixture_coefs is None: mixture_coefs = [1.] if not (isinstance(structures, list) or isinstance(structures, SequenceBox)): structures = [structures] if len(structures) != len(mixture_coefs): raise ValueError( "The number of structures (%d) and of mixture coefficents (%d)" " should be identical." % (len(structures), len(mixture_coefs))) obj = {k: ag_np.float64(0.) for k, v in self.lambdas.items() if v != 0} for struct, gamma in zip(structures, mixture_coefs): if 'bcc' in self.lambdas and self.lambdas['bcc']: neighbor_dis = ag_np.sqrt( (ag_np.square(struct[self.row_adj] - struct[self.col_adj])).sum(axis=1)) n_edges = neighbor_dis.shape[0] obj['bcc'] = obj['bcc'] + gamma * self.lambdas['bcc'] * \ (n_edges * ag_np.square(neighbor_dis).sum() / ag_np.square( neighbor_dis.sum()) - 1.) if 'hsc' in self.lambdas and self.lambdas['hsc']: nbeads_per_homo = self.lengths_lowres.sum() struct_masked = ag_np.where(self.torm_3d, np.nan, struct) homo1 = struct_masked[:nbeads_per_homo, :] homo2 = struct_masked[nbeads_per_homo:, :] begin = end = 0 for i in range(len(self.lengths_lowres)): end = end + self.lengths_lowres[i] homo_dis = ag_np.sqrt( ag_np.square( np.nanmean(homo1[begin:end, :], axis=0) - np.nanmean(homo2[begin:end, :], axis=0)).sum()) obj['hsc'] = obj['hsc'] + gamma * self.lambdas['hsc'] * \ ag_np.square(ag_np.array([float( self.params['hsc'][i]) - homo_dis, 0]).max()) begin = end if 'struct' in self.lambdas and self.lambdas['struct']: struct_myres = decrease_struct_res( struct, multiscale_factor=struct.shape[0] / self.params['struct'].shape[0], lengths_prev=self.lengths).reshape(-1, 3) obj['struct'] = obj['struct'] + gamma * \ self.lambdas['struct'] * ag_np.sqrt(np.nanmean(ag_np.square( self.params['struct'] - struct_myres))) / ( self.params['struct'].max() - self.params['struct'].min()) # Check constraints objective for k, v in obj.items(): if ag_np.isnan(v): raise ValueError("Constraint %s is nan" % k) elif ag_np.isinf(v): raise ValueError("Constraint %s is infinite" % k) return {'obj_' + k: v for k, v in obj.items()}
def test_tangent_vector_multiplication(self): # Regression test for https://github.com/pymanopt/pymanopt/issues/49. manifold = Product((Euclidean(12), Grassmann(12, 3))) x = manifold.random_point() eta = manifold.random_tangent_vector(x) np.float64(1.0) * eta