def get_type(self, val, line, localvar, localval): # Get a variable's type m = re.search(var_id, val) if m: # print('A variable!') if val in self.variables: return self.get_type(Utils.val_to_jr(self.getval(val)), line, localvar, localval) else: error(val + " is not a registered variable", self.script[line - 1], line) if not m: if val != "": m = Utils.tokenize(val, self.script[line - 1], line, localvar, localval) if m: # print("A number") # return 'num' if type(m) == Tokens.Num: return 'num' elif type(m) == Tokens.Str: return 'str' elif type(m) in [Tokens.Bool, Tokens.And, Tokens.Equal]: return 'bool' else: return None # if not m: # m = re.search('^".*?"$', val) # if m: # # print("A string") # return 'str' if not m: error(val + " is not a registered variable", self.script[line - 1], line)
def _is_valid(self): for f in self.facts: for q in self.queries: if f == q: error("queries: [%c] is already known in facts [%s]" % (q, self.content["facts"])) for r in self.rules: if f in r.split("=")[1]: error("facts: [%c] cannot be defined in a rule [%s]" % (f, r))
def update_weights(A, B, C, X_unfold, Id, norm_x, lamb, weights, sketching_rates, rank, nu, eps): for i, w in enumerate(weights): start = time.time() s = sketching_rates[i] A_new, B_new, C_new = update_factors(A, B, C, X_unfold, Id, lamb, s, rank) total_time = time.time() - start weights[i] *= np.exp( -nu / eps * (error(X_unfold[0], norm_x, A_new, B_new, C_new) - error(X_unfold[0], norm_x, A, B, C)) / (total_time)) weights /= np.sum(weights) return
def function_call(self, name, args, line, localvar, localval): # Analyze a function call line = line + 1 if (name, len(args)) in self.functions: argspass = [] # print("Function " + name + " exists!") for arg in args: arg = arg.strip(' ') argspass.append(self.detect_val(arg, line, localvar, localval)) #print(line, name, argspass, '->') self.jr_function(name, argspass) else: func = str(self.script[line - 1].strip('\n')) for arg in args: func = func.replace( arg, self.get_type(arg, line, localvar, localval)) error("The function " + func + " is not registered", self.script[line - 1].strip('\n'), line)
def detect_val( self, val, line, localvar=None, localval=None ): # Get a variable's value or infer the texts type and return it's value log(self.verbose, line, localvar, localval) if localval is None: localval = [] if localvar is None: localvar = [] m = re.search(var_id, val) if m: # Variable: get it's value if val in self.variables: return self.getval(val) elif val in localvar: return localval[localvar.index(val)] else: error(val + " is not a registered variable", self.script[line - 1], line) if not m and val != "": m = Utils.tokenize(val, self.script[line - 1], line, localvar, localval, self) if m: # print("A number") # return 'num' return m.value() if val == "": return None # Not a variable: infer it's type and return it's value #m = re.search('^\d+$', val) #if m: # Number (num) # return int(val) # if not m: # m = re.search('^".*?"$', val) # if m: # # String (str) # return val.strip('"') if not m: error(val + " is not a registered variable", self.script[line - 1], line)
def register_functions(self, toRegister): # Find the functions and their code blocks = [] finding_end = False start = None #for line in toRegister: for i in range(len(toRegister)): line = toRegister[i] m = re.search('func\s+(\w+)\s*\((.*?)\)\s*{', line) if m: if finding_end: error("Cannot declare a function inside another function", self.script[i], i + 1) start = i finding_end = True blocks.append(m.group(0).rstrip('{')) continue m = re.search('(.*?){', line) if m: blocks.append(m.groups(0)[0]) continue m = re.search('}', line) if m: popped = blocks.pop(len(blocks) - 1) log(self.verbose, "FUNC REGISTRATION: End for " + popped + " found") if len(blocks) == 0: finding_end = False m = re.search('func\s+(\w+)\s*\((.*?)\)\s*', popped) if m: arg_str = m.groups(0)[1] args = re.split('\s*,\s*', arg_str) self.functions.append((m.groups(0)[0], len(args))) self.arguments.append(args) self.codes.append(('script', start, i)) log(self.verbose, "FUNC REGISTRATION ENDED") log(self.verbose, "FUNCS ", self.functions, self.arguments, self.codes) log(self.verbose, "SCRIPT:") Utils.print_script(log, self.verbose, self.script) log(self.verbose, "\n" * 10)
def update_weights( X, A, B, C, X_unfold, Id, norm_x, lamb, weights, sketching_rates, rank, eta_cpd, eps, b0, eta_ada, F, ): dim_1, dim_2, dim_3 = X.shape old_error = error(X_unfold[0], norm_x, A, B, C) for i, w in enumerate(weights): start = time.time() s, grad = sketching_rates[i] if grad: A_new, B_new, C_new, _ = AdaIteration(X, X_unfold, A, B, C, b0, eta_ada, F, {}, int(s * dim_1**2), norm_x) else: A_new, B_new, C_new = update_factors(A, B, C, X_unfold, Id, lamb, s, rank) total_time = time.time() - start weights[i] *= np.exp( -eta_cpd / eps * (error(X_unfold[0], norm_x, A_new, B_new, C_new) - old_error) / (total_time)) weights /= np.sum(weights)
from sys import argv from Config import Config from InferenceEngine import InferenceEngine from Utils import error CONFIG_FILE = "config" def main(): config = Config(CONFIG_FILE) Expert = InferenceEngine(config) Expert.expertise() Expert.results() # print(Expert.data) # print(Expert.queries) # print(Expert.facts) if __name__ == "__main__": if len(argv) != 2: error("arguments") main()
def parse_func(self, toParse, isMain=False, localvariables=None, localvalues=None): # Parse an actual code block if localvalues is None: localvalues = [] if localvariables is None: localvariables = [] #print(localvariables, localvalues) for line in toParse: i = toParse.index(line) inside = False #print("PARSING", line) for func in self.codes: log(self.verbose, func) if func[1] < i < func[2]: inside = True break if inside: if not isMain: #print("LINE", line) m = re.search('(\w+?)\((.*?)\)$', line) if m: #print(m.groups()) #print("CALLING:", m.groups(0)[0]) self.function_call( m.groups(0)[0], re.split(',\s*', m.groups(0)[1]), i, localvariables, localvalues) else: # Match a function #print("LINE", line) m = re.search('(\w+?)\((.*?)\)$', line) if m: #print(m.groups()) #print("CALLING:", m.groups(0)[0]) self.function_call( m.groups(0)[0], re.split(',\s*', m.groups(0)[1]), i, localvariables, localvalues) # Match a global variable declaration regex = '(global\s+)?(\w+)\s*=\s*(".*"|\d+|\w+)$' if not isMain: regex = '(global\s+)(\w+)\s*=\s*(".*"|\d+|\w+)$' # Match a global variable declaration m = re.search(regex, line) if m: self.register_var(m.groups(0)[1], m.groups(0)[2], i) m = re.search('if\s*\((.*?)\){', line) if m: blocks = [] finding_end = False start = None for line in toParse: i = toParse.index(line) m = re.search('if\s*\((.*?)\){', line) if m: if finding_end: error( "Cannot declare a function inside an if block", self.script[i], i + 1) start = i finding_end = True blocks.append(m.group(0).rstrip('{')) continue m = re.search('(.*?){', line) if m: blocks.append(m.groups(0)[0]) continue m = re.search('}', line) if m: popped = blocks.pop(len(blocks) - 1) if len(blocks) == 0: finding_end = False m = re.search('if\s*\((.*?)\){', popped) if m: arg_str = m.groups(0)[1] args = re.split('\s*(==|<=|<|>=|>)\s*', arg_str) self.functions.append( (m.groups(0)[0], len(args))) self.arguments.append(args) self.codes.append(('script', start, i))
def decompose(X, F, sketching_rates, lamb, eps, eta_cpd, Hinit, max_time, b0, eta_ada): weights = np.array([1] * (len(sketching_rates))) / (len(sketching_rates)) global Gt Gt = [] print(sketching_rates) dim_1, dim_2, dim_3 = X.shape A, B, C = Hinit[0], Hinit[1], Hinit[2] X_unfold = [tl.unfold(X, m) for m in range(3)] norm_x = norm(X) I = np.eye(F) PP = tl.kruskal_to_tensor((np.ones(F), [A, B, C])) e = np.linalg.norm(X - PP)**2 / norm_x NRE_A = {} prev_e = e sketching_rates_selected = {} now = time.time() itr = 1 weights_record = {} weights_record[0] = list(weights) timer = Timer() while timer.elapsed() < max_time: s, grad = sketching_weight(sketching_rates, weights) if not grad: # Solve Ridge Regression for A,B,C A, B, C = update_factors(A, B, C, X_unfold, I, lamb, s, F) else: A, B, C, _ = AdaIteration(X, X_unfold, A, B, C, b0, eta_ada, F, None, int(s * dim_1**2), norm_x) # Update weights updated = False p = np.random.binomial(n=1, p=eps) if p == 1 and len(sketching_rates) > 1: print("updating_weights:") weights_t = time.time() update_weights( X, A, B, C, X_unfold, I, norm_x, lamb, weights, sketching_rates, F, eta_cpd, eps, b0, eta_ada, F, ) print(f"Weights updated in {time.time() - weights_t}:") print(weightsStr(weights, sketching_rates)) updated = True timer.pause() elapsed = timer.elapsed() if updated: weights_record[elapsed] = list(weights) e = error(X_unfold[0], norm_x, A, B, C) NRE_A[elapsed] = (e, grad, s) sketching_rates_selected[elapsed] = s print( f"Iteration Summary:\nIteration: {itr}\nAlgorithim: {'adagrad' if grad else 'sketched ALS'}\nSketching Rate: {s}\nError: {e}\nNormalized Error: {e/norm_x}\nChange in error: {prev_e - e}\nRelative Change in error: {(prev_e - e)/prev_e}\nElapsed: {elapsed}" ) print() itr += 1 prev_e = e timer.resume() return (A, B, C, NRE_A, weights_record)
def BrasCPD(X, b0, n_mb, max_it, A_init, A_gt): A = A_init # setup parameters dim = len(X.shape) dim_vec = X.shape F = A[0].shape[1] PP = tl.kruskal_to_tensor((np.ones(F), A)) err_e = (np.linalg.norm(X[..., :] - PP[..., :])**2) / X.size NRE_A = [err_e] MSE_A = [] mse = 10**10 for x in list(permutations(range(3), 3)): m = (1.0 / 3.0) * (MSE(A[x[0]], A_gt[0]) + MSE(A[x[1]], A_gt[1]) + MSE(A[x[2]], A_gt[2])) mse = min(mse, m) MSE_A.append(mse) tic = time.time() time_A = [time.time() - tic] for it in range(1, int(math.ceil(max_it))): # step size alpha = b0 / (n_mb * (it)**(10**-6)) # randomly permute the dimensions block_vec = np.random.permutation(dim) d_update = block_vec[0] # sampling fibers and forming the X_[d] = H_[d] A_[d]^t least squares [tensor_idx, factor_idx] = sample_fibers(n_mb, dim_vec, d_update) tensor_idx = tensor_idx.astype(int) cols = [tensor_idx[:, x] for x in range(len(tensor_idx[0]))] X_sample = X[tuple(cols)] X_sample = X_sample.reshape( (int(X_sample.size / dim_vec[d_update]), dim_vec[d_update])) # perform a sampled khatrirao product A_unsel = [] for i in range(d_update - 1): A_unsel.append(A[i]) for i in range(d_update + 1, dim): A_unsel.append(A[i]) H = np.array(sampled_kr(A_unsel, factor_idx)) alpha_t = alpha d = d_update A[d_update] = A[d_update] - alpha_t * ( A[d_update] @ H.transpose() @ H - (X_sample.transpose() @ H)) A[d_update] = proxr(A[d_update], d) if it % math.ceil((X.shape[0]**2 / n_mb)) == 0: time_A.append(time.time() - tic) err = error(tl.unfold(X, 0), tl.norm(X), A[0], A[1], A[2]) NRE_A.append(err) mse = 10**10 for x in list(permutations(range(3), 3)): m = (1.0 / 3.0) * (MSE(A[x[0]], A_gt[0]) + MSE( A[x[1]], A_gt[1]) + MSE(A[x[2]], A_gt[2])) mse = min(mse, m) MSE_A.append(mse) print("MSE = {}, NRE = {}".format(mse, err)) return (time_A, NRE_A, MSE_A, A)