コード例 #1
1
 def test(self):
   """ Test that every image is correctly recognized. """
   Experiment.test(self)
   start = time.time()
   
   num_confused = 0.0
   
   classifier = self.network.get_classifier()
   i = 0
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     recognized = self.network.do_inference(numpy.array(image), category)
     if not recognized:
       active_cats = classifier.get_winning_category()
       #print colored("Failed: " + category + " recognized as "+active_cats, 'red')
       num_confused += 1
     i += 1
     if i % self.PRINT_INCR == 0: print "Iter:", i
     
   confusion_rate = num_confused / float(i)
   
   elapsed = (time.time() - start)
   print "Testing time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Testing complete", "green")
   print colored("\nConfusion rate:  " + str(confusion_rate), 'cyan')
コード例 #2
1
def run_experiment(args):
    """ Parallelizable method for computing experiments.

    This method is used in parallel computation for running experiments in
    parallel. Due to the nature of pickling, it must be declared globally,
    because instance methods cannot be pickled.
    """
    # args is a tuple, so that we can map over an array of tuples.
    # see run_parallel_experiments()
    params, param_name, val = args

    params = params.copy()
    params[param_name] = val

    while True:
        try:
            start_time = time.clock()
            exp = Experiment(**params)
            exp.compute_informativeness()
            break
        except Exception:
            traceback.print_exc()

    elapsed_time = time.clock() - start_time
    print "Experiment with val %s added in %0.2f seconds" % \
            (str(val), elapsed_time)

    return val, exp
コード例 #3
0
    def altruism(self, altruisticProbability, selfishProbability, altruismCost,
                 altruismBenefit, disease, harshness, numTicks):
        """
        Runs an experiment of the Biology/Evolution/Altruism model.

        :returns: The table output of the experiment.
        """
        job = uuid4()
        exp_path = '/tmp/%s/experiment.xml' % job
        out_path = '/tmp/%s/out.csv' % job

        params = {
            'altruistic-probability': altruisticProbability,
            'selfish-probability': selfishProbability,
            'cost-of-altruism': altruismCost,
            'benefit-from-altruism': altruismBenefit,
            'disease': disease,
            'harshness': harshness,
        }

        exp = Experiment(steps=numTicks, params=params)
        exp.add_metric(color='pink').add_metric(color='green')
        exp.write_xml(exp_path)

        model = '%s/models/Sample Models/Biology/Evolution/Altruism.nlogo' % NETLOGO_HOME
        self.call_experiment(model, exp_path, exp.name, out_path)

        with open(out_path, 'r') as out_file:
            return out_file.read()
コード例 #4
0
 def __init__(self):
     Experiment.__init__(self)
     persistance.Dommable.__init__(self)
     
     self.amplifiers = {'None':'bridge', 'Prana':'Prana', 'Milmega 1':'Milmega', 'Milmega 2':'Milmega'}
     self.amplifier = EnumerateProperty('None',['Automatic'] + self.amplifiers.keys())
     self.amplifier.changedTo.connect(self.setAmplifier)
コード例 #5
0
ファイル: main.py プロジェクト: zydmayday/runfast
def testQValueNetwork(startTurn=0, loopNum=1000, type=''):
	agents = []
	win_nums = {}
	test_name = PLAYER_LIST[type][0]
	test_filename = TEST[type]
	if os.path.isfile(test_filename):
		with open(test_filename, 'r') as f:
			win_nums = pickle.load(f)

	for i in range(0, 3):
		playerName = PLAYER_LIST[type][i]
		nw = NETWORK[type](playerName)
		if playerName == test_name:
			nw.loadNet(playerName, startTurn)
		rfa = AGENT[type](playerName, nw)
		agents.append(rfa)
		 
	env = RunFastEnvironment()
	exp = Experiment(env, agents)

	for i in range(loopNum):
		if not win_nums.get(startTurn):
			win_nums[startTurn] = {}
		testHistory = exp.doTest(test_name)
		for j in range(0,3):
			playerName = PLAYER_LIST[type][j]
			if not win_nums[startTurn].get(playerName):
				win_nums[startTurn][playerName] = {'point': 0, 'win': 0}
			win_nums[startTurn][playerName]['point'] += testHistory[playerName]
			if testHistory['name'] == playerName:
				win_nums[startTurn][playerName]['win'] += 1
	with open(test_filename, 'w') as f:
		pickle.dump(win_nums, f)
コード例 #6
0
ファイル: main.py プロジェクト: tomsilver/thesis
def individualStrategies():
	#allxVals = {1: range(4, 10, 5), 2: range(100, 111, 10)}
	allxVals = {1: range(4, 101, 5), 2: range(100, 1001, 10)}
	allPlayerTypes = {'Random Play': players.RandomPlayer, 
					  'Honest Play': players.HonestPlayer, 
					  'Minimum Guessing': players.MinimumGuessingPlayer, 
					  'Constant Guessing': players.ConstantGuessPlayer, 
					  'Mean Guessing': players.MeanGuessPlayer, 
					  'Quantile Guessing': players.QuantileGuessPlayer,
					 }
	
	allExperimentTypes = [2]
	allStatistics = [2]

	for statistic in allStatistics:
		for experimentType in allExperimentTypes:
			xVals = allxVals[experimentType]
			for title, playerType in allPlayerTypes.items():
				playerTypes = {playerType: 1.0}
				savefile = title.replace(' ', '_')+str(experimentType)+str(statistic)+'.png'
				print "Running "+savefile

				exp = Experiment(xVals, playerTypes, experimentType, statistic)
				yVals = exp.run()
				print yVals
				plotResults(exp, yVals, title, savefile)
				print
コード例 #7
0
ファイル: brute_force.py プロジェクト: kkansky/and_or_images
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   gabor = GaborRegion((144, 192), rotations=3, 
                       initial_wavelength=3, 
                       num_wavelengths=2)
   
   # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork((144,192), num_regions=1, 
                               input_region = gabor)
   and_region = self.network.regions[1]
   classifier = self.network.get_classifier()
   
   i = 0
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     gabor.do_inference(numpy.array(image))
     active_nodes = gabor.get_active_nodes()
     pos = and_region.create_node((0,0), cxns = active_nodes)
     classifier.create_node(category, pos)
     i += 1
     if i % self.PRINT_INCR == 0: print "Iter:", i
     
   and_region.prepare_for_inference()
   classifier.prepare_for_inference()
   
   num_cxns = and_region.get_num_cxns() + classifier.get_num_cxns()
   print "Number of connections:", num_cxns
   elapsed = (time.time() - start)
   print "Training time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Training complete", "green")
コード例 #8
0
ファイル: window_grid.py プロジェクト: kkansky/and_or_images
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   self.image_shape = (144, 192)
   
   gabor = GaborRegion(self.image_shape, rotations=3, 
                       initial_wavelength=3, 
                       num_wavelengths=2)
   
   # Regions = [ GaborRegion, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork((144,192), num_regions=2, 
                               input_region = gabor)
   f1 = self.network.regions[1]
   f2 = self.network.regions[2]
   classifier = self.network.get_classifier()
   
   self.gabor_acts = gabor.precompute_image_activations(self.image_iter)
   windows = self.get_windows()
   
   for window in windows:
     
   
   
   self.network.prepare_for_inference(1)
   elapsed = (time.time() - start)
   
   total_cxns = 0
   for i, r in enumerate(self.network.regions[1:]):
     num_cxns = r.get_num_cxns()
     print "Region %s cxns: %s" % (i, num_cxns)
     total_cxns += num_cxns
   
   print "Total connections:", total_cxns
   print "Training time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Training complete", "green")
     
 def test(self):
   """ Test that every image is correctly recognized. """
   Experiment.test(self)
   start = time.time()
   
   classifier = self.network.get_classifier()
   i = 0
   while self.image_iterator.has_next():
     image, category = self.image_iterator.next()
     recognized = self.network.do_inference(numpy.array(image), category)
     if not recognized:
       active_cats = classifier.get_active_categories()
       print colored("Failed: " + category + " recognized as "+repr(active_cats), 'red')
     i += 1
     if i % self.PRINT_INCR == 0: print "Iter:", i
   
   elapsed = (time.time() - start)
   print "Testing time:", elapsed
   print "Time per category:", (elapsed / i)
   print colored("Testing complete", "green")
コード例 #9
0
ファイル: app.py プロジェクト: eraldop/blockcanvas
 def load_code_from_file(self, filename):
     experiment = Experiment()
     experiment.load_code_from_file(filename)
     self.project.experiments.append(experiment)
     self.project.active_experiment = experiment
     self.update_functions_UI(self.project.active_experiment.exec_model.statements)   
     self.update_functions_context(self.project.active_experiment,
                                   self.project.active_experiment.exec_model.statements)
コード例 #10
0
 def __init__(self, output_path=None, linewidth=1.5):
     e = Experiment(output_path)
     self.linewidth = linewidth
     if output_path is None:
         self.output_path = os.path.join(e.final_output_path, self.default_plots_path_addon)
     else:
         self.output_path = output_path
     e.makedir(self.output_path)
コード例 #11
0
ファイル: dpi.py プロジェクト: houssem21/emctestbench
    def __init__(self):
        Experiment.__init__(self)
        self.passCriterion = ExperimentSlot(parent=self,defaultValue='VoltageCriterion')
        self.transmittedPower = ExperimentSlot(parent=self,defaultValue='TransmittedPower')
        self.powerMinimum = Property(Power(-30.,'dBm'),changedSignal=self.settingsChanged)

        self.powerMaximum = Property(Power(+15.,'dBm'),changedSignal=self.settingsChanged)
        self.frequencies = SweepRange(Frequency(150e3),Frequency(1500e6),21,changedSignal=self.settingsChanged) 
コード例 #12
0
def main():
    try:
        folder_name = sys.argv[1]
        experiment = Experiment(folder_name)
    except IndexError:
        experiment = Experiment()
    experiment.retrieve_data()
    parser = FeatureParser()
    parser.initialize_matrix(experiment.raw_data_set)
    parser.print_global_names()
コード例 #13
0
ファイル: main.py プロジェクト: tomsilver/thesis
def quantileStrategy():
	xVals = [10]
	playerTypes = {players.QuantileGuessPlayer: 1.0}
	experimentType = 2
	statistic = 1
	title = "Quantile Guessing"

	exp = Experiment(xVals, playerTypes, experimentType, statistic)
	yVals = exp.run()
	print yVals
コード例 #14
0
def run_model():
    experiment = Experiment(model  = "../experiments/model-guthrie.json",
                            task   = "../experiments/task-guthrie.json",
                            result = "data/test-experiment-guthrie.npy",
                            report = "data/test-experiment-guthrie.txt",
                            n_session = 8, n_block = 1, seed = 1,
                            rootdir=os.path.dirname(__file__)) # for unittest and nosetests.
    records = experiment.run(session, save=True, force=True, parse=False)
    records = np.squeeze(records)
    mean = np.mean(records["best"], axis=0)[-1]
    assert mean >= 0.85
コード例 #15
0
def main():
    data = Experiment()
    data.retrieve_data()
    emails = []
    for example in data.raw_data_set:
        emails.append(Email(example[1]))

    i = 1
    with open("dates.csv", "w") as dates_file:
        for email in emails:
            dates_file.write(str(i) + ","+str(email.get_hour()) + "\n")
コード例 #16
0
	def runExperiment(self):
		try:
			e = Experiment(int(self.sampleEntry.get()), int(self.populationEntry.get()),
				int(self.numSuccessesEntry.get()))
			e.perform(int(self.numTrialsEntry.get()))
			self.drawResult(e.results, 0)
		except AssertionError as error:
			tkMessageBox.showerror('Data Entry Error', error.args[0])
		except ValueError as error:
			tkMessageBox.showerror('Data Entry Error', 'Please make sure each field is filled out'
			+ ' and only contains numbers') 
コード例 #17
0
def main():
    if len(sys.argv) < 2:
            sys.stderr.write("Need to pass user folder name\n")
            return

    folder_name = sys.argv[1]
    experiment = Experiment(folder_name)
	experiment.retrieve_data()
	parser = FeatureParser(experiment.raw_data_set)
	(names, secondary_names, body_text) = parser.get_mail_elements("data/beck-s/moscoso__mike/4.")
	print names
	print secondary_names
	print body_text
コード例 #18
0
 def __init__(self):
     Experiment.__init__(self)
     
     # Load cluster information
     self.loadClusters()
     
     self.makeSeedUsersData()
     self.makeFriendListData()
     self.makeLikeVectorData()
     self.makeTweetListLikedByEgoNetworkMembers()
     self.makeLikeCountData()
     self.makeMentionCountData()
     self.makeMutualFriendsCountData()
     self.makeClusterData()
