def load_parameters_with_filename(filepath): f = open(filepath, 'r') _ = f.readline() settings = arg.processCommandLineArguments(f.readline().strip().split(" "), None) f.close() return settings[0]
def load_from_pickle(f): """ Reads models from pickle with settings as header. Settings are pre-populated with default values from tools.arguments. """ # Skip deprecated first line _ = f.readline() settings = arg.processCommandLineArguments(f.readline().strip().split(" "), None) try: savedVars = pickle.load(f) except IndexError: return False f.close() return dict(savedVars), settings[0]
score_types['Inpt.size %d' % size] = 'Score by input size = %d:' % size for symbolId in range(20): score_types['Symbol %d' % symbolId] = 'Symbol %d:' % symbolId for offset in range(20): score_types['X offset %d' % offset] = 'X offset = %d:' % offset for trueSize in range(20): for nrCorrect in range(20): score_types['T %d C %d' % (trueSize, nrCorrect)] = 'Prediction size %d nr correct %d' % ( trueSize, nrCorrect) trackerreporter.init('http://rjbruin.nl/experimenttracker/api/', api_key) cmdargs = sys.argv[1:] # Check for experiment settings file argument and obtain new arguments allparameters = processCommandLineArguments(cmdargs) newparameters = [] if (allparameters[0]['debug']): newparameters = allparameters else: for i in range(len(allparameters)): iterative = False # Ask for experiment base name basename = raw_input("Experiment %d name (%s): " % (i + 1, allparameters[i]['name'])) if (' ' in basename): raise ValueError( "Experiment name cannot contain whitespace! Offending name: \"%s\"" % basename) allparameters[i]['basename'] = allparameters[i]['name'] if (basename != ''):
def writeToVerbose(verboseOutputter, s): f = verboseOutputter['f']() f.write(s + '\n') f.close() if (__name__ == '__main__'): np.set_printoptions(precision=3, threshold=10000000) # Specific settings - default name is time of experiment name = time.strftime("%d-%m-%Y_%H-%M-%S") saveModels = True # Process parameters parameters = processCommandLineArguments(sys.argv[1:]) # Set up extreme verbose output # Can always be called but will not do anything if not needed if (parameters['extreme_verbose']): if (parameters['tensorflow']): raise ValueError( "Feature extreme_verbose = True not supported in combination with tensorflow = True!" ) verboseOutputter = { 'name': './verbose_output/%s.debug' % name } verboseOutputter['f'] = lambda: open(verboseOutputter['name'], 'a') verboseOutputter['write'] = lambda s: writeToVerbose( verboseOutputter, s) else:
@author: Robert-Jan ''' from tools.arguments import processCommandLineArguments from tools.model import constructModels if __name__ == '__main__': max_length = 17 intervention_range = 12 base_offset = 5 # Process parameters cmd = "--dataset ./data/subsystems_deep_simple_topcause --finish_subsystems True" #cmd += " --max_training_size 1000"; parameters = processCommandLineArguments(cmd.split(" ")) # Construct models datasets, model = constructModels(parameters, 0, {}) dataset = datasets[0] validCountsPerInterventionLocation = {} for interventionLocation in range( max_length - intervention_range - base_offset, max_length - base_offset): validCountsPerInterventionLocation[interventionLocation] = 0 result = dataset.expressionsByPrefix.get_next([]) while (result != False): expression, path = result print(" / ".join([expression]))
def testExists(self): params = [ '--finish_subsystems', 'True', '--only_cause_expression', '1', '--dataset', '../data/subsystems_shallow_simple_topcause', "--sample_testing_size", "10000", "--n_max_digits", "17", "--intervention_base_offset", "0", "--intervention_range", "17", "--nesterov_optimizer", "True", "--decoder", "True", "--learning_rate", "0.005", "--hidden_dim", "256" ] params = processCommandLineArguments(params) datasets, _ = constructModels(params, 1234, {}) dataset = datasets[0] storage = dataset.expressionsByPrefix expressions = [ "(3-9)*(0-3)=18", "(4-7)+(6*5)=27", "(0/6)+(2*8)=16", "(1-4)+(3+6)=6", "(6+0)+(0-1)=5" ] for i, expr in enumerate(expressions): self.assertEqual( storage.exists(expr), True, "(exists) Failing exists lookup for sample %d" % i) _, _, _, _, branch = storage.get(expr[:4], alsoGetStructure=True) closest, _, _, _ = branch.get_closest(expr[4:]) self.assertNotEqual( closest, False, "(exists) Branch-based lookup failed with False for sample %d: %s" % (i, closest)) self.assertEqual( closest, expr, "(exists) Failing branch-based lookup for sample %d: %s" % (i, closest)) # Apply mutations and test if both methods get the same new label for n in range(20): intervention_location = np.random.randint(0, len(expr)) new_symbol = np.random.randint(dataset.data_dim) new_expression = expr[:intervention_location] + dataset.findSymbol[ new_symbol] + expr[intervention_location + 1:] print("Old: %s\tNew: %s" % (expr, new_expression)) _, _, valids, _, branch = storage.get( new_expression[:intervention_location + 1], alsoGetStructure=True) if (new_expression not in valids and len(valids) > 0): # Old method: compare all profiler.start('old') nearest = -1 nearest_score = 100000 for j, nexpr in enumerate(valids): score = string_difference(new_expression, nexpr) if (score < nearest_score): nearest = j nearest_score = score closest_old = valids[nearest] profiler.stop('old') profiler.start('new') # New method: closest_new, _, _, _ = branch.get_closest( new_expression[intervention_location + 1:]) profiler.stop('new') if (closest_old != closest_new): print( "(exists) Intervened closest do not match for sample %d: loc %d / orig %s / int %s / old %s / new %s" % (i, intervention_location, expr, new_expression, closest_old, closest_new)) # self.assertEqual(closest_old, closest_new, # "(exists) Intervened closest do not match for sample %d: loc %d / orig %s / int %s / old %s / new %s" % # (i, intervention_location, expr, new_expression, closest_old, closest_new)); profiler.profile()