Example #1
0
def test2():
    #onethread로 테스트.
    game = simple_go.Game(9)     
    
    komi = 6.5
    game.set_komi(komi)
    
    #현재상태.
    game =  load_sgf.load_file("sgfs/test.sgf")
    print str(game.current_board)
    
    #network test
    m_network = network()
    m_network.init2()
    t1 = time.time()
    col, row = m_network.predict(game, type = 'SLPOLICY')
    t2 = time.time()
    print 'col : ' + str(col) + ', row : ' + str(row) + "  takes : " + str(t2 - t1)
    
    game.network = m_network
    
    root = StateNode(None, GoState(game))
    evaluation(root)
    
    print 'main ended'
Example #2
0
def test3():
    game = simple_go.Game(9)     
    
    komi = 6.5
    game.set_komi(komi)
    
    #현재상태.
    game =  load_sgf.load_file("sgfs/test.sgf")
    print str(game.current_board)
    
    #network test
    m_network = network()
    m_network.init2()
    t1 = time.time()
    col, row = m_network.predict(game, type = 'SLPOLICY')
    t2 = time.time()    
    print 'col : ' + str(col) + ', row : ' + str(row) + "  takes : " + str(t2 - t1)
    
    game.network = m_network
    
    m_jobmgr = jobmgr3()
    m_jobmgr.genmove(game)
    
    time.sleep(10)
    
    m_jobmgr.release()
    
    printgraph('result/test.gv', m_jobmgr.root)
    print 'main ended'
Example #3
0
 def __init__(self, host, port, name, mode):
     self.host = host
     self.port = port
     self.mode = mode
     
     self.name = name
     self.queue = Queue.Queue()
     
     self.logger = logging.getLogger('evaluator_' + name)
     self.logger.setLevel(logging.DEBUG)
     
     # create console handler and set level to debug
     ch = logging.FileHandler('evaluator.log')
     ch.setLevel(logging.DEBUG)
     
     # create formatter
     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     
     # add formatter to ch
     ch.setFormatter(formatter)
     self.logger.addHandler(ch)
     
     '''
     logger.debug('debug message')
     logger.info('info message')
     logger.warn('warn message')
     logger.error('error message')
     logger.critical('critical message')
     '''
     
     self.m_network = network()
     self.m_network.init2()
     
     self.init_conn()