コード例 #19
0
    def __init__(self):
        Experiment.__init__(self)
        self.transmittedPower = ExperimentSlot(parent=self,defaultValue='TransmittedPower')
        self.measurement = ExperimentSlot(parent=self,defaultValue='ReceivedPower') #,defaultValue='VoltageCriterion')
        
        self.generatorPower = Property(Power(+10,'dBm'),changedSignal=self.settingsChanged)
        self.generatorFrequency = ScalarProperty(Frequency(1,'GHz'),changedSignal=self.settingsChanged,minimum=Frequency(1,'Hz'))

        self.startPosition = ScalarProperty(Position(22,'mm'),changedSignal=self.settingsChanged)
        self.stopPosition = ScalarProperty(Position(-24,'mm'),changedSignal=self.settingsChanged)
        self.xPosition = ScalarProperty(Position(122,'mm'),changedSignal=self.settingsChanged)
        self.zPosition = ScalarProperty(Position(84,'mm'),changedSignal=self.settingsChanged)
        
        self.numberOfSteps = ScalarProperty(Integer(11),minimum=Integer(1),maximum=Integer(10001),changedSignal=self.settingsChanged)
コード例 #20
0
def run(agent_count, duration):
    for s in STRATEGIES:
        test = None
        if 'mixed' in s:
            test = Experiment(agent_count, s, duration, mixed_strategies=(s.split(' ')[1], s.split(' ')[2]))
        elif 'random-tsp' in s:
            test = Experiment(agent_count, s, duration, s.split(' ')[1])
        else:
            test = Experiment(agent_count, s, duration)
        test.start_game()
        average_idle_results[s].append(test.average_idle)
        max_idle_results[s].append(test.max_idle)
        exploration_time_results[s].append(test.exploration_time)
        alerts_detected_results[s].append(test.normalized_alerts)
コード例 #21
0
ファイル: project.py プロジェクト: enthought/blockcanvas
    def load(self, dirname):
        """ Loads the project from the given directory.  The existing state
        of the project is completely modified.
        """
        # TODO: We should formalize this dependency at some point and move
        # this import to the top level
        from configobj import ConfigObj

        if dirname == "":
            raise IOError("Cannot load project from empty path.")
        elif not os.path.isdir(dirname):
            raise IOError("Cannot find directory: " + dirname)

        filename = abspath(join(dirname, self.PROJECT_FILE_NAME))
        if not os.path.isfile(filename):
            raise IOError('Cannot load %s from project directory "%s".' % \
                        (self.PROJECT_FILE_NAME, dirname))

        # Every project is saved as a text ConfigObj file that describes
        # the name and location of the real data files.
        configspec = ConfigObj(self.CONFIG_SPEC, list_values=False)
        config = ConfigObj(filename, configspec=configspec)

        contexts = []
        for context_config in config["Contexts"].values():
            ctx = DataContext.load(join(dirname, context_config["file"]))
            ctx.name = context_config["name"]
            contexts.append(ctx)
        self.contexts = contexts

        experiments = []
        if hasattr(scripting, "app"):
            app = scripting.app
        else:
            app = None
        for exp_config in config["Experiments"].values():
            experiment = Experiment()
            exp_dir = join(dirname, exp_config["save_dir"])
            experiment.load_from_config(exp_config, exp_dir, project=self)
            experiments.append(experiment)
        self.experiments = experiments

        proj_config = config["Project"]
        if proj_config.has_key("active_experiment"):
            self.active_experiment = self.find_experiment(proj_config["active_experiment"])
            
        # Update Project Save Path 
        self.project_save_path = dirname
        
        return
コード例 #22
0
def test_experiment(source_data):
    # the oracles
    p_ = [0.55, 0.56, 0.57, 0.58, 0.59, 0.60, 0.75, 0.80, 0.85, 0.90]
    oracles_ = [NoisyOracle(pi) for pi in p_]
    ie_oracles_ = [IENoisyOracle(pi) for pi in p_]
    wt_oracles_ = [WTNoisyOracle(pi) for pi in p_]
    # the parameters for the experimentation
    train_p = 0.7
    n_rounds = 200
    eps = 0.9
    alpha = 0.05
    # number of repetitions
    n_rep = 10
    # the final accuracies for Random, Repeated, and IEThresh
    acc_rnd = [0 for i in range(n_rounds)]
    acc_rep = [0 for i in range(n_rounds)]
    acc_iet = [0 for i in range(n_rounds)]
    acc_wei = [0 for i in range(n_rounds)]
    accuracy_ = [acc_rnd, acc_rep, acc_iet, acc_wei]
    # the data set
    data_set = source_data.get_dataset()
    # for each method
    for k in range(len(method)):
        ora_ = oracles_
        # the oracles
        if k == 2:
            ora_ = ie_oracles_
        elif k == 3:
            ora_ = wt_oracles_
        # repeat the experiment n_rep times and take the average for the accuracy
        for i in range(n_rep):
            experiment_ = Experiment(data_set, train_p, ora_, n_rounds, eps, alpha)
            this_accuracy_ = experiment_.run_exp(method[k])
            # update the overall accuracy with this experiment
            for j in range(n_rounds):
                accuracy_[k][j] += this_accuracy_[j]
            print('k=' + str(k) + ', round: ' + str(i))
    # averaging
    for k in range(len(method)):
        for i in range(n_rounds):
            accuracy_[k][i] /= n_rep
    source_data.record_accuracy(acc_rnd, acc_rep, acc_iet, acc_wei)
    # plot
    fig = plt.gcf()
    fig.set_size_inches(10, 10)
    for i in range(len(accuracy_)):
        plt.plot(accuracy_[i], label=method[i])
    plt.legend(bbox_to_anchor=(1, 0.225))
    plt.show()
コード例 #23
0
 def train(self):
   """ Store a copy of every image in the iterator. """
   Experiment.train(self)
   start = time.time()
   
   num_images = len(self.image_iterator)
   print "Num images:", num_images
   assert num_images > 0
   
   gabor = self.gabor_region = GaborRegion((144, 192), rotations=3, 
                                           initial_wavelength=3, 
                                           num_wavelengths=2)
   
   kmeans = KmeansRegion(max(1,int(float(num_images) * self.compression)),
                         self.window_sampler)
   and_region = AndOrRegion(kmeans.image_shape, num_images)
   
   
   # Regions = [ GaborRegion, Kmeans, AndRegion, OrRegion (classifier) ]
   self.network = AndOrNetwork([gabor, kmeans, and_region])
   classifier = self.network.get_classifier()
   
   self.categories = []
   print "Extracting windows..."
   i = 0
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     self.categories.append(category)
     gabor.do_inference(numpy.array(image))
     kmeans.do_learning()
     i += 1
     
   print "Training K-means..."
   kmeans.prepare_for_inference()
   
   # Send all of the images through the feature learner to save the image
   # activations.
   print "Storing classifier features..."
   self.image_iterator.reset()
   while self.image_iterator.has_next():
     image, category, img_idx = self.image_iterator.next()
     gabor.do_inference(numpy.array(image))
     kmeans.do_inference()
     cxns = kmeans.get_active_nodes()
     pos = and_region.create_node((0,0), cxns = cxns)
     classifier.create_node(category, pos)
   
   print "Preparing for inference..."
   self.network.prepare_for_inference(2)
コード例 #24
0
ファイル: main_nn.py プロジェクト: zydmayday/runfast
def trainQValueNetwork(loopNum=10000, startTurn=0, history_filename='train_winners_nn', inputNum=192, type=1):
	'''
	通过让三个agent互相玩游戏,然后来训练出一个Q值网络
	三个agent的网络保存在playeri里面,数字分别代表的是训练了多少次后得出的网络
	胜负情况记录在train_winners里面
	'''
	agents = []
	winners = {}
	if os.path.isfile(history_filename):
		with open(history_filename, 'r') as f:
			winners = pickle.load(f)
			startTurn = sum([v for i,v in winners.items()]) 

	for i in range(0, 3):
		playerName = PLAYER_LIST[i]
		nw = RunFastNetwork(playerName, inputNum=inputNum, hiddenNum=inputNum, outNum=1)
		nw.loadNet(playerName, startTurn)
		rfa = RunFastAgent(playerName, nw)
		agents.append(rfa)
		 
	env = RunFastEnvironment()
	exp = Experiment(env, agents, type=type)

	for i in xrange(startTurn, startTurn + loopNum):

		if i % 200 == 0:
			for agent in agents:
				agent.saveNet()

			with open(history_filename, 'w') as f:
				pickle.dump(winners, f)
		# exp.setTurn(i)
		winner = exp.doEpisode()
		if winners.has_key(winner):
			winners[winner] += 1
		else:
			winners[winner] = 1

	for agent in agents:
		agent.saveNet()

	with open(history_filename, 'w') as f:
		pickle.dump(winners, f)


	print winners
	with open(history_filename, 'w') as f:
		pickle.dump(winners, f)
コード例 #25
0
ファイル: main_dn.py プロジェクト: zydmayday/runfast
def trainDeepNetwork(loopNum=10000, startTurn=0, history_filename='train_winners_dn', type=1, inputNum=192):
	'''
	用深度网络来训练Q值
	'''
	agents = []
	winners = {}

	# load history match
	if os.path.isfile(history_filename):
		with open(history_filename, 'r') as f:
			winners = pickle.load(f)
			startTurn = sum([v for i,v in winners.items()]) 

	# load agents with network
	for i in range(0, 3):
		playerName = PLAYER_LIST[i]
		nw = RunFastDeepNetwork(playerName, inputNum=inputNum, hidden1Num=inputNum, hidden2Num=inputNum, hidden3Num=inputNum, outNum=1)
		nw.loadNet(playerName, startTurn)
		rfa = RunFastAgent(playerName, nw)
		agents.append(rfa)
		 
	env = RunFastEnvironment()
	exp = Experiment(env, agents, type=type)

	for i in range(startTurn, startTurn + loopNum):

		if i % 200 == 0:
			for agent in agents:
				agent.saveNet()

			with open(history_filename, 'w') as f:
				pickle.dump(winners, f)

		# exp.setTurn(i)
		winner = exp.doEpisode()
		if winners.has_key(winner):
			winners[winner] += 1
		else:
			winners[winner] = 1

	for agent in agents:
		agent.saveNet()
	with open(history_filename, 'w') as f:
		pickle.dump(winners, f)

	print winners
	with open(history_filename, 'w') as f:
		pickle.dump(winners, f)
コード例 #26
0
ファイル: state.py プロジェクト: psederberg/smile
    def enter(self):
        # get the starting time from the parent
        self.state_time = self.get_parent_state_time()
        self.start_time = self.state_time

        # save the starting state time
        #self.log['start_time'] = self.start_time

        # add the callback to the schedule
        delay = self.state_time - now()
        if delay < 0:
            delay = 0
        if self.interval < 0:
            # schedule it for every frame
            schedule_delayed(self.callback, delay)
        else:
            # schedule the interval (0 means once)
            schedule_delayed_interval(self.callback, delay, self.interval)

        # say we're active
        self.active = True

        # update the parent time if necessary
        if self.duration > 0:
            self.advance_parent_state_time(self.duration)

        # if we don't have the exp reference, get it now
        if self.exp is None:
            from experiment import Experiment
            self.exp = Experiment.last_instance()
            
        # custom enter code
        self._enter()

        pass
コード例 #27
0
def test_model():
    experiment = Experiment(model  = "../experiments/model-guthrie.json",
                            task   = "../experiments/task-guthrie.json",
                            result = "data/test-experiment-guthrie.npy",
                            report = "data/test-experiment-guthrie.txt",
                            n_session = 25, n_block = 1, seed = 1,
                            rootdir=os.path.dirname(__file__))
    records = experiment.run(session, save=False, force=True, parse=False)
    records = np.squeeze(records)

    mean = np.mean(records["best"], axis=0)[-1]
    std  = np.std(records["best"], axis=0)[-1]
    print("Mean performance: %.2f ± %.2f" % (mean, std))
    print("-"*30)

    assert mean >= 0.85
コード例 #28
0
ファイル: state.py プロジェクト: beegica/smileDocs
def _ParallelWithPrevious(name=None, parallel_name=None, blocking=True):
    # get the exp reference
    from experiment import Experiment
    try:
        exp = Experiment._last_instance()
    except AttributeError:
        raise StateConstructionError(
            "You must first instantiate an Experiment.")

    # find the parent
    parent = exp._parents[-1]

    # find the previous child state (-1 because this is not a state)
    try:
        prev_state = parent._children[-1]
        parallel_parent = parent
    except IndexError:
        prev_state = parent
        if parent._parent is None:
            parallel_parent = parent._exp
        else:
            parallel_parent = parent._parent

    # build the new Parallel state
    with Parallel(name=parallel_name, parent=parallel_parent,
                  blocking=blocking) as p:
        p.override_instantiation_context(3)
        p.claim_child(prev_state)
        with Serial(name=name) as s:
            s.override_instantiation_context(3)
            yield p
コード例 #29
0
ファイル: state.py プロジェクト: eweich/smile
    def enter(self):
        # get the starting time from the parent
        self.state_time = self.get_parent_state_time()
        self.start_time = self.state_time

        # add the callback to the schedule
        delay = self.state_time - now()
        if delay < 0 or issubclass(self.__class__,RunOnEnter):
            # parents states (and states like Logging) run immediately
            delay = 0
        if self.interval < 0:
            # schedule it for every event loop
            schedule_delayed(self.callback, delay)
        else:
            # schedule the interval (0 means once)
            schedule_delayed_interval(self.callback, delay, self.interval)

        # say we're active
        self.active = True

        # if we don't have the exp reference, get it now
        if self.exp is None:
            from experiment import Experiment
            self.exp = Experiment.last_instance()
            
        # custom enter code
        self._enter()

        # update the parent time if necessary
        # moved to after _enter in case we update duration
        if self.duration > 0:
            self.advance_parent_state_time(self.duration)

        pass
コード例 #30
0
ファイル: state.py プロジェクト: beegica/smileDocs
def Else(name="ELSE BODY"):
    """State to attach to the else of an If state.

    """
    # get the exp reference
    from experiment import Experiment
    try:
        exp = Experiment._last_instance()
    except AttributeError:
        raise StateConstructionError(
            "You must first instantiate an Experiment.")

    # find the parent
    parent = exp._parents[-1]

    # find the previous child state (-1 because this is not a state)
    try:
        prev_state = parent._children[-1]
    except IndexError:
        raise StateConstructionError("No previous state for Else.")
    
    # make sure it's the If
    if not isinstance(prev_state, If):
        raise StateConstructionError(
            "The previous state must be an If or Elif state.")

    # return the false_state (the last out_state)
    false_state = prev_state._out_states[-1]
    false_state._name = name
    false_state.override_instantiation_context()
    return false_state
コード例 #31
0
# Create a list of columns that won't be used in prediction to drop
non_features = [
    'cv_credito', 'coloniaid', 'colonia_num_abd', 'nom_mun', 'mun_region',
    'mun_geo_zone', 'year_granted', 'cve', 'abandoned', 'abandoned_y',
    'abandoned_ever_y', 'abandon_year', 'abandon_month', 'cur_year', 'past',
    'regimen', 'loan_has_subsudy', 'loan_voluntary_contrib_bool',
    'personal_gender', 'personal_married', 'loan_building_type'
]

# list of features to not convert to quantiles since we are converting all
# other features
non_quantize_features = ['years_since_granted']

# Create the experiment object
exp = Experiment()

# Specify the HDF5 file to load data from
exp.hdf_file = '/mnt/scratch/master_loan_features_v41.comp.h5'
# Specify the years to load
exp.load_data_hdf(train_range=(2008, 2014),
                  test_range=(2014, 2015),
                  past=True,
                  all_years=True)

# Drop non-feature columns by name
exp.drop_cols(non_features)

# Drop all columns that have any NA's in them
# Can also pass 'row' to drop all rows with NA's in them
exp.drop_nas('row')
コード例 #32
0
            cnf.one_x_row
        ],
        "y": [
            cnf.one_digit_per_cell,
            cnf.one_y_row
        ],
        "z": [
            cnf.one_digit_per_cell,
            cnf.one_z_row
        ],
        "block": [
            cnf.one_digit_per_cell,
            cnf.block_constraint
        ]
    }

    for experiment_name, constraints in experiments.items():
        experiment = Experiment(9, "Image Generation", constraints,
                                "./puzzles/", PycoSatSolver(), None, "./results/")
        sudoku, statistics = experiment.run_one(sudoku, givens)

        viz_cube.save_slices(sudoku, experiment_name, "tests")

        print(statistics)

    viz_cube.plot_slice(sudoku, 0, 1, save_fig=os.path.join(target_directory, "example_orientation_0.png"))
    viz_cube.plot_slice(sudoku, 1, 1, save_fig=os.path.join(target_directory, "example_orientation_1.png"))
    viz_cube.plot_slice(sudoku, 2, 1, save_fig=os.path.join(target_directory, "example_orientation_2.png"))


コード例 #33
0
ファイル: do_experiment.py プロジェクト: dkassler/fcor-sim2
from __future__ import print_function, division
import argparse
from experiment import Experiment

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--exp-name',
                        type=str,
                        required=True,
                        help='the name of the json experiment file to run')
    parser.add_argument(
        '-overwrite',
        action='store_true',
        default=False,
        help='forces submission of the relevant jobs even if results \
                    already appear to exist.')
    parser.add_argument('-debug',
                        action='store_true',
                        default=False,
                        help='prevents actual submission of the jobs')
    args = parser.parse_args()

    print('loading experiment', args.exp_name)
    exp = Experiment(args.exp_name)

    print('submitting runs')
    for s in exp.simulations:
        for e in exp.estimators_and_truth:
            e.submit_runs(s, overwrite=args.overwrite, debug=args.debug)
コード例 #34
0
ファイル: run_mlp_experiments.py プロジェクト: 2ez4ai/iddl
    model_list.append(BaseMlp(input_dim, output_dim, layers3_1, nn.Linear, name='MLP-3-1').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_2, nn.Linear, name='MLP-3-2').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_3, nn.Linear, name='MLP-3-3').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_4, nn.Linear, name='MLP-3-4').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_1, BibdLinear, name='B-MLP-3-1').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_2, BibdLinear, name='B-MLP-3-2').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_3, BibdLinear, name='B-MLP-3-3').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_4, BibdLinear, name='B-MLP-3-4').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_1, RandomSparseLinear, name='R-MLP-3-1').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_2, RandomSparseLinear, name='R-MLP-3-2').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_3, RandomSparseLinear, name='R-MLP-3-3').to(device))
    model_list.append(BaseMlp(input_dim, output_dim, layers3_4, RandomSparseLinear, name='R-MLP-3-4').to(device))
else:
    print('CUDA is not available. Stopped.')
print('model_list: ')
for model in model_list:
    print('   {}'.format(model.name))


experiment = Experiment(n_epoch=10)
for model in model_list:
    experiment.run_model(model, train_loader, validation_loader)


# Save all the experiment data
filename = 'mlp_experiments_{}.p'.format(date_time)
pickle.dump(experiment, open(filename, "wb"))
print('The Experiment instance experiment dumped to the file: {}'.format(filename))

print('MLP experiments completed at {}'.format(datetime.now().strftime("%Y%m%d_%H%M%S")))
コード例 #35
0
        exp.model.process(exp.task, trial, model = exp.model)
    records[1] = exp.task.records

    # Day 1 : GPi OFF
    exp.model["GPi:cog → THL:cog"].gain = 0
    exp.model["GPi:mot → THL:mot"].gain = 0
    for trial in exp.task:
        exp.model.process(exp.task, trial, model = exp.model)
    records[2] = exp.task.records
        
    return records


experiment = Experiment(model = "model-topalidou.json",
                        task = "task-topalidou.json",
                        result = "data/experiment-topalidou-protocol-2-new.npy",
                        report = "data/experiment-topalidou-protocol-2-new.txt",
                        n_session = 25, n_block = 3, seed = 533)
records = experiment.run(session, "Protocol 2")


# Save performance (one column per session)
# -----------------------------------------------------------------------------
# P = np.squeeze(records["best"][:,0])
# np.savetxt("data/experiment-topalidou-protocol-2-D1-P.csv", P.T, fmt="%d", delimiter=",")
# P = np.squeeze(records["best"][:,1])
# np.savetxt("data/experiment-topalidou-protocol-2-D2-P.csv", P.T, fmt="%d", delimiter=",")
# P = np.squeeze(records["best"][:,2])
# np.savetxt("data/experiment-topalidou-protocol-2-D3-P.csv", P.T, fmt="%d", delimiter=",")
# P = np.squeeze(records["RT"][:,0])
# np.savetxt("data/experiment-topalidou-protocol-2-D1-RT.csv", P.T, fmt="%.4f", delimiter=",")
コード例 #36
0
ファイル: main.py プロジェクト: isaachenrion/phone-a-friend
def main():
    '''LOAD FRIENDS'''
    active_sensors = [None] * args.batch_size
    subordinates_batch = [None] * args.batch_size
    for idx in range(args.batch_size):
        subordinates = {}
        if args.use_subordinates:
            model_strs = []
            model_strs.append('Mar-27___12-12-19-RandomPear-recurrent') # BAD PEAR
            #model_strs.append('Mar-24___16-14-48-RandomPear-recurrent') # GOOD PEAR
            for s in model_strs:
                subordinates[s] = Subordinate(s)
                if idx == 0:
                    print("Loaded subordinate: {}".format(s))
        num_subordinates = len(subordinates)
        if num_subordinates > 0:
            subordinates_batch[idx] = subordinates

        policy_sensors = []
        if args.use_policy_sensors:
            model_strs = []
            #model_strs.append('Mar-23___14-33-50-RandomPear-recurrent') # BAD PEAR
            model_strs.append('Mar-24___16-14-48-RandomPear-recurrent') # GOOD PEAR
            for s in model_strs:
                policy_sensors.append(PolicySensor(s))
                if idx == 0:
                    print("Loaded policy sensor: {}".format(s))

        reward_sensors = []
        if args.use_reward_sensors:
            reward_strs = ['pear', 'orange', 'apple']
            for s in reward_strs:
                reward_sensors.append(RewardSensor(s))
                if idx == 0:
                    print("Loaded reward sensor: {}".format(s))

        if idx == 0:
            num_active_sensors = len(policy_sensors) + len(reward_sensors)
            print("Total number of active sensors: {}".format(num_active_sensors))
        if num_active_sensors > 0:
            active_sensors[idx] = policy_sensors + reward_sensors

    ''' LOAD ENVIRONMENTS '''
    print("Creating %d environments" % args.batch_size)
    from environments.maze.env_list import ENVS
    Env = ENVS[args.env]
    print("Environment: {}".format(Env.__name__))

    #allowed_actions = range(MC.NUM_BASIC_ACTIONS + num_active_sensors + num_subordinates)
    policy_dict = {}
    model_strs = []
    #model_strs.append('Mar-23___14-33-50-RandomPear-recurrent') # BAD PEAR
    #model_strs.append('Mar-27___14-26-20-RandomPear-recurrent') # GOOD PEAR

    for s in model_strs:
        print("Loading model: {}".format(s))
        filename = os.path.join(C.WORKING_DIR, 'experiments', s, s + '.ckpt')
        model = RecurrentModel(1, torch.load(filename), filename)
        policy_dict[s] = DiscreteModelPolicy(model, stochastic=True, baseline_model=None, batch_size=args.batch_size)

    envs=[Env(active_sensors=active_sensors[idx], subordinates=model_strs, seed=idx) for idx in range(args.batch_size)]

    A = envs[0].action_space.n
    action_types = envs[0].world.agent.action_types
    goal_state = envs[0].reward.goal_state
    try:
        E = sum([sensor.shape for sensor in envs[0].world.agent.sensors])

    except TypeError:
        E = None

    print("Total sensor size: {}".format(E))
    state_size_dict = envs[0].world.state_size_dict

    print("Number of Actions is: {}".format(A))

    ''' BUILD MODEL '''

    if args.multiprocessing:
        bs = 1
    else: bs = args.batch_size

    baseline_net = RecurrentNet2(input_size_dict=state_size_dict, hidden_size=20, action_types=["value"], softmax=False, bn=args.bn, wn=args.wn)
    baseline_model = RecurrentModel(bs, baseline_net)
    #baseline_model= None

    _action_net = RecurrentNet2(hidden_size=50, input_size_dict=state_size_dict, action_types=action_types, goal_state=goal_state, bn=args.bn, wn=args.wn)
    _action_model = RecurrentModel(bs, _action_net)


    if args.load is not None:
        print("Loading model: {}".format(args.load))
        filename = os.path.join(C.WORKING_DIR, 'experiments', args.load, args.load + '.ckpt')
        _action_net = torch.load(filename)

        _action_model = RecurrentModel(1, _action_net, filename)

    if MC.EXPERIMENTAL and False:
        policy = DiscreteModelPolicy(_action_model, stochastic=True, baseline_model=baseline_model)
        policy_dict["main_policy"] = policy
        policy = CollectionOfPolicies(policy_dict, envs)
    else:
        action_model = _action_model
        policy = DiscreteModelPolicy(action_model, stochastic=True, baseline_model=baseline_model)


    print("Action model: {}".format(_action_net))
    print("Baseline model: {}".format(baseline_net))

    optimizer= optim.Adam(policy.parameters(), lr=args.lr)
    #optimizer= optim.RMSprop(policy.parameters(), lr=args.lr)

    ''' RUN EXPERIMENT '''
    print("Running experiment...")
    print("There are {} CPUs on this machine".format(multiprocessing.cpu_count()))
    experiment = Experiment(policy, optimizer, envs, n_train_steps=1000, eval_freq=10, save_freq=50, args=args)
    experiment._run()
コード例 #37
0
                   env_name=MergeEnv,
                   network=network,
                   simulator='traci',
                   sim=sim_params,
                   env=env_params,
                   net=net_params,
                   veh=vehicles,
                   initial=initial_config)
# # number of time steps
flow_params['env'].horizon = 2000