Example #4
0
	def __init__(self):
		# independant threads
		__network = network();
		__ncurses = ui_ncurses();
		# simple message queuing
		while True:
			__ncurses.process_message(__network.queue());
		return;
		'''
Example #5
0
def train():
    (train_X, train_Y, val_X, val_Y, test_X, test_Y) = preprocess(datapath)
    model = network()
    history = model.fit(x=train_X, y=train_Y, validation_data = (val_X, val_Y),
                    batch_size=64,
                      epochs=10)
    # Calculate its accuracy on validation data
    _,acc = model.evaluate(val_X, val_Y)
    print('The accuracy on the validation data is {}.'.format(acc*100))
    # Save the model
    model.save('model1_10epoch.h5')
Example #6
0
def train():
    (train_X, train_Y, test_X, test_Y) = preprocess()
    model = network()
    # There is no validation data. We use test data to get the accuracy
    model.fit(train_X, train_Y,
              validation_data=(test_X, test_Y),
              epochs=5, batch_size=32)
    # Calculate its accuracy on testing data
    _,acc = model.evaluate(test_X, test_Y)
    print('The accuracy on the testing data is {}.'.format(acc*100))
    # Save the model
    model.save('model1_cifar_5epoch.h5')
Example #7
0
 def testBuildNoClasses(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = None
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         net, endpoints = network(inputs, num_classes)
         self.assertTrue('AuxLogits' not in endpoints)
         self.assertTrue('Logits' not in endpoints)
         self.assertTrue(
                 net.op.name.startswith('InceptionResnetV2/Logits/AvgPool'))
         self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
Example #8
0
    def __init__(self):
        self.graph = tf.Graph()

        with self.graph.as_default():
            self.x = tf.placeholder(tf.float32, [None, hp.timestep, 1], name='X')

            output = network(self.x, use_mulaw=hp.use_mulaw)

            if hp.use_mulaw:
                self.prediction = mu_law_decode(tf.argmax(output, axis=2))
            else:
                self.prediction = tf.squeeze(output, -1)
Example #9
0
 def testBuildWithoutAuxLogits(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, endpoints = network(inputs, num_classes,
                                                                                                             create_aux_logits=False)
         self.assertTrue('AuxLogits' not in endpoints)
         self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
         self.assertListEqual(logits.get_shape().as_list(),
                                                  [batch_size, num_classes])
Example #10
0
def main():
    # 装载数据
    with tf.name_scope("load_data"):
        test_img, test_lab = data_loader(test_file, EPOCH + 5)
        test_image, test_label = tf.train.shuffle_batch([test_img, test_lab],
                                                        batch_size=BATCH,
                                                        capacity=1005,
                                                        min_after_dequeue=1000)
    # 使用占位符构建输入
    with tf.name_scope('input'):
        input_image = tf.placeholder(dtype=tf.float32, shape=[None, 96, 96, 3])
        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=[None, students_num])
        drop_input = tf.placeholder(dtype=tf.float32)
    # 预测
    predict, regular = network(input_image, drop_input)
    # 计算准确率
    with tf.name_scope('accuracy'):
        predict_1 = tf.argmax(predict, 1)
        correct_1 = tf.argmax(correct_label, 1)
        correct_prediction = tf.equal(correct_1, predict_1)
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        tf.summary.scalar('accuracy', accuracy)
    # 使用已经训练好的模型初始化网络
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    # 开启多线程读取数据
    threads = tf.train.start_queue_runners(sess=sess)
    load_net(sess, model_save_dir)
    mean_acc = 0
    for j in range(int(test_num / BATCH)):
        # 获取tfrecords中的数据作为输入
        image, label = sess.run([test_image, test_label])
        # 预测
        correct, predict, accuracy_i = sess.run(
            [predict_1, correct_1, accuracy],
            feed_dict={
                input_image: image,
                correct_label: label,
                drop_input: 1.0
            })
        # 展示每个batch中第一个图片及其预测结果和对应的真实结果
        print("真实:", new_students[correct[0]], "预测: ",
              new_students[int(predict[0])])
        cv2.imshow('a', image[0])
        cv2.waitKey(2000)
        mean_acc += accuracy_i
    # 计算最终的准确率
    mean_acc = mean_acc * BATCH / test_num
    print("accuracy: ", mean_acc)
Example #11
0
def main():

    i = neurons(3)
    h = neurons(5)
    o = neurons(1)
    
    n = network()
    n += e_hidden(i, h)
    n += e_out(h, o)
    
    x = xor_dataset()
    t = train(n)
    while True:
        print t.train(x)
Example #12
0
 def __init__(self, tag):
     self.tempd = [10,23,44,12,42,12,2,34,23,24,54,76,45,13,44,68,80,35,34,56,68,79,45,24,45,78,8,68,46,54,45]
     self.lightd = []
     self.s = 0
     self.max = 100
     self.min = 0
     self.x2 = 50
     self.y2 = 550 - 5*90
     self.set = True
     self.tl = True # termperature of brightness. True == temperature
     self.color = 'red'
     self.tag = tag
     self.canvases = []
     self.n = network()
Example #13
0
    def __init__(self):
        #if config.use_opening_library:
        #    self.opening_tree = {}
        #    for color in "WB":
        #        self.opening_tree[color + "0"] = cgos_opening_tree.OpeningTree("opening%s_0.dat" % color, None)
        #        self.opening_tree[color] = cgos_opening_tree.OpeningTree("opening%s.dat" % color, None)
        self.engine = simple_go.Game(9)
        self.master = GTP_controller(sys.stdin, sys.stdout)
        self.version = version.number
        #self.name = version.name + version.message
        self.name = version.name
        self.komi = 0.0
        self.handicap = 0
        self.capture_all_dead = False
        log_name = get_next_filename("gtpc%04i.log")
        self.log_fp = open(log_name, "w")
        self.clock = None
        self.move_generated = False

        komi = 6.5
        self.engine.set_komi(komi)

        # note formatting: follow newline with tab
        self.cmd_options = {
            "help":
            "Can also use '-h'. Displays this message.",
            "message=":
            "Message robot gives on new games\n\tQuotes required for spaces.",
            "node_limit=":
            "Positive integer\n\tSpecifies the normal depth of the search for a 9x9 game.",
            "manage_time":
            "If passed to program, uses time management.",
            "capture_all_dead":
            "If passed to program, will capture all dead before passing"
        }

        self.mode = None
        self.randomcount = 0

        print 'ready to setting '

        #jobmgr, network ¼³Á¤.
        self.m_network = network()
        self.m_network.init2()

        self.engine.network = self.m_network

        self.m_jobmgr = jobmgr2(self.mode)

        print 'setting is done'
Example #14
0
 def __init__(self, queue, logger, port):
     threading.Thread.__init__(self)
     self.queue = queue
     self.bStop = False
     
     self.m_network = network()
     self.m_network.init2()
     
     self.logger = logger
     self.port = port
     
     self.curpath = os.path.dirname( os.path.abspath( __file__ ) )
     
     self.count = 0
Example #15
0
def MF_simulation():
    # Create constants
    #create_constants(dir,time)

    if not os.path.exists(path_to_folder):
        os.makedirs(path_to_folder)

    output_file = open(path_to_folder + delivery_file_name, "w")
    output_file.write("ID\ts\td\tts\tte\tLLC\tsize\tparent\n")
    output_file.write("----------------------------------------------------\n")
    output_file.close()

    output_file2 = open(path_to_folder + notDelivered_file_name, "w")
    output_file2.write("ID\ts\td\tts\tte\tLLC\tsize\tparent\n")
    output_file2.write(
        "----------------------------------------------------\n")
    output_file2.close()

    output_file3 = open(path_to_folder + consumedEnergyFile, 'w')
    output_file3.write("Time\tEnergy\n")
    output_file3.close()

    #Load Link Exists
    LINK_EXISTS = pickle.load(open(Link_Exists_path + "LINK_EXISTS.pkl", "rb"))
    specBW = pickle.load(open(Link_Exists_path + "specBW.pkl", "rb"))
    # LINK_EXISTS = createLinkExistenceADJ()
    # print(LINK_EXISTS[3,4,])

    #Create network
    net = network()
    #Fill network with datamules, sources, and destinations
    net.fill_network()
    #Create messages
    # path = "Bands/" + day + "/"
    # create_messages(path)

    message_path_file = "../Bands" + str(
        max_nodes) + "/" + Link_Exists_path.split(
            "/")[2] + "/Day1/" + "generated_messages.txt"
    print(message_path_file)
    with open(message_path_file, "r") as f:
        msg_lines = f.readlines()[1:]

    #Run simulation
    for i in range(T):
        # print("TIME: " + str(i))
        net.network_GO(i, LINK_EXISTS, specBW, msg_lines)

    net.all_messages()
Example #16
0
def main():
    sess = tf.InteractiveSession()
    cap = cv2.VideoCapture(0)
    # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
    # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 960)
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    videoWriter = cv2.VideoWriter('output.avi', fourcc, 45, (640, 480))
    input = tf.placeholder(tf.float32, shape=[None, 96, 96, 3])
    drop_input = tf.placeholder(dtype=tf.float32)
    pre, _ = network(input, drop_input)
    name = tf.argmax(pre, 1)
    sess.run(tf.initialize_all_variables())
    load_net(sess, 'save/best.ckpt')
    while True:
        ret, frame = cap.read()
        local = get_face(frame)
        print(local)
        data = np.zeros((len(local), 96, 96, 3), np.uint8)
        for i in range(len(local)):
            try:
                # img2 = frame[local[i][0]:local[i][1], local[i][2]:local[i][3]]
                cv2.rectangle(frame, (local[i][2], local[i][0]),
                              (local[i][3], local[i][1]), (255, 0, 0), 5)
                img = cv2.resize(
                    frame[local[i][0]:local[i][1], local[i][2]:local[i][3]],
                    (96, 96))
                img = np.array(img, np.float32)
                ave = np.mean(img)
                GCN = np.sqrt(np.mean(np.square(img - ave) + 1e-8))
                data[i] = (img - ave) / GCN
            except cv2.error:
                print("Flase")
        predict, w = sess.run([pre, name],
                              feed_dict={
                                  input: data,
                                  drop_input: 1.0
                              })
        for i in range(len(w)):
            print(new_students[w[i]])
            print(predict[i][w[i]])
            frame = cv2.putText(frame, new_students[w[i]],
                                (local[i][3], local[i][0]),
                                cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 255, 255),
                                2)
        cv2.imshow('img', frame)
        videoWriter.write(frame)
        cv2.waitKey(1)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Example #17
0
def epidemic_simulation(dir, time):
    # Create constants
    # create_constants(dir,time)

    output_file = open(path_to_folder + delivery_file_name, "w")
    output_file.write(
        "ID\ts\td\tts\tte\tLLC\tsize\tparent\tparentTime\treplica\tTV\tISM\tLTE\tCBRS\tenergy\n"
    )
    output_file.write("----------------------------------------------------\n")
    output_file.close()

    output_file2 = open(path_to_folder + notDelivered_file_name, "w")
    output_file2.write(
        "ID\ts\td\tts\tte\tLLC\tsize\tparent\tparentTime\treplica\tenergy\n")
    output_file2.write(
        "----------------------------------------------------\n")
    output_file2.close()

    #Load Link Exists
    LINK_EXISTS = pickle.load(open(Link_Exists_path + "LINK_EXISTS.pkl", "rb"))
    specBW = pickle.load(open(Link_Exists_path + "specBW.pkl", "rb"))

    output_file3 = open(path_to_folder + consumedEnergyFile, 'w')
    output_file3.write("Time\tEnergy\n")
    output_file3.close()

    #Create network
    net = network()
    #Fill network with datamules, sources, and destinations
    net.fill_network()
    #Create messages
    # path = "Bands/" + day + "/"
    # create_messages(path)

    path_to_mess_arr = Link_Exists_path.split('/')
    path_to_mess = path_to_mess_arr[0] + '/' + path_to_mess_arr[
        1] + '/' + path_to_mess_arr[2] + '/Day1/generated_messages.txt'

    with open(generated_messages_file, "r") as f:
        msg_lines = f.readlines()[1:]

    #Run simulation
    for i in range(T):
        if i % 20 == 0:
            print("TIME: " + str(i))
        net.network_GO(i, LINK_EXISTS, specBW, msg_lines)

    net.all_messages()
Example #18
0
 def __init__(self, tag):
     self.tempd = []
     self.gem_temp = []
     self.lightd = []
     self.gem_light = []
     self.s = 0
     self.max = 100
     self.min = 0
     self.x2 = 50
     self.y2 = 550 - 5 * 90
     self.set = True
     self.tl = True  # termperature of brightness. True == temperature
     self.color = 'red'
     self.tag = tag
     self.n = network()
     self.activeTab = 0
Example #19
0
def main(_):
    run_config = tf.ConfigProto()
    run_config.gpu_options.allow_growth = True

    #create graph, images, and checkpoints folder if they don't exist
    if not os.path.exists(args.checkpoints_path):
        os.makedirs(args.checkpoints_path)
    if not os.path.exists(args.graph_path):
        os.makedirs(args.graph_path)
    if not os.path.exists(args.images_path):
        os.makedirs(args.images_path)

    with tf.Session(config=run_config) as sess:
        model = network(args)

        print 'Start Training...'
        train(args, sess, model)
Example #20
0
    def __init__(self):
        self.graph = tf.Graph()

        with self.graph.as_default():
            self.mixture = tf.placeholder(
                tf.float32, [None, hp.frequency, hp.timestep, hp.num_channel],
                name='mixture')
            self.true_vocal = tf.placeholder(
                tf.float32, [None, hp.frequency, hp.timestep, hp.num_channel],
                name='true_vocal')

            gen, fake_D, real_D = network(self.mixture, self.true_vocal)

            self.wgan_loss = -tf.reduce_mean(real_D) + tf.reduce_mean(fake_D)
            self.gen_loss = -tf.reduce_mean(fake_D)
            self.l1_loss = tf.reduce_mean(tf.abs(self.true_vocal - gen))

            disc_op = tf.train.AdamOptimizer(learning_rate=0.0001)
            gen_op = tf.train.AdamOptimizer(learning_rate=0.0001)

            gen_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              scope="network/generator")
            disc_variables = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES,
                scope="network/discriminator")

            disc_grad = disc_op.compute_gradients(self.wgan_loss,
                                                  disc_variables)
            gen_grad = gen_op.compute_gradients(self.gen_loss + self.l1_loss,
                                                gen_variables)

            self.update_D = disc_op.apply_gradients(disc_grad)
            self.update_G = gen_op.apply_gradients(gen_grad)
            self.clip_D = [
                var.assign(tf.clip_by_value(var, -0.01, 0.01))
                for var in disc_variables
            ]

            tf.summary.scalar("generator_loss", self.gen_loss + self.l1_loss)
            tf.summary.scalar("discriminator_loss", self.wgan_loss)

            tf.summary.image("true_mixture", self.mixture)
            tf.summary.image("true_vocal", self.true_vocal)
            tf.summary.image("generated_vocal", gen)

            self.merged = tf.summary.merge_all()
def main(_):
    run_config = tf.ConfigProto()
    run_config.gpu_options.allow_growth = True

    #create graph, images, and checkpoints folder if they don't exist
    if not os.path.exists(args.checkpoints_path):
        os.makedirs(args.checkpoints_path)
    if not os.path.exists(args.graph_path):
        os.makedirs(args.graph_path)
    if not os.path.exists(args.images_path):
        os.makedirs(args.images_path)

    with tf.Session(config=run_config) as sess:
        model = network(args)

        print 'Start Training...'
        train(args, sess, model)
Example #22
0
 def __init__(self):
     #if config.use_opening_library:
     #    self.opening_tree = {}
     #    for color in "WB":
     #        self.opening_tree[color + "0"] = cgos_opening_tree.OpeningTree("opening%s_0.dat" % color, None)
     #        self.opening_tree[color] = cgos_opening_tree.OpeningTree("opening%s.dat" % color, None)
     self.engine = simple_go.Game(9)
     self.master = GTP_controller(sys.stdin, sys.stdout)
     self.version = version.number
     #self.name = version.name + version.message
     self.name = version.name
     self.komi = 0.0
     self.handicap = 0
     self.capture_all_dead = False
     log_name = get_next_filename("gtpc%04i.log")
     self.log_fp = open(log_name, "w")
     self.clock = None
     self.move_generated = False
     
     komi = 6.5
     self.engine.set_komi(komi)
     
     # note formatting: follow newline with tab
     self.cmd_options = {"help" : "Can also use '-h'. Displays this message.",
                         "message=" : "Message robot gives on new games\n\tQuotes required for spaces.",
                         "node_limit=" : "Positive integer\n\tSpecifies the normal depth of the search for a 9x9 game.",
                         "manage_time" : "If passed to program, uses time management.",
                         "capture_all_dead" : "If passed to program, will capture all dead before passing"}
     
     self.mode = None
     self.randomcount = 0
     
     print 'ready to setting '
     
     #jobmgr, network ¼³Á¤.
     self.m_network = network()
     self.m_network.init2()
     
     self.engine.network = self.m_network
     
     self.m_jobmgr = jobmgr2(self.mode)
     
     print 'setting is done'
Example #23
0
def main():
    params = NetConfig('config.yaml')
    print('-----------------> preparing DataLoader')
    dataset_args = params.hyperparameters['dataset']
    loader_args = params.hyperparameters['loader']
    batch_size = params.hyperparameters['network']['net']['batch_size']
    name = params.hyperparameters['network']['net']['name']
    discription = params.discription
    save_path = params.save_path
    epochs = params.epochs

    train_loader = torch.utils.data.DataLoader(
        dataset=ImageDataset(**dataset_args),
        batch_size=batch_size,
        **loader_args)
    print('-----------------> preparing model: {}'.format(name))
    net = network(params.hyperparameters['network'])
    coach = trainer(net, save_path, name, discription)
    coach.train(data_loader=train_loader, epochs=epochs)
    print('-----------------> start training')
Example #24
0
def validating(network, loader):
    """
    Validating the network during and after training

    :param network: trained network
    :param loader: dataloader
    :return: accuracy
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    correct_num = 0
    total_num = 0
    for i, data in enumerate(loader, 0):
        digits, labels = data
        total_num += labels.size(0)
        digits, labels = digits.to(device), labels.to(device)
        outputs = network(digits)
        _, predicted = torch.max(outputs, 1)
        correct_num += ((predicted == labels).sum().to("cpu")).item()
    accuracy = correct_num / total_num
    return accuracy
Example #25
0
    def __init__(self):
        self.graph = tf.Graph()

        with self.graph.as_default():
            self.x = tf.placeholder(tf.float32, [None, hp.timestep, 1], name='X')
            self.y = tf.placeholder(tf.float32, [None, hp.timestep, 1], name='Y')
            if hp.use_mulaw:
                label = mu_law_encode(self.y)
            else:
                label = self.y

            output = network(self.x, use_mulaw=hp.use_mulaw)

            if hp.use_mulaw:
                self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=label))
            else:
                self.loss = tf.reduce_mean(tf.abs(output - label))

            self.train_op = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(self.loss)

            tf.summary.scalar("loss", self.loss)
            self.merged = tf.summary.merge_all()
Example #26
0
File: Server.py Project: PassEr1/P1
def clientthread(conn):
    #Sending message to connected client
    connection_serv = network("server")
    connection_serv.start_session_server()
    print "------------------------------------"

    #infinite loop so that function do not terminate and thread do not end.
    while True:

        #Receiving from client
        data=conn.recv(1024)
        print data
        client_request=connection_serv.dec("Key_server",data)
        print str(client_request) +"this is the client req"
        reply = answer(client_request)
        if not client_request:
            print "data is empty!!!"
            break

        conn.sendall(reply)

    #came out of loop
    conn.close()
Example #27
0
def test4():
    game = simple_go.Game(9)     
    
    komi = 6.5
    game.set_komi(komi)
    
    #현재상태.
    game =  load_sgf.load_file("sgfs/test.sgf")
    print str(game.current_board)
    
    #network test
    m_network = network()
    m_network.init2()
    t1 = time.time()
    col, row = m_network.predict(game, type = 'SLPOLICY')
    t2 = time.time()    
    print 'col : ' + str(col) + ', row : ' + str(row) + "  takes : " + str(t2 - t1)
    
    game.network = m_network
    
    valunetresult = m_network.predict_valuenet(game)
    print 'valunetresult : ' + str(valunetresult)
    
    print 'main ended'
Example #28
0
from game import *
from mcts import *
from network import *
import numpy as np
import tensorflow as tf

# 加载模型
model = network()
model.load_weights("model-3cnn-3000")

# 进行人机对战,n_playout是模拟次数,越大结果越好
mcts = MCTS(n_playout=2000)
mcts.human_play(model=model, use_model=True)
from network import *
from matplotlib import pyplot as plt

T = 10  #duration of simulation
delta = 0.01  #bin sizes
iterations = int(T / delta)
N = 200  #number of neurons

model = network(N=N, delta=delta, lambda_junction=0.02)

spike_train = []
voltages = []
spike_events = [[] for i in range(N)]

for i in range(iterations):

    s, v = model.step()
    spike_train.append(s.copy())
    voltages.append(v.copy())

    for j in np.where(s)[0]:  #convert spike into event

        spike_events[j].append(i * delta)

spike_train = np.array(spike_train).transpose()  #neuron x time
voltages = np.array(voltages).transpose()  #neuron x time

plt.figure(1)
plt.eventplot(spike_events, color='black')
plt.xlabel('time (s)')
plt.ylabel('neuron #')
Example #30
0
        return False


    def remove_url(self,url):
        pass




conn = socket.socket()         # Create a socket object
host ='127.0.0.1' #TODO: Get local machine name
port = 12345                # Reserve a port for your service.

conn.connect((host, port))
while True:
    connection_client = network("client")
    connection_client.start_session_server()
    print "+++++++++++++++++++++++++++++++"
    data="register/Admin/AD12345"
    datatosend = connection_client.enc("key_server",data)
    print connection_client.dec("Key_server",datatosend)
    conn.send(datatosend)
    server_reply=connection_client.dec(conn.recv(1024))
    print server_reply
    break
conn.closeI()

#program_worker=protocol()
#program_worker.add_data_tojson("TheBank/3/amit/123/my id!!!")
Example #31
0
import mnist_loader
import network

trainingData, validationData, testingData = \
    mnist_loader.loadDataWrapper()
net = network([784, 30,10])
net.SGD(trainingData, 30, 10,3.0, testingData=testingData)
Example #32
0
from network import *

test = network(5000)

test.start()

while 1:
    data = test.read()
    if data != "":
        if ord(data[0]) != 13:
            print 'Data received : ', data
        test.send_to_base("ok !")
Example #33
0
def run_experiments(M_values,
                    d_values,
                    ds_size=1000,
                    fixed_N=False,
                    N=None,
                    use_square_error=False,
                    nrof_repeats=1,
                    outname=None):

    if fixed_N:  # What do we mean by fixed_N
        assert N is not None, "Need to provide a value for N!"
        print("Running experiments with fixed N={}.\n\n".format(N))
        M_values = M_values[M_values <= N]
        N_values = np.array([N for _ in M_values])
    else:
        print("Running experiments with N=2M for different values of M.\n\n")
        N_values = np.array([2 * M for M in M_values])

    for d in d_values:
        # For every dimension in the dimension array(which itself is an array of power of 2s)
        X = np.array([get_random_on_shpere(d=d) for _ in range(ds_size)])
        print("For d={} we get an X of ".format(d))
        print(X)
        #return
        for on_sphere in [True, False]:

            if on_sphere:
                continue
                print(
                    "d={}, original weights sampled on hypersphere.".format(d))
            else:
                print(
                    "d={}, original weights sampled from Gaussian.".format(d))
                print

            errors_dict = {}

            for M in M_values:

                if not fixed_N:
                    N = 2 * M

                if M > N:
                    continue

                yhats_dict = {}

                y_nw = network(N=N,
                               d=d,
                               on_sphere=on_sphere,
                               nrof_repeats=nrof_repeats)
                y = y_nw(X)

                mu = 1 / 2 * np.sqrt(2 / np.pi)
                yhats_dict["constant"] = np.array([mu for _ in X])

                yhat_nw = network(N=M, d=d, on_sphere=on_sphere)
                yhats_dict["random weights"] = yhat_nw(X)

                yhat_nw = network(N=M, d=d,
                                  on_sphere=on_sphere).with_opposite_weights()
                yhats_dict["random opposites weights"] = yhat_nw(X)

                yhat_nw = network(N=M, d=d,
                                  on_sphere=on_sphere).subsample_network(y_nw)
                yhats_dict["subsample"] = yhat_nw(X)

                yhat_nw = network(N=M, d=d,
                                  on_sphere=on_sphere).approximate_network(
                                      y_nw, rescale=False)
                yhats_dict["sum weights locally"] = yhat_nw(X)

                yhat_nw = network(
                    N=M, d=d, on_sphere=on_sphere).approximate_network(y_nw)
                yhats_dict["sum weights locally + rescale"] = yhat_nw(X)

                yhat_nw = network(
                    N=M, d=d,
                    on_sphere=on_sphere).greedy_forward_selection(y_nw, X)
                yhats_dict["forward selection"] = yhat_nw(X)

                # yhat_nw_merge = network(N=M, d=d, on_sphere=on_sphere).merge_network_weights(y_nw, keep_norm=False)
                # # yhat_nw_merge = network(N=M, d=d, on_sphere=on_sphere).merge_network_weights(y_nw, keep_norm=True)
                # yhats_dict["merge"] = yhat_nw_merge(X)
                # yhats_dict["merge + correction"] = yhat_nw_merge(X)*((N/M)**(1/2))

                for key, yhat in yhats_dict.items():
                    if use_square_error:
                        error = np.mean((y - yhat)**2)
                    else:
                        error = np.mean(np.abs(y - yhat))
                    if key not in errors_dict.keys():
                        errors_dict[key] = []
                    errors_dict[key].append(error)

            plt.figure(figsize=(12, 6))

            color_dict = {  # "constant": "silver",
                "random weights": "blue",
                # "random opposites weights": "darkblue",
                "subsample": "purple",
                "sum weights locally": "orange",
                "sum weights locally + rescale": "red",
                # "merge": "red",
                # "merge + correction": "brown",
                "forward selection": "aqua"
            }
            for key, errors in errors_dict.items():
                if key not in color_dict.keys():
                    continue
                plt.plot(M_values,
                         errors,
                         label=key,
                         color=color_dict.get(key, "black"))

            exp = 1. if use_square_error else 1 / 2
            label_str = "${}$" if use_square_error else "$\sqrt{{{}}}$"

            start_value = sigma**2
            plt.plot(M_values,
                     (start_value * (M_values**(-1.) + N_values**(-1.)))**exp,
                     '--',
                     color="blue",
                     alpha=0.4,
                     label=label_str.format("\sigma^2 (1/M + 1/N)"))

            start_value = sigma**2
            plt.plot(M_values,
                     (start_value * (M_values**(-1.) - N_values**(-1.)))**exp,
                     '--',
                     color="purple",
                     alpha=0.4,
                     label=label_str.format("\sigma^2 (1/M - 1/N)"))

            start_value = sigma**2
            plt.plot(M_values, (start_value * M_values**(-2.))**exp,
                     '--',
                     color="aqua",
                     alpha=0.4,
                     label=label_str.format("\sigma^2 / M^2"))

            scaling_exp = -4 / (d - 1)
            start_value = sigma**2
            plt.plot(M_values, (start_value * M_values**scaling_exp)**exp,
                     '--',
                     color="orange",
                     alpha=0.4,
                     label=label_str.format(
                         "\sigma^2 / M^{{4/(d-1)}}".format(scaling_exp)))

            plt.xscale("log")
            plt.xticks(M_values)
            plt.gca().xaxis.set_major_formatter(plt_ticker.ScalarFormatter())
            plt.yscale("log")
            plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
            plt.xlabel("M")
            if use_square_error:
                plt.ylabel("Squared Error")
                err_tit = "MSE"
                outerr = "mse"
            else:
                plt.ylabel("Absolute Error")
                err_tit = "Abs. Err."
                outerr = "abserr"
            if fixed_N:
                plt.title("{} for N={} and d={}".format(err_tit, N, d))
            else:
                plt.title("{} for N=2M and d={}".format(err_tit, d))
            plt.tight_layout()

            if outname is not None:
                plt.savefig(
                    os.path.join("{}", "{}_{}_d{}.png").format(
                        outfolder, outname, outerr, d))
            plt.show()
Example #34
0
                tile_num = math.ceil((opt.num_point * opt.batch_size) / point_num)
                indices_shuffle = np.tile(np.arange(point_num), tile_num)[0:opt.num_point * opt.batch_size]
                np.random.shuffle(indices_shuffle)
                indices_batch_shuffle = np.reshape(indices_shuffle, (opt.batch_size, opt.num_point, 1))

                input_data = torch.from_numpy(points_batch.astype(np.float32)) #(B,N,C), N is the point_num
                sampled_indices = torch.from_numpy(indices_batch_shuffle.astype(np.int64)) #(B,n,1), n is the num_point
                sampled_indices = sampled_indices.expand(-1,-1,input_data.shape[2]).contiguous()
                input_data = torch.gather(input_data, dim=1, index=sampled_indices) #(B,n,C)

                knn_idx = knn_builder.self_build_search(input_data[:,:,:3].detach())
                knn_idx = torch.from_numpy(knn_idx.astype(np.int64)).cuda()
                input_data = input_data.transpose(1,2).cuda() #(B,C,n)

                network.eval()
                score = network(input_data, knn_idx)
                preds = softmax(score) #(B,class,n)
                preds = preds.transpose(1,2) #(B,n, class)

                preds_2d = np.reshape(preds.cpu().detach().numpy(),(opt.num_point*opt.batch_size, -1))

                predictions = [(-1, 0.0)] * point_num
                for idx in range(opt.num_point * opt.batch_size):
                    point_idx = indices_shuffle[idx]
                    probs = preds_2d[idx, :]
                    confidence = np.amax(probs)
                    label = np.argmax(probs)
                    if confidence > predictions[point_idx][1]:
                        predictions[point_idx] = [label, confidence]
                labels_pred[batch_idx, 0:point_num] = np.array([label for label, _ in predictions])
                confidences_pred[batch_idx, 0:point_num] = np.array([confidence for _, confidence in predictions])
Example #35
0
File: test.py Project: Na-Z/PS-2Net
            if input_pc.shape[0] > opt.max_volumes:
                num_batches = math.ceil(input_pc.shape[0] / opt.max_volumes)
                # print('The number of resultant volumes (%d) is larger than max_volumes (%d), processing in %d batches'
                #      %(input_pc.shape[0], opt.max_volumes, num_batches))
                pred = []

                for j in range(num_batches):
                    start_idx = j * opt.max_volumes
                    if j != num_batches - 1:
                        end_idx = (j + 1) * opt.max_volumes
                    else:
                        end_idx = input_pc.shape[0] + 1
                    pc_slice = input_pc[start_idx:end_idx, :, :]
                    knn_slice = knn_idx[start_idx:end_idx, :, :]

                    score_slice = network(pc_slice, knn_slice)
                    pred_slice = softmax(score_slice)
                    pred.append(pred_slice)
                pred = torch.cat(pred, dim=0)
            else:
                score = network(input_pc, knn_idx)
                pred = softmax(score)

            # accumulate accuracy
            _, predicted_label = torch.max(pred.detach(), dim=1, keepdim=False)

            predicted_label_total.append(predicted_label.cpu().detach())
            gt_label_total.append(input_label.cpu().detach())

        # compute iou
        predicted_label_total = torch.cat(predicted_label_total,
Example #36
0
output_file2.write("----------------------------------------------------\n")
output_file2.close()
#Load Link Exists
LINK_EXISTS = pickle.load(open(Link_Exists_path + "LINK_EXISTS.pkl", "rb"))
specBW = pickle.load(open(Link_Exists_path + "specBW.pkl", "rb"))
# LINK_EXISTS = createLinkExistenceADJ()
# print(LINK_EXISTS[3,4,])

#Create constants
# create_constants(time)

#Generate Messages
# create_messages()

#Create network
net = network()
#Fill network with datamules, sources, and destinations
net.fill_network()
#Create messages
# path = "Bands/" + day + "/"
# create_messages(path)

message_path_file = "../Bands" + str(max_nodes) + "/" + Link_Exists_path.split(
    "/")[2] + "/Day1/" + "generated_messages.txt"
print(message_path_file)
with open(message_path_file, "r") as f:
    msg_lines = f.readlines()[1:]

#Run simulation
for i in range(T):
    # print("TIME: " + str(i))
Example #37
0
for epoch in range(opt.nepoch):
    # train mode
    time_use1 = 0
    train_loss.reset()
    network.train()
    start = time.time()
    for i, data in enumerate(dataloader_train):
        optimizer.zero_grad()
        img, img_mimic, label, fn = data
        img = img.cuda()
        img_mimic = img_mimic.cuda()
        label = label.cuda()
        if len(img.shape) == 4:
            img = img.unsqueeze(1)

        pred = network(img, img_mimic)
        #pred_ill = torch.nn.functional.normalize(torch.sum(torch.sum(pred,2),2),dim=1)
        pred_ill = torch.nn.functional.normalize(pred, dim=1)
        loss = get_angular_loss(pred_ill, label)

        if i % 5 == 0:
            #print(pred_ill, label)
            print('epoch', epoch, 'i', i, loss)
        loss.backward()
        train_loss.update(loss.item())
        optimizer.step()
    time_use1 = time.time() - start
    try:
        vis.updateTrace(X=np.array([epoch]),
                        Y=np.array([train_loss.avg]),
                        win=win_curve,
Example #38
0
        ip = X @ self.old_weights.T
        ip_relu = relu(ip)
        loss = (current_target[:, None] - ip_relu)**2
        loss_per_weight = np.mean(loss, axis=0)
        best_weight_idx = np.argmax(-loss_per_weight)
        sel_subset.append(best_weight_idx)
        self.ws = self.old_weights[sel_subset]

        if len(sel_subset) < self.N:
            self.greedy_forward_selection(nw, X, sel_subset=sel_subset)

        return self


y = network(N=100, d=10)
print(y(get_random_on_shpere(d=10)))
print(y(get_random_on_shpere(d=10)))
print(y(get_random_on_shpere(d=10)))
X = np.array([get_random_on_shpere(d=10) for _ in range(1000)])
y(X)

# # Compare error for different models:

# ## Run experiments functionality

# In[21]:


def run_experiments(M_values,
                    d_values,
Example #39
0
    for epoch in range(10):
        running_loss = 0.0
        running_acc = 0.0
        count = 0
        for i, data in enumerate(data_loader_train):
            count += 1
            # get the inputs
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)
            # print('i %d', i)
            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            outputs = network(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()
            _, predicted = torch.max(outputs, 1)
            running_acc += torch.sum(predicted == labels)
            if i % 200 == 0:  # print every 2000 mini-batches
                print('[%d, %5d] loss: %.3f, accuracy: %.3f' %
                      (epoch + 1, i + 1, running_loss / count,
                       running_acc / count))
                running_loss = 0.0
                running_acc = 0.0
                count = 0
Example #40
0
#
import network

net = network()