############ EXPERIMENTS ##############
if TEST_SETTINGS:
    print("this is the test for the environment")
    from experiment import Experiment
    exp = Experiment(flow_params)

    # run the sumo simulation
    exp.run(num_runs=3,
            num_cav=(NUM_MERGE_0 + NUM_MERGE_1),
            num_human=actual_num_human)
elif RAY_RL:
    from ray_rl_setting import *
else:
    from rl_experiments import Experiment

    exp = Experiment(flow_params)
    # run the sumo simulation
    exp.run(num_runs=10,training=TRAINING, \
            num_human=NUM_HUMAN, \
            actual_num_human = actual_num_human,\
コード例 #38
0
from sklearn import svm

# Add pipeline_src to include path (NOTE: this is a hardcoded path... will fail if you move dirs)
path = "../pipeline_src/"
if not path in sys.path:
    sys.path.insert(1, path)
del path

from experiment import Experiment

############################################################################
# Pipeline: Loading & Preprocessing
############################################################################

# Instantiate an experiment
exp = Experiment()

# Load data & specify train and test ranges
# can also specify a list of columns to load with load_list=[]
# or a list of columns to drop with drop_list=[]
exp.load_data(load_list = [],
              drop_list = ['abandoned', 'cur_year', 'past'],
              train_range=(2008,2012), 
              test_range=(2012, 2013), 
              train_current=False,
              test_current=False,
              fast=False,
              all_years=False,
              head=True)

# Create a list of columns that won't be used in prediction to drop
コード例 #39
0
def test_probability_of_hh():
    e = Experiment()
    p = e.flip_coin_2x_n_times(100_000)
    assert p == approx(0.25, rel=0.01)
コード例 #40
0
ファイル: main.py プロジェクト: scott0002/DRUM
def main():
    parser = argparse.ArgumentParser(description="Experiment setup")
    # misc
    parser.add_argument('--seed', default=33, type=int)
    parser.add_argument('--gpu', default="", type=str)
    parser.add_argument('--no_train', default=False, action="store_true")
    parser.add_argument('--from_model_ckpt', default=None, type=str)
    parser.add_argument('--rule_thr', default=1e-2, type=float)
    parser.add_argument('--no_preds', default=False, action="store_true")
    parser.add_argument('--get_vocab_embed',
                        default=False,
                        action="store_true")
    parser.add_argument('--exps_dir', default=None, type=str)
    parser.add_argument('--exp_name', default=None, type=str)
    # data property
    parser.add_argument('--datadir', default=None, type=str)
    parser.add_argument('--resplit', default=False, action="store_true")
    parser.add_argument('--no_link_percent', default=0., type=float)
    parser.add_argument('--type_check', default=False, action="store_true")
    parser.add_argument('--domain_size', default=128, type=int)
    parser.add_argument('--no_extra_facts', default=False, action="store_true")
    parser.add_argument('--query_is_language',
                        default=False,
                        action="store_true")
    parser.add_argument('--vocab_embed_size', default=128, type=int)
    # model architecture
    parser.add_argument('--num_step', default=3, type=int)
    parser.add_argument('--num_layer', default=1, type=int)
    parser.add_argument('--rank', default=3, type=int)
    parser.add_argument('--rnn_state_size', default=128, type=int)
    parser.add_argument('--query_embed_size', default=128, type=int)
    # optimization
    parser.add_argument('--batch_size', default=64, type=int)
    parser.add_argument('--print_per_batch', default=3, type=int)
    parser.add_argument('--max_epoch', default=10, type=int)
    parser.add_argument('--min_epoch', default=5, type=int)
    parser.add_argument('--learning_rate', default=0.001, type=float)
    parser.add_argument('--no_norm', default=False, action="store_true")
    parser.add_argument('--thr', default=1e-20, type=float)
    parser.add_argument('--dropout', default=0., type=float)
    # evaluation
    parser.add_argument('--get_phead', default=False, action="store_true")
    parser.add_argument('--adv_rank', default=False, action="store_true")
    parser.add_argument('--rand_break', default=False, action="store_true")
    parser.add_argument('--accuracy', default=False, action="store_true")
    parser.add_argument('--top_k', default=10, type=int)

    d = vars(parser.parse_args())
    option = Option(d)
    if option.exp_name is None:
        option.tag = time.strftime("%y-%m-%d-%H-%M")
    else:
        option.tag = option.exp_name
    if option.resplit:
        assert not option.no_extra_facts
    if option.accuracy:
        assert option.top_k == 1

    os.environ["CUDA_VISIBLE_DEVICES"] = option.gpu
    tf.logging.set_verbosity(tf.logging.ERROR)

    if not option.query_is_language:
        data = Data(option.datadir, option.seed, option.type_check,
                    option.domain_size, option.no_extra_facts)
    else:
        data = DataPlus(option.datadir, option.seed)
    print("Data prepared.")

    option.num_entity = data.num_entity
    option.num_operator = data.num_operator
    if not option.query_is_language:
        option.num_query = data.num_query
    else:
        option.num_vocab = data.num_vocab
        option.num_word = data.num_word  # the number of words in each query

    option.this_expsdir = os.path.join(option.exps_dir, option.tag)
    if not os.path.exists(option.this_expsdir):
        os.makedirs(option.this_expsdir)
    option.ckpt_dir = os.path.join(option.this_expsdir, "ckpt")
    if not os.path.exists(option.ckpt_dir):
        os.makedirs(option.ckpt_dir)
    option.model_path = os.path.join(option.ckpt_dir, "model")

    option.save()
    print("Option saved.")

    learner = Learner(option)
    print("Learner built.")

    saver = tf.train.Saver(max_to_keep=option.max_epoch)
    saver = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = False
    config.log_device_placement = False
    config.allow_soft_placement = True
    with tf.Session(config=config) as sess:
        tf.set_random_seed(option.seed)
        sess.run(tf.global_variables_initializer())
        print("Session initialized.")

        if option.from_model_ckpt is not None:
            saver.restore(sess, option.from_model_ckpt)
            print("Checkpoint restored from model %s" % option.from_model_ckpt)

        data.reset(option.batch_size)
        experiment = Experiment(sess, saver, option, learner, data)
        print("Experiment created.")

        if not option.no_train:
            print("Start training...")
            experiment.train()

        if not option.no_preds:
            print("Start getting test predictions...")
            experiment.get_predictions()

        if option.get_vocab_embed:
            print("Start getting vocabulary embedding...")
            experiment.get_vocab_embedding()

    experiment.close_log_file()
    print("=" * 36 + "Finish" + "=" * 36)
コード例 #41
0
        rc.RumorCenter(),
        dc.DistanceCenter(),
        jc.JordanCenter(),
        ri.ReverseInfection(),
        di.DynamicImportance(),
        prior_detector8,
        gsba.GSBA(prior_detector1),
        # gsba_bao7.GSBA_coverage_7(prior_detector1),
        gsba_bao9.GSBA_coverage_9(prior_detector1),
        gsba_bao15.GSBA_coverage_15(prior_detector1)
    ]

    logger = log.Logger(logname='../data/main_small_world3000.log',
                        loglevel=logging.INFO,
                        logger="experiment").get_log()
    experiment = Experiment(methods, logger)
    experiment.propagation_model = 'SI'

    start_time = clock()
    print "Starting..."
    d = data.Graph("../data/small_world/swall-world-graph3000.txt", weighted=0)
    d.debug = False
    test_num = 100

    print 'Graph size: ', d.graph.number_of_nodes(), d.graph.number_of_edges()
    test_category = experiment.RANDOM_TEST
    experiment.start(d, test_category, test_num, 20, 350, 40)
    # test_category = experiment.FULL_TEST
    # experiment.start(d, test_category, test_num, 200, 400,100)

    end_time = clock()
コード例 #42
0
ファイル: run_default.py プロジェクト: aixioma/benchmarks-1
        from catboost_experiment import CABExperiment
        BstExperiment = CABExperiment

    learning_task = namespace.learning_task
    train_path = namespace.train
    test_path = namespace.test
    cd_path = namespace.cd
    n_estimators = namespace.n_estimators
    output_folder_path = os.path.join(namespace.output_folder_path, '')
    max_hyperopt_evals = namespace.hyperopt_evals

    print 'Loading and preprocessing dataset...'

    X_train, y_train, X_test, y_test, cat_cols = Experiment(
        learning_task,
        train_path=train_path,
        test_path=test_path,
        cd_path=cd_path).read_data()

    bst_experiment = BstExperiment(learning_task,
                                   train_path=train_path,
                                   test_path=test_path,
                                   cd_path=cd_path,
                                   max_hyperopt_evals=max_hyperopt_evals,
                                   n_estimators=n_estimators)

    cv_pairs, (dtrain,
               dtest) = bst_experiment.split_and_preprocess(X_train.copy(),
                                                            y_train,
                                                            X_test.copy(),
                                                            y_test,
コード例 #43
0
def train(build_model, dataset, hparams, logdir, name, observer, tb_graph):

    # Check if the given directory already contains model
    if os.path.exists(f"{logdir}/stats.json"):
        # then we will load the model weights
        model_dir = logdir
    else:
        # otherwise, create
        # location to save the model -- <logdir>/<dataset>/<model>/<timestamp>
        model_dir = os.path.join(
            logdir, dataset.dataset_name, build_model.__name__,
            datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
        os.makedirs(model_dir, exist_ok=True)
    model_path = os.path.join(model_dir, "weights.h5")

    # Check if any observers are added for experiment.
    # If not, add a file_storage observer
    if observer is None:
        observer = f"file_storage={model_dir}"

    # Create an experiment
    ex = Experiment(name, dataset.dataset_name, build_model.__name__, hparams,
                    observer)

    # Main function to run for experiment
    @ex.main
    def train(_run):
        # Build model
        model = build_model(hparams, **dataset.preprocessing.kwargs)

        # Compile model
        model.compile(optimizer=optimizers.make_optimizer(
            hparams.optimizer, hparams.opt_param),
                      loss="categorical_crossentropy",
                      metrics=["categorical_accuracy"])

        # Print Summary of models
        lq.models.summary(model)

        # If the model already exists, load it and continue training
        initial_epoch = 0
        if os.path.exists(os.path.join(model_dir, "stats.json")):
            with open(os.path.join(model_dir, "stats.json"),
                      "r") as stats_file:
                initial_epoch = json.load(stats_file)["epoch"]
                click.echo(
                    f"Restoring model from {model_path} at epoch = {initial_epoch}"
                )
                model.load_weights(model_path)

        cb = [callbacks.SaveStats(model_dir=model_dir)]

        if tb_graph:
            # If tensorboard logging is enabled, write graph
            cb.extend([
                tf.keras.callbacks.TensorBoard(
                    log_dir=os.path.join(model_dir, "tb"),
                    write_graph=tb_graph,
                    histogram_freq=0,
                    update_freq='epoch',
                    # update_freq=0,
                    profile_batch=0,
                    embeddings_freq=0),
            ])

        # Callback for sending data to Sacred Experiment
        cb.extend([
            tf.keras.callbacks.LambdaCallback(
                on_epoch_end=lambda epoch, logs: [
                    ex.log_scalar(metric, value, epoch + 1)
                    for (metric, value) in logs.items()
                ])
        ])

        # Train this mode
        train_log = model.fit(
            dataset.train_data(hparams.batch_size),
            epochs=hparams.epochs,
            steps_per_epoch=dataset.train_examples // hparams.batch_size,
            validation_data=dataset.validation_data(hparams.batch_size),
            validation_steps=dataset.validation_examples // hparams.batch_size,
            initial_epoch=initial_epoch,
            callbacks=cb)

    # Execute the experiment
    ex.execute()
コード例 #44
0
class Ui_MainWindow(object):
    def createUiElement(self,
                        type,
                        name,
                        parent=None,
                        font=None,
                        alignment=QtCore.Qt.AlignRight
                        | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter,
                        layoutDirection=QtCore.Qt.LeftToRight,
                        maximumSize=None,
                        minimumSize=None,
                        fixedSize=None,
                        isHidden=False,
                        value="",
                        items=[],
                        margins=None):
        if type == ElementType.Label:
            uiElement = QtWidgets.QLabel(parent)
            uiElement.setObjectName(name)
            uiElement.setText(value)
            if alignment:
                uiElement.setAlignment(alignment)
            if font:
                uiElement.setFont(font)
        elif type == ElementType.Value:
            uiElement = QtWidgets.QLineEdit(parent)
            uiElement.setLayoutDirection(layoutDirection)
            uiElement.setText(value)
        elif type == ElementType.Form:
            uiElement = QtWidgets.QFormLayout()
            uiElement.setAlignment(alignment)
            if margins:
                uiElement.setContentsMargins(*margins)
        elif type == ElementType.VBox:
            if parent:
                uiElement = QtWidgets.QVBoxLayout(parent)
            else:
                uiElement = QtWidgets.QVBoxLayout()
            if margins:
                uiElement.setContentsMargins(*margins)
        elif type == ElementType.HBox:
            uiElement = QtWidgets.QHBoxLayout()
        elif type == ElementType.ComboBox:
            uiElement = QtWidgets.QComboBox(parent)
            for item in items:
                uiElement.addItem(item)
        elif type == ElementType.CheckBox:
            uiElement = QtWidgets.QCheckBox(parent)
            uiElement.setLayoutDirection(layoutDirection)
        elif type == ElementType.Button:
            uiElement = QtWidgets.QPushButton(parent)
        else:
            raise AttributeError("Invalid UI element type")

        if maximumSize:
            uiElement.setMaximumSize(maximumSize)
        if minimumSize:
            uiElement.setMinimumSize(minimumSize)
        if fixedSize:
            uiElement.setFixedSize(*fixedSize)
        if isHidden:
            uiElement.setHidden(True)

        uiElement.setObjectName(name)

        return uiElement

    def setupUi(self, mainWindow):
        self.mainWindow = mainWindow
        self.mainWindow.setObjectName("mainWindow")

        self.translate = QtCore.QCoreApplication.translate
        self.threadpool = QtCore.QThreadPool()
        self.models = Models()

        self.statusBar = QtWidgets.QStatusBar(mainWindow)
        self.statusBar.setObjectName("statusBar")
        self.statusBar.clearMessage()
        self.mainWindow.setStatusBar(self.statusBar)

        self.menuBar = QtWidgets.QMenuBar(mainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 680, 20))
        self.menuBar.setObjectName("menuBar")
        self.mainWindow.setMenuBar(self.menuBar)

        self.loadNewModelMenu = QtWidgets.QMenu(self.menuBar)
        self.loadNewModelMenu.setObjectName("loadNewModelMenu")

        self.actionLoadExecutedModel = QtWidgets.QAction(
            "actionLoadExecutedModel", mainWindow)
        self.actionLoadImaginedModel = QtWidgets.QAction(
            "actionLoadImaginedModel", mainWindow)
        self.addActions(
            self.loadNewModelMenu,
            [self.actionLoadExecutedModel, self.actionLoadImaginedModel])
        self.addActions(self.menuBar, [self.loadNewModelMenu.menuAction()])

        self.actionLoadExecutedModel.triggered.connect(
            lambda: self.loadModel(ModelType.Executed))
        self.actionLoadImaginedModel.triggered.connect(
            lambda: self.loadModel(ModelType.Imagined))

        self.retranslateUi()
        self.defineEvents(mainWindow)

        self.setupSettingsViewUi()

    def loadModel(self, model_type):
        if model_type == ModelType.Executed:
            model_path, _ = QtWidgets.QFileDialog.getOpenFileName(
                self.mainWindow, 'Load executed model', os.getcwd(),
                "Model files (*.h5)")
        elif model_type == ModelType.Imagined:
            model_path, _ = QtWidgets.QFileDialog.getOpenFileName(
                self.mainWindow, 'Load imagined model', os.getcwd(),
                "Model files (*.h5)")
        else:
            raise Exception("Invalid model type")

        if model_path == '':
            return

        model_name = model_path[model_path.rfind('/') +
                                len('/'):model_path.rfind('.h5')]
        try:
            self.models.add_model(model_name, model_type, model_path)
            self.displayNewModelNames(self.runTypeComboBox.currentIndex())
        except AttributeError:
            self.displayError("Model with that name is already loaded")
        except OSError:
            self.displayError("Model loading failed")

    def retranslateUi(self):
        self.loadNewModelMenu.setTitle(
            self.translate("mainWindow", "Load new model"))
        self.actionLoadExecutedModel.setText(
            self.translate("mainWindow", "Load executed task model"))
        self.actionLoadImaginedModel.setText(
            self.translate("mainWindow", "Load imagined task model"))

    def toggleActionsEnabled(self, value):
        self.actionLoadExecutedModel.setEnabled(value)
        self.actionLoadImaginedModel.setEnabled(value)

    def setupSettingsViewUi(self):
        self.nrOfTasks = None
        self.subjectId = None
        self.runType = None
        self.transferLearningEnabled = None
        self.selectedModelName = None
        self.debugMode = None
        self.showingPredictions = None
        self.evaluateAllModels = None
        self.view = ViewType.Settings
        self.toggleActionsEnabled(True)

        self.mainWindow.setFixedSize(QtCore.QSize(340, 340))
        self.centerOnScreen()

        self.settingsWidget = QtWidgets.QWidget(self.mainWindow)
        self.settingsWidget.setMaximumSize(QtCore.QSize(340, 340))
        self.settingsWidget.setObjectName("settingsWidget")
        self.mainWindow.setCentralWidget(self.settingsWidget)

        self.settingsLayout = self.createUiElement(ElementType.VBox,
                                                   "settingsLayout",
                                                   parent=self.settingsWidget,
                                                   margins=(10, 10, 10, 10))

        self.settingsForm = self.createUiElement(
            ElementType.Form,
            "settingsForm",
            parent=self.settingsWidget,
            alignment=QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter,
            margins=(10, 10, 10, 10))
        self.settingsLayout.addLayout(self.settingsForm)

        self.settingsLabel = self.createUiElement(
            ElementType.Label,
            "settingsLabel",
            parent=self.settingsWidget,
            font=QtGui.QFont("Times", 14, QtGui.QFont.Bold),
            alignment=QtCore.Qt.AlignVCenter)
        self.settingsForm.addRow(self.settingsLabel)

        self.subjectIdLabel = self.createUiElement(ElementType.Label,
                                                   "subjectIdLabel",
                                                   parent=self.settingsWidget)
        self.subjectIdValue = self.createUiElement(ElementType.Value,
                                                   "subjectIdValue",
                                                   parent=self.settingsWidget,
                                                   maximumSize=QtCore.QSize(
                                                       170, 50),
                                                   value="1")
        self.settingsForm.addRow(self.subjectIdLabel, self.subjectIdValue)

        self.nrOfTasksLabel = self.createUiElement(ElementType.Label,
                                                   "nrOfTasksLabel",
                                                   parent=self.settingsWidget)
        self.nrOfTasksValue = self.createUiElement(ElementType.Value,
                                                   "nrOfTasksValue",
                                                   parent=self.settingsWidget,
                                                   maximumSize=QtCore.QSize(
                                                       170, 50),
                                                   value="16")
        self.settingsForm.addRow(self.nrOfTasksLabel, self.nrOfTasksValue)

        self.runTypeComboBoxLabel = self.createUiElement(
            ElementType.Label,
            "runTypeComboBoxLabel",
            parent=self.settingsWidget,
        )
        self.runTypeComboBox = self.createUiElement(
            ElementType.ComboBox,
            "runTypeComboBox",
            parent=self.settingsWidget,
            maximumSize=QtCore.QSize(170, 50),
            items=["Executed tasks", "Imagined tasks"])
        self.runTypeComboBox.currentIndexChanged.connect(
            self.displayNewModelNames)
        self.settingsForm.addRow(self.runTypeComboBoxLabel,
                                 self.runTypeComboBox)

        modelNames = [
            model.get_name()
            for model in self.models.get_models(ModelType.Executed)
        ]
        self.modelComboBoxLabel = self.createUiElement(
            ElementType.Label,
            "modelComboBoxLabel",
            parent=self.settingsWidget,
        )
        self.modelComboBox = self.createUiElement(ElementType.ComboBox,
                                                  "modelComboBox",
                                                  parent=self.settingsWidget,
                                                  maximumSize=QtCore.QSize(
                                                      170, 50),
                                                  items=modelNames)
        self.modelComboBox.currentIndexChanged.connect(
            self.toggleTransferLearningCheckboxEnabled)
        self.settingsForm.addRow(self.modelComboBoxLabel, self.modelComboBox)

        self.showPredictionsCheckboxLabel = self.createUiElement(
            ElementType.Label,
            "showPredictionsCheckboxLabel",
            parent=self.settingsWidget)
        self.showPredictionsCheckbox = self.createUiElement(
            ElementType.CheckBox,
            "showPredictionsCheckbox",
            parent=self.settingsWidget)
        self.settingsForm.addRow(self.showPredictionsCheckboxLabel,
                                 self.showPredictionsCheckbox)

        self.transferLearningCheckboxLabel = self.createUiElement(
            ElementType.Label,
            "transferLearningCheckboxLabel",
            parent=self.settingsWidget)
        self.transferLearningCheckbox = self.createUiElement(
            ElementType.CheckBox,
            "transferLearningCheckboxLabel",
            parent=self.settingsWidget)
        self.settingsForm.addRow(self.transferLearningCheckboxLabel,
                                 self.transferLearningCheckbox)

        self.evaluateAllModelsLabel = self.createUiElement(
            ElementType.Label,
            "evaluateAllModelsLabel",
            parent=self.settingsWidget)
        self.evaluateAllModelsCheckBox = self.createUiElement(
            ElementType.CheckBox,
            "evaluateAllModelsCheckBox",
            parent=self.settingsWidget)
        self.settingsForm.addRow(self.evaluateAllModelsLabel,
                                 self.evaluateAllModelsCheckBox)

        self.debugModeCheckboxLabel = self.createUiElement(
            ElementType.Label, "debugModeLabel", parent=self.settingsWidget)
        self.debugModeCheckbox = self.createUiElement(
            ElementType.CheckBox,
            "debugModeCheckbox",
            parent=self.settingsWidget)
        self.settingsForm.addRow(self.debugModeCheckboxLabel,
                                 self.debugModeCheckbox)

        self.startButton = self.createUiElement(ElementType.Button,
                                                "startButton",
                                                parent=self.settingsWidget,
                                                fixedSize=(320, 40))
        self.settingsLayout.addWidget(self.startButton)
        self.startButton.clicked.connect(self.startExperiment)

        self.retranslateSettingsView()

    def displayNewModelNames(self, selectionIndex):
        self.modelComboBox.clear()
        model_type = ModelType.Executed if selectionIndex == 0 else ModelType.Imagined
        self.modelComboBox.addItems(
            [model.get_name() for model in self.models.get_models(model_type)])

    def toggleTransferLearningCheckboxEnabled(self, selectionIndex):
        if any(tl_model in self.modelComboBox.currentText() for tl_model in
               ['EEGNet', 'EEGNet_Fusion', 'ShallowConvNet', 'DeepConvNet']):
            self.transferLearningCheckbox.setEnabled(True)
        else:
            self.transferLearningCheckbox.setChecked(False)
            self.transferLearningCheckbox.setEnabled(False)

    def retranslateSettingsView(self):
        self.mainWindow.setWindowTitle(
            self.translate("mainWindow", "BCI Application v1.0"))
        self.startButton.setText(self.translate("mainWindow", "Start"))
        self.settingsLabel.setText(self.translate("mainWindow", "Settings"))
        self.subjectIdLabel.setText(
            self.translate("mainWindow", "Current subject ID:"))
        self.modelComboBoxLabel.setText(
            self.translate("mainWindow", "Classifier model:"))
        self.transferLearningCheckboxLabel.setText(
            self.translate("mainWindow", "Enable transfer learning:"))
        self.showPredictionsCheckboxLabel.setText(
            self.translate("mainWindow", "Show predictions:"))
        self.evaluateAllModelsLabel.setText(
            self.translate("mainWindow", "Evaluate all models:"))
        self.debugModeCheckboxLabel.setText(
            self.translate("mainWindow", "Enable debug mode:"))
        self.nrOfTasksLabel.setText(
            self.translate("mainWindow", "Number of tasks to perform:"))
        self.runTypeComboBoxLabel.setText(
            self.translate("mainWindow", "Run type:"))

    def setupExperimentViewUi(self):
        self.mainWindow.setFixedSize(QtCore.QSize(1024, 768))
        self.centerOnScreen()
        self.view = ViewType.Experiment
        self.toggleActionsEnabled(False)

        self.experimentWidget = QtWidgets.QWidget(mainWindow)
        self.experimentWidget.setMaximumSize(QtCore.QSize(1024, 768))
        self.experimentWidget.setObjectName("experimentWidget")
        self.mainWindow.setCentralWidget(self.experimentWidget)

        self.experimentLayout = self.createUiElement(
            ElementType.VBox,
            "experimentLayout",
            parent=self.experimentWidget,
            margins=(10, 10, 10, 10))

        self.experimentLabel = self.createUiElement(
            ElementType.Label,
            "experimentLabel",
            parent=self.experimentWidget,
            font=QtGui.QFont("Roboto", 16, QtGui.QFont.Bold),
            alignment=QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter,
            margins=(50, 0, 0, 0))
        self.updateExperimentLabel(LabelType.Rest)
        self.experimentLayout.addWidget(self.experimentLabel)

        self.graphicsLayout = self.createUiElement(
            ElementType.HBox, "graphicsLayout", parent=self.experimentWidget)
        self.experimentLayout.addLayout(self.graphicsLayout)

        self.experimentGraphic = self.createUiElement(
            ElementType.Label,
            "experimentGraphic",
            parent=self.experimentWidget)
        self.graphicsLayout.addWidget(self.experimentGraphic)

        self.stopButton = self.createUiElement(ElementType.Button,
                                               "stopButton",
                                               parent=self.experimentWidget,
                                               fixedSize=(1004, 40))
        self.stopButton.setEnabled(False)
        self.experimentLayout.addWidget(self.stopButton)
        self.stopButton.clicked.connect(self.experiment.stop)

        self.taskNrLabel = self.createUiElement(ElementType.Label,
                                                "taskNrLabel")
        self.taskNr = 0

        self.updateGraphics(GraphicType.Rest)  # show rest state graphics

        self.retranslateExperimentView('executed' if self.runType ==
                                       0 else 'imagined')

        self.statusBar.showMessage(self.taskNrLabel.text() +
                                   str(self.taskNr))  # update status bar

    def retranslateExperimentView(self, run_type):
        self.stopButton.setText(self.translate("mainWindow", "Stop"))
        self.taskNrLabel.setText(
            self.translate("mainWindow",
                           "Running {} task number: ".format(run_type)))

    def centerOnScreen(self):
        resolution = QtWidgets.QDesktopWidget().screenGeometry(
        )  # TODO: QDesktopWidget deprecated
        self.mainWindow.move((resolution.width() / 2) -
                             (self.mainWindow.frameSize().width() / 2),
                             (resolution.height() / 2) -
                             (self.mainWindow.frameSize().height() / 2))

    def updateTaskNr(self, new_nr):
        self.taskNr += 1
        self.statusBar.showMessage(self.taskNrLabel.text() + str(self.taskNr))

    def setupStatisticsViewUi(self):
        self.mainWindow.setFixedSize(QtCore.QSize(360, 360))
        self.centerOnScreen()
        self.view = ViewType.Statistics
        self.statistics = self.experiment.get_statistics()

        self.statisticsWidget = QtWidgets.QWidget(self.mainWindow)
        self.statisticsWidget.setMaximumSize(QtCore.QSize(360, 340))
        self.statisticsWidget.setObjectName("statisticsWidget")
        self.mainWindow.setCentralWidget(self.statisticsWidget)

        self.statisticsLayout = self.createUiElement(
            ElementType.VBox,
            "statisticsLayout",
            parent=self.statisticsWidget,
            margins=(10, 10, 10, 10))

        self.statisticsForm = self.createUiElement(
            ElementType.Form,
            "statisticsForm",
            parent=self.statisticsWidget,
            alignment=QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter,
            margins=(50, 10, 10, 10))
        self.statisticsLayout.addLayout(self.statisticsForm)

        self.statisticsFormLabel = self.createUiElement(
            ElementType.Label,
            "statisticsFormLabel",
            parent=self.statisticsWidget,
            font=QtGui.QFont("Times", 14, QtGui.QFont.Bold),
            alignment=QtCore.Qt.AlignVCenter)
        self.statisticsForm.addRow(self.statisticsFormLabel)

        modelType = ModelType.Executed if self.runType == 0 else ModelType.Imagined
        self.modelSelectionComboboxLabel = self.createUiElement(
            ElementType.Label,
            "modelSelectionComboboxLabel",
            parent=self.statisticsWidget)

        all_model_names = [
            model.get_name() for model in self.models.get_models(modelType)
        ]
        model_names = all_model_names if self.evaluateAllModels else [
            self.selectedModelName
        ]
        self.modelSelectionCombobox = self.createUiElement(
            ElementType.ComboBox,
            "modelSelectionCombobox",
            parent=self.statisticsWidget,
            maximumSize=QtCore.QSize(160, 50),
            items=model_names)
        self.statisticsForm.addRow(self.modelSelectionComboboxLabel,
                                   self.modelSelectionCombobox)
        self.modelSelectionCombobox.currentTextChanged.connect(
            self.displayNewStatistics)

        self.runTypeLabel = self.createUiElement(ElementType.Label,
                                                 "runTypeLabel",
                                                 parent=self.statisticsWidget)
        self.runTypeValue = self.createUiElement(ElementType.Label,
                                                 "runTypeValue",
                                                 parent=self.statisticsWidget,
                                                 alignment=None)
        self.statisticsForm.addRow(self.runTypeLabel, self.runTypeValue)

        self.transferLearningLabel = self.createUiElement(
            ElementType.Label,
            "transferLearningLabel",
            parent=self.statisticsWidget)
        self.transferLearningValue = self.createUiElement(
            ElementType.Label,
            "transferLearningValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.transferLearningLabel,
                                   self.transferLearningValue)

        self.leftHandPredictionsLabel = self.createUiElement(
            ElementType.Label,
            "leftHandPredictionsLabel",
            parent=self.statisticsWidget)
        self.leftHandPredictionsValue = self.createUiElement(
            ElementType.Label,
            "leftHandPredictionsValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.leftHandPredictionsLabel,
                                   self.leftHandPredictionsValue)

        self.rightHandPredictionsLabel = self.createUiElement(
            ElementType.Label,
            "rightHandPredictionsLabel",
            parent=self.statisticsWidget)
        self.rightHandPredictionsValue = self.createUiElement(
            ElementType.Label,
            "rightHandPredictionsValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.rightHandPredictionsLabel,
                                   self.rightHandPredictionsValue)

        self.correctPredictionsLabel = self.createUiElement(
            ElementType.Label,
            "correctPredictionsLabel",
            parent=self.statisticsWidget)
        self.correctPredictionsValue = self.createUiElement(
            ElementType.Label,
            "correctPredictionsValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.correctPredictionsLabel,
                                   self.correctPredictionsValue)

        self.incorrectPredictionsLabel = self.createUiElement(
            ElementType.Label,
            "incorrectPredictionsLabel",
            parent=self.statisticsWidget)
        self.incorrectPredictionsValue = self.createUiElement(
            ElementType.Label,
            "incorrectPredictionsValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.incorrectPredictionsLabel,
                                   self.incorrectPredictionsValue)

        self.totalPredictionsLabel = self.createUiElement(
            ElementType.Label,
            "totalPredictionsLabel",
            parent=self.statisticsWidget)
        self.totalPredictionsValue = self.createUiElement(
            ElementType.Label,
            "totalPredictionsValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.totalPredictionsLabel,
                                   self.totalPredictionsValue)

        self.accuracyLabel = self.createUiElement(ElementType.Label,
                                                  "accuracyLabel",
                                                  parent=self.statisticsWidget)
        self.accuracyValue = self.createUiElement(ElementType.Label,
                                                  "accuracyValue",
                                                  parent=self.statisticsWidget,
                                                  alignment=None)
        self.statisticsForm.addRow(self.accuracyLabel, self.accuracyValue)

        self.totalRuntimeLabel = self.createUiElement(
            ElementType.Label,
            "totalRuntimeLabel",
            parent=self.statisticsWidget)
        self.totalRuntimeValue = self.createUiElement(
            ElementType.Label,
            "totalRuntimeValue",
            parent=self.statisticsWidget,
            alignment=None)
        self.statisticsForm.addRow(self.totalRuntimeLabel,
                                   self.totalRuntimeValue)

        self.statisticsButtonLayout = self.createUiElement(
            ElementType.HBox,
            "statisticsButtonLayout",
            parent=self.statisticsWidget)
        self.statisticsLayout.addLayout(self.statisticsButtonLayout)

        self.reRunExperimentButton = self.createUiElement(
            ElementType.Button,
            "reRunExperimentButton",
            parent=self.statisticsWidget,
            fixedSize=(150, 40))
        self.statisticsButtonLayout.addWidget(self.reRunExperimentButton)
        self.reRunExperimentButton.clicked.connect(self.startExperiment)

        self.newExperimentButton = self.createUiElement(
            ElementType.Button,
            "newExperimentButton",
            parent=self.statisticsWidget,
            fixedSize=(150, 40))
        self.statisticsButtonLayout.addWidget(self.newExperimentButton)
        self.newExperimentButton.clicked.connect(self.setupSettingsViewUi)

        # By default select and show statistics for the model selected in settings
        model_index = self.modelSelectionCombobox.findText(
            self.selectedModelName, QtCore.Qt.MatchFixedString)
        if model_index >= 0:
            self.modelSelectionCombobox.setCurrentIndex(model_index)
        self.displayNewStatistics(self.modelSelectionCombobox.currentText())

        self.statusBar.clearMessage()

        self.retranslateStatisticsView()

    def retranslateStatisticsView(self):
        self.statisticsFormLabel.setText(
            self.translate("mainWindow", "Statistics"))
        self.modelSelectionComboboxLabel.setText(
            self.translate("mainWindow", "Show model: "))
        self.runTypeLabel.setText(
            self.translate("mainWindow", "Experiment type: "))
        self.transferLearningLabel.setText(
            self.translate("mainWindow", "Transfer learning used: "))
        self.leftHandPredictionsLabel.setText(
            self.translate("mainWindow", "Left hand predictions:"))
        self.rightHandPredictionsLabel.setText(
            self.translate("mainWindow", "Right hand predictions:"))
        self.correctPredictionsLabel.setText(
            self.translate("mainWindow", "Correct predictions:"))
        self.incorrectPredictionsLabel.setText(
            self.translate("mainWindow", "Incorrect predictions:"))
        self.totalPredictionsLabel.setText(
            self.translate("mainWindow", "Total predictions:"))
        self.accuracyLabel.setText(
            self.translate("mainWindow", "Prediction accuracy:"))
        self.totalRuntimeLabel.setText(
            self.translate("mainWindow", "Experiment run time:"))
        self.reRunExperimentButton.setText(
            self.translate("mainWindow", "Run experiment again"))
        self.newExperimentButton.setText(
            self.translate("mainWindow", "New experiment"))

    def displayNewStatistics(self, model_name):
        model_stats = self.statistics[model_name]

        runTypeMsg = "Executed movement" if model_stats.get_run_type(
        ) == 0 else "Imagined movement"
        self.runTypeValue.setText(runTypeMsg)

        transferLearningMsg = "Yes" if self.transferLearningEnabled else "No"
        self.transferLearningValue.setText(transferLearningMsg)

        leftHandPredictions = str(model_stats.get_left_hand_total())
        self.leftHandPredictionsValue.setText(leftHandPredictions)

        rightHandPredictions = str(model_stats.get_right_hand_total())
        self.rightHandPredictionsValue.setText(rightHandPredictions)

        correctPredictions = str(model_stats.get_correct_total())
        self.correctPredictionsValue.setText(correctPredictions)

        incorrectPredictions = str(model_stats.get_incorrect_total())
        self.incorrectPredictionsValue.setText(incorrectPredictions)

        totalPredictions = str(model_stats.get_total_tasks())
        self.totalPredictionsValue.setText(totalPredictions)

        accuracy = '{:.2%}'.format(model_stats.get_accuracy())
        self.accuracyValue.setText(accuracy)

        minutes, seconds = self.calculate_runtime(model_stats.get_total_time())
        totalRuntime = '{} minutes and {} seconds'.format(minutes, seconds)
        self.totalRuntimeValue.setText(totalRuntime)

    def calculate_runtime(self, runtime):
        minutes = int(runtime / 60)
        seconds = int(runtime - minutes * 60)
        return minutes, seconds

    def addActions(self, item, actions):
        for action in actions:
            item.addAction(action)

    def defineEvents(self, mainWindow):
        QtCore.QMetaObject.connectSlotsByName(mainWindow)

    def exitGracefully(self):
        self.stopExperiment(quitCalled=True)
        sys.exit()

    def startExperiment(self):
        try:
            self.validateInputFields()
        except AssertionError:
            self.displayError("Please fill all input fields")
            return
        except AttributeError:
            self.displayError("Input fields must be positive integers")
            return

        self.runType = self.runType if self.runType != None else self.runTypeComboBox.currentIndex(
        )
        self.transferLearningEnabled = self.transferLearningEnabled if self.transferLearningEnabled != None else self.transferLearningCheckbox.isChecked(
        )
        self.showingPredictions = self.showingPredictions if self.showingPredictions != None else self.showPredictionsCheckbox.isChecked(
        )
        self.selectedModelName = self.selectedModelName if self.selectedModelName != None else str(
            self.modelComboBox.currentText())
        self.debugMode = self.debugMode if self.debugMode != None else self.debugModeCheckbox.isChecked(
        )
        self.models.set_selected(self.runType, self.selectedModelName)
        self.evaluateAllModels = self.evaluateAllModels if self.evaluateAllModels != None else self.evaluateAllModelsCheckBox.isChecked(
        )
        selectedModel = self.models.get_selected()
        if self.evaluateAllModels:
            modelType = ModelType.Executed if self.runType == 0 else ModelType.Imagined
            models = self.models.get_models(modelType)
        else:
            models = [selectedModel]

        self.experiment = Experiment(self.subjectId, self.runType,
                                     selectedModel, self.nrOfTasks,
                                     self.showingPredictions,
                                     self.transferLearningEnabled,
                                     self.debugMode, models)

        self.setupExperimentViewUi()

        try:
            self.runExperiment()
        except Exception as e:
            self.handleException(e)

    def validateInputFields(self):
        if (not self.nrOfTasks \
            or not self.subjectId) \
                and (not self.nrOfTasksValue.text() \
                     or not self.subjectIdValue.text()):
            raise AssertionError
        if not self.nrOfTasks or not self.subjectId:
            try:
                nrOfTasks = int(self.nrOfTasksValue.text())
                subjectId = int(self.subjectIdValue.text())
                if nrOfTasks < 1 or subjectId < 1:
                    raise AttributeError
                self.nrOfTasks = nrOfTasks
                self.subjectId = subjectId
            except:
                raise AttributeError

    def displayError(self, message):
        QtWidgets.QMessageBox.critical(self.mainWindow, "Error", message)

    def handleException(self, exception):
        exc_type, msg, trace = exception
        print(trace)
        self.displayError(str(msg))

    def stopExperiment(self, quitCalled=False):
        if not hasattr(self, 'experiment'):
            return
        self.threadpool.waitForDone(1)
        self.experiment.stop()
        self.experiment.set_run_time(time.time() - self.experimentStartTime)
        if quitCalled:
            return
        self.setupStatisticsViewUi()
        if self.transferLearningEnabled:
            self.transferLearningRunning(True)
            try:
                worker = Worker(self.experiment.apply_transfer_learning)
                worker.signals.finished.connect(
                    self.onTransferLearningFinished)
                worker.signals.error.connect(self.onTransferLearningFinished)
                self.threadpool.start(worker)
            except Exception as e:
                self.handleException(e)

    def onTransferLearningFinished(self):
        self.transferLearningRunning(False)

    def runExperiment(self):
        worker = Worker(self.experiment.run)
        worker.signals.progress.connect(self.determineExperimentAction)
        worker.signals.error.connect(self.handleException)
        worker.signals.finished.connect(self.stopExperiment)
        self.threadpool.start(worker)
        self.experimentStartTime = time.time()

    def determineExperimentAction(self, *args):
        if self.view != ViewType.Experiment:
            return
        task, progress_type = args[0]
        if progress_type == ProgressType.TaskStart:
            self.updateExperimentUi(task)
            self.stopButton.setEnabled(True)
        elif progress_type == ProgressType.TaskResult:
            if self.showingPredictions:
                self.showPrediction(task)
            else:
                self.showRest()
        else:
            raise Exception("Invalid progress type")

    def showRest(self):
        self.updateExperimentLabel(LabelType.Rest)
        self.updateGraphics(GraphicType.Rest)

    def transferLearningRunning(self, running):
        self.reRunExperimentButton.setEnabled(not running)
        self.newExperimentButton.setEnabled(not running)
        if running:
            msg = "Running transfer learning. Please wait..."
        else:
            msg = "Transfer learning finished"
        self.statusBar.showMessage(msg)

    def updateExperimentUi(self, task):
        self.updateExperimentLabel(LabelType.Task)
        self.updateTaskNr(task.get_task_nr())
        graphic_type = GraphicType.LeftTask if task.get_task_type(
        ) == 0 else GraphicType.RightTask
        self.updateGraphics(graphic_type)

    def showPrediction(self, task):
        task_label = task.get_task_type()
        prediction = task.get_majority_prediction()
        if prediction == 0 and task_label == prediction:
            self.updateGraphics(GraphicType.LeftCorrectPrediction)
        elif prediction == 0 and task_label != prediction:
            self.updateGraphics(GraphicType.LeftIncorrectPrediction)
        elif prediction == 1 and task_label == prediction:
            self.updateGraphics(GraphicType.RightCorrectPrediction)
        elif prediction == 1 and task_label != prediction:
            self.updateGraphics(GraphicType.RightIncorrectPrediction)
        else:
            raise AttributeError(
                "Invalid prediction value {}".format(prediction))

        self.updateExperimentLabel(LabelType.Prediction)

    def updateExperimentLabel(self, labelType):
        if labelType == LabelType.Prediction:
            self.experimentLabel.setText(
                "The model predicts that the previous task was")
        elif labelType == LabelType.Task:
            self.experimentLabel.setText(
                "Please perform the task indicated by the circle")
        elif labelType == LabelType.Rest:
            self.experimentLabel.setText("Please rest before the next task")
        else:
            raise AttributeError("Invalid label type")

    def updateGraphics(self, graphic_type):
        pixmap = self.getTaskGraphic(graphic_type)
        self.experimentGraphic.setPixmap(pixmap)

    def getTaskGraphic(self, graphic_type):
        file_locations = {
            GraphicType.LeftTask:
            "./images/circle_left.jpg",
            GraphicType.RightTask:
            "./images/circle_right.jpg",
            GraphicType.Rest:
            "./images/blank.jpg",
            GraphicType.LeftCorrectPrediction:
            "./images/circle_left_correct.jpg",
            GraphicType.RightCorrectPrediction:
            "./images/circle_right_correct.jpg",
            GraphicType.LeftIncorrectPrediction:
            "./images/circle_left_incorrect.jpg",
            GraphicType.RightIncorrectPrediction:
            "./images/circle_right_incorrect.jpg"
        }

        return QtGui.QPixmap(file_locations.get(graphic_type))
コード例 #45
0
def main():
	NAME = None
	LAYER_SIZES = None
	C_VECTOR = None # list of digits or the string 'crossLipschitz'
	RANDOM_SEED = None
	RADIUS = None
	MNIST_DIGITS = None 	
	FREQUENCY = None
	EPOCHS = None
	assert all([_ is not None for _ in [NAME, LAYER_SIZES, C_VECTOR, 
										RANDOM_SEED, RADIUS, FREQUENCY, 
										EPOCHS]])

	exp_kwargs = {'c_vector': C_VECTOR,
				  'primal_norm': 'linf'}
	DIMENSION = 784
	GLOBAL_LO = np.zeros(DIMENSION)
	GLOBAL_HI = np.ones(DIMENSION)
	DOMAIN = Hyperbox.build_unit_hypercube(DIMENSION)
	BALL_FACTORY = Factory(Hyperbox.build_linf_ball, radius=RADIUS)
	NAMER = lambda epoch_no: '%s_EPOCH%04d' % (NAME, epoch_no)
	# ================================================================
	# =           Data Parameters Setup                              =
	# ================================================================
	# Make both the training/validation sets 

	train_set = dl.load_mnist_data('train', digits=MNIST_DIGITS)
	val_set = dl.load_mnist_data('val', digits=MNIST_DIGITS)

	# Make the data arg_bundle object
	loader_kwargs = {'batch_size': 100, 'digits': MNIST_DIGITS,
					 'shuffle': True}
	train_arg_bundle = {'data_type': 'MNIST',
		 			    'loader_kwargs': loader_kwargs,
		 			    'ball_factory': BALL_FACTORY,
		 			    'train_or_val': 'train'}
	val_arg_bundle = {'data_type': 'MNIST',
 	 			      'loader_kwargs': loader_kwargs,
		 			  'ball_factory': BALL_FACTORY,
		 			  'train_or_val': 'val'}

	# ================================================================
	# =           Training Parameter Setup                           =
	# ================================================================

	# Build the loss functional and set the optimizer 
	xentropy = train.XEntropyReg()
	l2_penalty = train.LpWeightReg(scalar=1e-2, lp='l2')
	loss_functional = train.LossFunctional(regularizers=[xentropy])
	train_params = train.TrainParameters(train_set, train_set, EPOCHS, 
										 loss_functional=loss_functional,
										 test_after_epoch=20)
	# Build the base network architecture
	network = ReLUNet(layer_sizes=LAYER_SIZES)


	# ================================================================
	# =           Build the Experiment objects                       =
	# ================================================================

	local_exp = Experiment([FastLip, LipLP, LipMIP], network=network,
						   **exp_kwargs)
	global_exp = Experiment(GLOBAL_METHODS, network=network, **exp_kwargs)

	# ================================================================
	# =           Build the methodNests                              =
	# ================================================================

	# --- randomly evaluated method nest
	random_nest = MethodNest(Experiment.do_random_evals, 
	   						  {'sample_domain': DOMAIN, 
							   'ball_factory': BALL_FACTORY,
							   'num_random_points': 20})

	# --- data-based method nest 
	train_nest = MethodNest(Experiment.do_data_evals, train_arg_bundle)
	val_nest = MethodNest(Experiment.do_data_evals, val_arg_bundle)


	# --- hypercube stuff 
	cube_nest = MethodNest(Experiment.do_unit_hypercube_eval)

	local_nests = [random_nest, train_nest, val_nest, cube_nest]
	global_nests = [cube_nest]


	def build_jobs(epoch_no, network=None):
		local_job_name = NAMER(epoch_no) + '_LOCAL'
		local_job = Job(local_job_name, local_exp, local_nests,
						save_loc=SCHEDULE_DIR)
		local_job.write()

		global_job_name = NAMER(epoch_no) + '_GLOBAL'
		global_job = Job(global_job_name, global_exp, global_nests,
						 save_loc=SCHEDULE_DIR)
		global_job.write()

	if FREQUENCY is None:
		job_do_every = None
	else:
		job_do_every = DoEvery(build_jobs, FREQUENCY)

	# ==============================================================
	# =           Train the network                                =
	# ==============================================================

	train.training_loop(network, train_params, epoch_callback=job_do_every)
	if FREQUENCY is None:
		build_jobs(EPOCHS)
コード例 #46
0
ファイル: train_search_sgl.py プロジェクト: jboboyle/SGL-DANN
                    help='portion of training data')
parser.add_argument('--unrolled',
                    action='store_true',
                    default=False,
                    help='use one-step unrolled validation loss')
parser.add_argument('--arch_learning_rate',
                    type=float,
                    default=6e-4,
                    help='learning rate for arch encoding')
parser.add_argument('--arch_weight_decay',
                    type=float,
                    default=1e-3,
                    help='weight decay for arch encoding')

# new hyperparams.
parser.add_argument('--weight_lambda', type=float, default=1.0)
parser.add_argument('--pretrain_steps', type=int, default=0)
args = parser.parse_args()

assert args.unrolled == False and '--unrolled not supported!'
assert args.train_portion == 0.5 and '--train_portion not supported!'

# Main Driver for your code. Either run `python main.py` which will run the experiment with default config
# or specify the configuration by running `python main.py custom`
if __name__ == "__main__":
    exp_name = args.save
    print("Running Experiment: ", exp_name)
    exp = Experiment(args)
    exp.run()
    # exp.test()
コード例 #47
0
        self._log_attrs.extend(['push_val', 'push_time'])

    def _callback(self):
        if type(self._push_val) != list:
            self._server.push_sample([self._push_val])
        else:
            self._server.push_sample(self._push_val)
        self._push_time = clock.now()


if __name__ == "__main__":

    from experiment import Experiment

    exp = Experiment()

    # Initialize the outlet
    OUTLET = init_lsl_outlet(server_name='MarkerStream',
                             server_type='Markers',
                             nchans=1,
                             suggested_freq=500,
                             channel_format='int32',
                             unique_id='SMILE_LSL_OUT')

    # Signal the beginning of the experiment.
    LSLPush(server=OUTLET, val=55)

    # Wait for the experiment to start!
    Wait(2.)
コード例 #48
0
ファイル: main.py プロジェクト: renatolm/fuzzy-inference
import numpy as np
import membership
import tnorms
import implications
from experiment import Experiment

#Antecedents
a1 = []

#Consequents
b1 = []

#Input
inp = []

for i in np.arange(0,10,0.01):	
	a1.append(membership.triang(i,1,4,7))		

	b1.append(membership.triang(i,1,3,5))			

	inp.append(membership.triang(i,2,3,4))	


name = "exp1"
description = "first test"
tnorm = tnorms.minimum
implication = implications.godel

experiment = Experiment(name, description, inp, a1, b1, tnorm, implication)

experiment.run_full_experiment()
コード例 #49
0
def test_probability_of_a_head():
    e = Experiment()
    p = e.flip_coin_x_times(100_000)
    assert p == approx(0.5, rel=0.01)
コード例 #50
0
ファイル: main.py プロジェクト: thund3rcake/proj
        gamma = None

    try:
        batch_size = params["method"]["batch_size"]
    except KeyError:
        batch_size = None

    if save_folder is not None and not os.path.exists(save_folder):
        os.makedirs(save_folder)

    with open(os.path.join(params["logging"]["save_folder"], "config.yml"),
              'w') as f:
        yaml.dump(params, f)

    experiment = Experiment(
        n=params["task"]["matrix"]["n"],
        kappa=params["task"]["matrix"]["kappa"],
        scale=params["task"]["scale"],
        alpha=params["method"]["alpha"],
        beta=params["method"]["beta"],
        tau=tau,
        gamma=gamma,
        batch_size=batch_size,
        save_folder=save_folder,
    )

    print(experiment.__dict__)

    experiment.run(num_iters=params["method"]["num_iters"])
    print("Training is completed.")
コード例 #51
0
    parser.add_argument('--beta1', type=float, default=0.9)
    parser.add_argument('--beta2', type=float, default=0.999)
    parser.add_argument('--weight_decay', type=float, default=0.)
    # misc
    parser.add_argument('--device', type=str, default='cpu')
    parser.add_argument('--log_dir', type=str, default='logs')
    parser.add_argument('--exp_name', type=str, default='garbage')

    args, unknown_args = parser.parse_known_args()

    args.data_A_dir = './data/horse2zebra/trainA'
    args.data_B_dir = './data/horse2zebra/trainB'
    args.batch_size = 1
    args.train_iters = int(1e+9)
    args.use_lsgan = True
    args.lr = 2e-4
    args.beta1 = 0.5
    args.device = 'cuda:0'
    args.exp_name = 'exp0.4'
    args.log_report_freq = 10
    args.scalar_report_freq = 10
    args.image_report_freq = 10

    args.log_dir = os.path.join(args.log_dir, args.exp_name)
    if os.path.exists(args.log_dir):
        raise OSError('`{}` already exists.'.format(args.log_dir))
    else:
        os.makedirs(args.log_dir)

    Experiment(args).run()
コード例 #52
0
import logging

import fire

import constants
from experiment import Experiment

logging.basicConfig(format=constants.LOG_FORMAT)
logging.getLogger(constants.LOGGER_NAME).setLevel(logging.INFO)

# the func below is a wrapped because of some python-fire docstrings issues

experiment = Experiment()

if __name__ == "__main__":
    fire_experiment = fire.Fire(Experiment())
コード例 #53
0
ファイル: run.py プロジェクト: Liuxg16/braingraph
def main():
    parser = argparse.ArgumentParser(description="Experiment setup")
    # misc
    parser.add_argument('--seed', default=33, type=int)
    parser.add_argument('--gpu', default="", type=str)
    parser.add_argument('--no_train', default=False, action="store_true")
    parser.add_argument('--from_model_ckpt', default=None, type=str)
    parser.add_argument('--no_rules', default=False, action="store_true")
    parser.add_argument('--rule_thr', default=1e-2, type=float)
    parser.add_argument('--no_preds', default=False, action="store_true")
    parser.add_argument('--get_vocab_embed',
                        default=False,
                        action="store_true")
    parser.add_argument('--exps_dir', default='exps', type=str)
    parser.add_argument('--exp_name', default=None, type=str)
    parser.add_argument('--load', default=None, type=str)
    # data property
    parser.add_argument('--datadir', default=None, type=str)
    parser.add_argument('--resplit', default=False, action="store_true")
    parser.add_argument('--no_link_percent', default=0., type=float)
    parser.add_argument('--type_check', default=False, action="store_true")
    parser.add_argument('--domain_size', default=128, type=int)
    parser.add_argument('--no_extra_facts', default=False, action="store_true")
    parser.add_argument('--query_is_language',
                        default=False,
                        action="store_true")
    parser.add_argument('--vocab_embed_size', default=128, type=int)
    # model architecture
    parser.add_argument('--num_step', default=4, type=int)
    parser.add_argument('--num_layer', default=1, type=int)
    parser.add_argument('--rnn_state_size', default=128, type=int)
    parser.add_argument('--query_embed_size', default=128, type=int)
    # optimization
    parser.add_argument('--batch_size', default=64, type=int)
    parser.add_argument('--print_per_batch', default=3, type=int)
    parser.add_argument('--max_epoch', default=200, type=int)
    parser.add_argument('--min_epoch', default=100, type=int)
    parser.add_argument('--learning_rate', default=0.001, type=float)
    parser.add_argument('--no_norm', default=False, action="store_true")
    parser.add_argument('--no_cuda', default=False, action="store_true")
    parser.add_argument('--thr', default=1e-20, type=float)
    parser.add_argument('--dropout', default=0., type=float)
    # evaluation
    parser.add_argument('--get_phead', default=False, action="store_true")
    parser.add_argument('--adv_rank', default=False, action="store_true")
    parser.add_argument('--rand_break', default=False, action="store_true")
    parser.add_argument('--accuracy', default=False, action="store_true")
    parser.add_argument('--top_k', default=10, type=int)

    d = vars(parser.parse_args())
    option = Option(d)

    random.seed(option.seed)
    np.random.seed(option.seed)
    torch.manual_seed(option.seed)

    load_fn = 'data.mat'
    load_data = sio.loadmat(load_fn)
    blood_oxygen = load_data['value1']  # ndarray
    speed = load_data['value2']  # ndarray
    labels = load_data['state']
    labels = (labels - np.min(labels)) * 1.0 / (np.max(labels) -
                                                np.min(labels))

    labels = labels.reshape(-1)
    data = {}
    trainn = 270
    print labels.shape
    data['train_edge1'] = blood_oxygen[:trainn]
    data['train_edge2'] = speed[:trainn]
    data['train_labels'] = labels[:trainn]

    data['valid_edge1'] = blood_oxygen[trainn:]
    data['valid_edge2'] = speed[trainn:]
    data['valid_labels'] = labels[trainn:]

    # 300*N,N,  300,N,N   300,1

    if option.exp_name is None:
        option.tag = time.strftime("%y-%m-%d-%H-%M")
    else:
        option.tag = option.exp_name

    option.this_expsdir = os.path.join(option.exps_dir, option.tag)
    if not os.path.exists(option.this_expsdir):
        os.makedirs(option.this_expsdir)
    option.ckpt_dir = os.path.join(option.this_expsdir, "ckpt")
    if not os.path.exists(option.ckpt_dir):
        os.makedirs(option.ckpt_dir)
    option.model_path = os.path.join(option.ckpt_dir, "model")

    option.save()
    print("Option saved.")

    # learner = Learner(option)
    device = torch.device(
        "cuda" if torch.cuda.is_available() and not option.no_cuda else "cpu")
    n_gpu = torch.cuda.device_count()

    # learner = CNN(option)
    learner = StackedGAT(option)
    # learner = BrainGraph(option)
    learner.to(device)
    # learner = nn.DataParallel(learner, [0,1,2])
    if option.load is not None:
        with open(option.load, 'rb') as f:
            learner.load_state_dict(torch.load(f))

    experiment = Experiment(option, device, learner=learner, data=data)
    print("Experiment created.")

    if not option.no_train:
        print("Start training...")
        experiment.train()

    if not option.no_preds:
        print("Start getting test predictions...")
        experiment.get_predictions()

    # saver = tf.train.Saver(max_to_keep=option.max_epoch)
    # saver = tf.train.Saver()
    # config = tf.ConfigProto()
    # config.gpu_options.allow_growth = False
    # config.log_device_placement = False
    # config.allow_soft_placement = True
    # with tf.Session(config=config) as sess:
    #     tf.set_random_seed(option.seed)
    #     sess.run(tf.global_variables_initializer())
    #     print("Session initialized.")

    #     if option.from_model_ckpt is not None:
    #         saver.restore(sess, option.from_model_ckpt)
    #         print("Checkpoint restored from model %s" % option.from_model_ckpt)

    #     data.reset(option.batch_size)
    #     experiment = Experiment(sess, saver, option, learner, data)
    #     print("Experiment created.")

    #     if not option.no_train:
    #         print("Start training...")
    #         experiment.train()
    #
    #     if not option.no_preds:
    #         print("Start getting test predictions...")
    #         experiment.get_predictions()
    #
    #     if not option.no_rules:
    #         print("Start getting rules...")
    #         experiment.get_rules()

    #     if option.get_vocab_embed:
    #         print("Start getting vocabulary embedding...")
    #         experiment.get_vocab_embedding()
    #
    # experiment.close_log_file()
    print("=" * 36 + "Finish" + "=" * 36)
コード例 #54
0
    exp.model["CTX:cog → STR:cog"].gain = g1
    for trial in exp.task.block(0):
        exp.model.process(exp.task, trial)
    records[1] = exp.task.records

    return records


mdl = "model-topalidou-thomas.json"
tsk = "tasks/task-topalidou.json"
rslt = folderRes + "protocol-NoStrCog.npy"
rprt = folderRep + "protocol-NoStrCog.txt"
experiment = Experiment(model=mdl,
                        task=tsk,
                        result=rslt,
                        report=rprt,
                        n_session=25,
                        n_block=2,
                        seed=377)
records = experiment.run(session, "Thomas Protocol No Str Cog")

# Textual results
# -----------------------------------------------------------------------------
P = np.squeeze(records["best"][:, 0, :25]) * 100
P = P.mean(axis=len(P.shape) - 1)
print("D1 start: %.3f ± %.3f" % (P.mean(), P.std()))
P = np.squeeze(records["best"][:, 0, -25:]) * 100
P = P.mean(axis=len(P.shape) - 1)
print("D1 end:   %.3f ± %.3f" % (P.mean(), P.std()))

print()
コード例 #55
0
def test_probability_of_ththh():
    e = Experiment()
    p = e.flip_coin_with_pattern_n_times([0, 1, 0, 1, 1], 1_000_000, 5)
    assert p == approx((0.5)**5, rel=0.01)
コード例 #56
0

mdl = "model-topalidou.json"
#revr = [10,20,30,40,50,60,70,80,90,100,200,300,400,500]
revr = 200
dt = "data_new"
fdl = "reverse" + str(revr) + "-sessions"

trial = str(revr)
tsk = "tasks/task-topalidou-reverse-" + trial + ".json"
rslt = dt + "/results/reverse-NoCtx/experiment-topalidou-reverse-" + trial + ".npy"  #
rprt = dt + "/reports/reverse-NoCtx/experiment-topalidou-reverse-" + trial + ".txt"  #
experiment = Experiment(model=mdl,
                        task=tsk,
                        result=rslt,
                        report=rprt,
                        n_session=25,
                        n_block=1,
                        seed=123)  #None)
records = experiment.run(session, "Protocol Reverse")
records = np.squeeze(records)

# -----------------------------------------------------------------------------
for session in range(25):

    xlims = 1000
    #session = 0
    P_mean = records["best"][session, :xlims]

    Rwrd_mean = records["reward"][session, :xlims]
コード例 #57
0
def test_probability_of_hhhh():
    e = Experiment()
    p = e.flip_coin_with_pattern_n_times([1, 1, 1, 1], 1_000_000, 4, 0.3)
    assert p == approx((0.3)**4, rel=0.01)
コード例 #58
0
def test_probability_of_odd_3_4_less_3():
    e = Experiment()
    pattern = [[1, 3, 5], [3], [4], [1, 2]]
    p = e.roll_die_with_pattern_n_times(pattern, 1_000_000, 4,
                                        (0.3, 0.05, 0.05, 0.3, 0.1, 0.2))
    assert p == approx(.45 * .05 * .30 * .35, rel=0.02)
コード例 #59
0
ファイル: train.py プロジェクト: kumiko-oreyome/qa_mrc
        if performance > best_performance:
            best_performance = performance
            print('best performance is %.3f' % (performance))
            torch.save(model.state_dict(), model_path)
        model.train()


parser = argparse.ArgumentParser(description="train script")
parser.add_argument('exp_name')
parser.add_argument('--debug', action='store_true', help='verbose mode')

args = parser.parse_args()

if args.debug:
    print('warning ... excute in debug mode')
    expe = Experiment('debug/pointwise/answer_doc')
else:
    expe = Experiment(args.exp_name)

config = expe.config
print_to_tee(expe.train_log_path)

sample_stg = sample_strategy_factory(config.SAMPLE_STG_NAME,
                                     k=config.NEG_SAMPLE_NUM)

print('load examples')
train_examples = load_examples_from_scratch(config.TRAIN_PATH,
                                            sample_stg,
                                            concat=True)
print('build model')
コード例 #60
0
    BATCH_SIZE = 1
    NO_OF_EPOCHS = 200
    input_shape = (224, 224, 3)
    lr = 1E-4

    # pre-trained model
    load = False
    # load = 'results/ckpt/FCN_Vgg16_32s_best_model_1.h5'

    train_gen = data_generation(img_dir_path=train_frame_path, batch_size=BATCH_SIZE,
                                inputshape=input_shape, labels_dir_path=train_mask_path)
    val_gen = data_generation(img_dir_path=val_frame_path, batch_size=BATCH_SIZE,
                              inputshape=input_shape, labels_dir_path=val_mask_path)

    experiment_object = Experiment(inputshape=input_shape, learning_rate=lr, train_gen=train_gen, val_gen=val_gen)

    STEP_PER_EPOCH = (len(os.listdir(train_frame_path)) // BATCH_SIZE)
    VAL_STEPS = (len(os.listdir(val_frame_path)) // BATCH_SIZE)

    config = tf.compat.v1.ConfigProto(gpu_options=
                                      tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.8)
                                      # device_count = {'GPU': 1}
                                      )
    config.gpu_options.allow_growth = True
    session = tf.compat.v1.Session(config=config)
    tf.compat.v1.keras.backend.set_session(session)
    experiment_object.train_model(epochs=NO_OF_EPOCHS,
                                  validation_steps=VAL_STEPS, pretrained_weights=load,
                                  steps_per_epoch=STEP_PER_EPOCH)
    print('*****Renaming File*****')