Example #1
0
 def __init__(self):
     super().__init__()
     self._stub = self.connect()
     self.network = Network(self)
     self.update_blockheight()
     self.set_info()
     self.public_active_channels = self.get_open_channels(public_only=True, active_only=True)
Example #2
0
 def __init__(self, num_layers, heads, head_conv, down_ratio, weight_path):
     self.device = torch.device(
         "cuda:0" if torch.cuda.is_available() else "cpu")
     self.net = Network(num_layers, heads, head_conv,
                        down_ratio).to(self.device)
     self.net.load_state_dict(torch.load(weight_path))
     self.net.eval()
     self.down_ratio = down_ratio
Example #3
0
 def test_save_and_load_model(self):
     self.network.define_model()
     first_prediction = self.network.batch_prediction(np.array([[1,1]]))
     self.network.save_network('saved_model')
     self.second_network = Network(2,2)
     self.second_network.load_network('saved_model')
     second_prediction = self.second_network.batch_prediction(np.array([[1,1]]))
     self.assertTrue((first_prediction == second_prediction).all())
Example #4
0
    def __init__(self, smarthome):
        super().__init__()
        if '.'.join(VERSION.split('.', 2)[:2]) <= '1.5':
            self.logger = logging.getLogger(__name__)
        try:
            self.shtime = Shtime.get_instance()
            self.handle_login = self.get_parameter_value('handle_login')
            try:
                self.dl = Http(timeout=self.get_parameter_value('timeout'),
                               hide_login=self.handle_login)
            except:
                self.dl = Http(hide_login=self.handle_login)
            self._items = []
            self._icals = {}
            self._ical_aliases = {}
            self._cycle = self.get_parameter_value('cycle')
            calendars = self.get_parameter_value('calendars')
            config_dir = self.get_parameter_value('directory')
        except Exception as err:
            self.logger.error('Problems initializing: {}'.format(err))
            self._init_complete = False
            return
        try:
            self._directory = '{}/{}'.format(self.get_vardir(), config_dir)
        except Exception:
            self._directory = '{}/var/{}'.format(smarthome.get_basedir(),
                                                 config_dir)
        self._directory = os.path.normpath(self._directory)
        try:
            os.makedirs(self._directory)
            self.logger.debug('Created {} subfolder in var'.format(config_dir))
        except OSError as e:
            if e.errno != errno.EEXIST:
                self.logger.error(
                    'Problem creating {} folder in {}/var'.format(
                        config_dir, smarthome.get_basedir()))
                self._init_complete = False
                return

        for calendar in calendars:
            if isinstance(calendar, dict):
                calendar = list("{!s}:{!s}".format(k, v)
                                for (k, v) in calendar.items())[0]
            if ':' in calendar and 'http' != calendar[:4]:
                name, _, cal = calendar.partition(':')
                calendar = cal.strip()
                self.logger.info(
                    'Registering calendar {} with alias {}.'.format(
                        Network.clean_uri(calendar, self.handle_login), name))
                self._ical_aliases[name.strip()] = calendar
            else:
                self.logger.info(
                    'Registering calendar {} without alias.'.format(
                        Network.clean_uri(calendar, self.handle_login)))
            calendar = calendar.strip()
            self._icals[calendar] = self._read_events(calendar)

        self.shtime = Shtime.get_instance()
Example #5
0
    def classifier(self, sx, qx, is_train, trainable=False,
                   eps=1e-8, scale=1e+1, normalized=True):
        proto_vec = Network.base_net(sx, is_train, reuse=False, trainable=trainable)
        query_vec = Network.base_net(qx, is_train, reuse=True, trainable=trainable)

        if normalized:
            proto_vec = proto_vec /
                    (tf.norm(proto_vec, axis=1, keepdims=True) + 1e-8) * scale
            query_vec = query_vec /
                    (tf.norm(query_vec, axis=1, keepdims=True) + 1e-8) * scale
Example #6
0
    def __init__(self, GameState):
        # Initialise pygame
        pygame.init()
        pygame.font.init()
        pygame.mixer.init()
        pygame.display.set_caption(self.caption)
        self.screen = pygame.display.set_mode(
            self.dimensions,
            pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)

        # Delegate
        self.net = Network()
        self.GameState = GameState
        self.state = MenuState(self)
Example #7
0
 def init(self):
     self.net = Network('localhost')
     pygame.init()
     self.build_board()
     self.build_main_menu()
     pygame.display.flip()
     self.main_menu()
Example #8
0
 def __init__(self, server_address, blockchain):
     super().__init__(server_address, MasterHandler)
     self.blockchain = blockchain
     rs = Network.get_relays()
     self.relays = [r[0] for r in rs]
     print('Server started on ' + server_address[0] + ':' +
           str(server_address[1]))
Example #9
0
    def test_from_json(self):
        fn = os.path.join(os.path.dirname(__file__), './test_network.json')
        with open(fn) as data_file:
            network_json = json.load(data_file)
            network = Network.from_json(network_json)

            self.check_network_and_json(network, network_json)
Example #10
0
 def _mac_check(self, item, item_type: str, leaf_item=None):
     if not Network.is_mac(self.get_iattr_value(item.conf, item_type)):
         self._log_item_error(
             item, "invalid {} attribute provided from {}".format(
                 item_type, item.path()))
         return False
     return True
Example #11
0
    def _update_calendars(self):
        for uri in self._icals:
            self._icals[uri] = self._read_events(uri)
            self.logger.debug('Updated calendar {0}'.format(
                Network.clean_uri(uri, self.handle_login)))

        if len(self._icals):
            self._update_items()
Example #12
0
 def __init__(self, relay=None):
     """initialize an http client to request the relay server
     if relay is None, random relay is chosen from Network
     """
     servers = Network.get_relays()
     if relay is None:
         relay = servers[randint(0, len(servers)-1)]
     self.url = 'http://'+relay[0]+':'+str(relay[1])+'/'
Example #13
0
 def __init__(self, frameset_size, num_actions, last_actions_size):
     super().__init__(num_actions)
     self.sample_size = 200
     self.num_epochs = 1
     self.epsilon = 1.0  #exploration rate
     self.epsilon_decay = 0.996
     self.epsilon_min = 0.01
     self.gamma = 0.95  #discount rate
     self.frameset_size = frameset_size
     self.learn_count = 0
     self.last_actions_size = last_actions_size
     self.num_actions = num_actions
     self.policy_network = Network(frameset_size,
                                   num_actions,
                                   last_actions_size=last_actions_size)
     self.target_network = Network(frameset_size,
                                   num_actions,
                                   last_actions_size=last_actions_size)
     self.target_network.weights = self.policy_network.weights
     self.target_network_update_rate = 5
     self.replay_memory = ReplayMemory(capacity=50000)
Example #14
0
 def __init__(self, server_address, blockchain):
     super().__init__(server_address, RelayHandler)
     self.blockchain = blockchain
     self.master = MasterClient()
     self.transactions = []
     # HTTP client relays, used to broadcast transactions
     self.relays = []
     for r in Network.get_relays():
         if r != server_address:
             self.relays.append(RelayClient(r))
     print('Server started on ' + server_address[0] + ':' +
           str(server_address[1]))
def main():
    logging.basicConfig(format=u'%(levelname)-8s [%(asctime)s] %(message)s',
                        level=logging.DEBUG)
    ########################################################

    n = Network()
    vk = VK(n)

    print('Token is: ' +
          vk.auth_direct(Config.USER_LOGIN, Config.USER_PASSWORD,
                         Config.APP_ID, Config.APP_SECRET))

    ########################################################
    logging.info("--- APP END ---")
    return
Example #16
0
    def _read_events(self,
                     ics,
                     username=None,
                     password=None,
                     prio=1,
                     verify=True):
        if ics.startswith('http'):
            _, _, cal = ics.partition('//')
            name = '{}.ics'.format(cal.split('/')[0].replace('.', '_'))
            for entry in self._ical_aliases:
                name = '{}.ics'.format(
                    entry) if ics == self._ical_aliases[entry] else name
            filename = '{}/{}'.format(self._directory, name)
            auth = 'HTTPBasicAuth' if username else None
            downloaded = self.dl.download(url=ics,
                                          local=filename,
                                          params={
                                              username: username,
                                              password: password
                                          },
                                          verify=verify,
                                          auth=auth)
            if downloaded is False:
                self.logger.error(
                    'Could not download online ics file {0}.'.format(ics))
                return {}
            self.logger.debug(
                'Online ics {} successfully downloaded to {}'.format(
                    Network.clean_uri(ics, self.handle_login), filename))
            ics = os.path.normpath(filename)
        try:
            ics = ics.replace('\\', os.sep).replace('/', os.sep)
            ics = '{}/{}'.format(self._directory,
                                 ics) if self._directory not in ics else ics
            with open(ics, 'r') as f:
                ical = f.read()
                self.logger.debug('Read offline ical file {}'.format(ics))
        except IOError as e:
            self.logger.error(
                'Could not open local ics file {0} (directory {1}): {2}'.
                format(ics, self._directory, e))
            return {}

        return self._parse_ical(ical, ics, prio)
Example #17
0
 def test_from_json(self):
     return
     fn = os.path.join(os.path.dirname(__file__), './test_network.json')
     settings = {
         'momentum': 0.99,
         'epoch': 5000,
         'log': 100,
         'error': 0.1,
         'rate': 0.2,
         'cost': Cost.SE
     }
     with open(fn) as data_file:
         network_json = json.load(data_file)
         network = Network.from_json(network_json)
         trainer = Trainer(network)
         start = time.time()
         error, epoch = trainer.XOR(settings)
         stop = time.time()
         print(stop - start)
Example #18
0
 def __init__(self, networkdb: 'NetworkDb', config: Config):
     self.networkdb: 'NetworkDb' = networkdb
     self.config: Config = config
     self.time: int = 0
     self.network: Network = Network(networkdb)
     self.trains: List[Train] = []
     self.start_minutes = {}
     if not self.config.deduce_schedule:
         self.read_trains()
     else:
         self.deduce_trains()
     self.all_routes = []
     self.passengers = None
     if config.num_passengers_per_route > 0:
         self.create_passengers()
     self.graph = None
     self.positions = None
     self.xlim = None
     self.ylim = None
     self.minute = -1
Example #19
0
    def __init__(self, window):
        self.window = window
        ## objects ##
        self.netevent = None
        self.apic = Calls()
        self.processor = ProcessEvent()
        self.network = Network(self.window)
        self.login = LoginDialog(self)
        self.friend = FriendDialog(self.apic, self.login)
        self.chat = Chat(self.window, self.network, self.apic, loop,
                         self.login)

        self.classroom_buttons = {}
        self.previous = None
        self.commands = {
            'button_login': self.login.show,
            'button_new': self.friend.show,
            'emoji_dialog': self.chat.dialog_emoji.start,
            'chat_send': self.chat.addMsgSelf,
            'emojize': self.chat.emojize
        }
Example #20
0
def run(args):

    x_dim = [76, 91, 75]
    y_dim = 10
    splits = [0.8, 0.1, 0.1]

    X_data = []
    Y_data = []

    num_examples = 10

    for i in range(1, num_examples + 1):
        f_x = os.path.join(args.dataset, f'X_Img_Values{i}.npy')
        f_y = os.path.join(args.dataset, f'YValues{i}.npy')

        X_data.append(torch.tensor(np.load(f_x)))
        Y_data.append(torch.tensor(np.load(f_y)))

    # (batch, channel, width, height)
    X_data = torch.cat(X_data, dim=0).view([-1, x_dim[2], *x_dim[:2]])
    Y_data = torch.cat(Y_data, dim=0).view([-1, y_dim])

    assert X_data.shape[0] == Y_data.shape[0]
    total_size = X_data.shape[0]

    X_train = X_data[:int(total_size * splits[0]), ...]
    Y_train = Y_data[:int(total_size * splits[0]), ...]

    X_dev = X_data[int(total_size * splits[0]):int(total_size *
                                                   (splits[0] + splits[1])),
                   ...]
    Y_dev = Y_data[int(total_size * splits[0]):int(total_size *
                                                   (splits[0] + splits[1])),
                   ...]

    X_test = X_data[int(total_size * (splits[0] + splits[1])):, ...]
    Y_test = Y_data[int(total_size * (splits[0] + splits[1])):, ...]

    train_dataset = Data.TensorDataset(X_train, Y_train)
    dev_dataset = Data.TensorDataset(X_dev, Y_dev)
    test_dataset = Data.TensorDataset(X_test, Y_test)

    train_loader = Data.DataLoader(dataset=train_dataset,
                                   batch_size=16,
                                   shuffle=True)
    dev_loader = Data.DataLoader(dataset=dev_dataset,
                                 batch_size=16,
                                 shuffle=False)
    test_loader = Data.DataLoader(dataset=test_dataset,
                                  batch_size=16,
                                  shuffle=False)

    net = Network(in_channels=x_dim[2], out_size=y_dim)
    if torch.cuda.is_available():
        net = nn.DataParallel(net)
        net.cuda()

    opt = torch.optim.Adam(net.parameters(), lr=0.001)
    loss_func = nn.MSELoss(reduction='mean')

    for epoch_index in range(20):
        st = time.time()

        torch.set_grad_enabled(True)
        net.train()
        for train_batch_index, (img_batch,
                                label_batch) in enumerate(train_loader):
            if torch.cuda.is_available():
                img_batch = img_batch.cuda()
                label_batch = label_batch.cuda()

            predict = net(img_batch)
            loss = loss_func(predict.float(), label_batch.float())

            net.zero_grad()
            loss.backward()
            opt.step()

        print('(LR:%f) Time of a epoch:%.4fs' %
              (opt.param_groups[0]['lr'], time.time() - st))

        # evaluation
        torch.set_grad_enabled(False)
        net.eval()
        total_loss = []
        total_sample = 0

        for dev_batch_index, (img_batch, label_batch) in enumerate(dev_loader):
            if torch.cuda.is_available():
                img_batch = img_batch.cuda()
                label_batch = label_batch.cuda()

            predict = net(img_batch)
            loss = loss_func(predict.float(), label_batch.float())

            total_loss.append(loss)
            total_sample += img_batch.size(0)

        mean_loss = sum(total_loss) / len(total_loss)

        print('[Test] epoch[%d/%d] loss:%.4f\n' %
              (epoch_index, 100, mean_loss.item()))
Example #21
0
    return param_group['lr']


logging.basicConfig(level=logging.INFO,
                    format='(%(asctime)s %(levelname)s) %(message)s',
                    datefmt='%d %b %H:%M:%S',
                    filename='logs/region_layer.log',
                    filemode='w')

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('(%(levelname)s) %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

net = Network(cfg.class_number)
#network parameters
# print(net)
if torch.cuda.is_available():
    net.cuda(cfg.cuda_num)

dataset = DataSet(cfg)
#train samples
train_sample_nb = len(dataset.train_dataset)
#batch numbers
train_batch_nb = len(dataset.train_loader)

test_sample_nb = len(dataset.test_dataset)
test_batch_nb = len(dataset.test_loader)

logging.info('Train batch[%d] sample[%d]' % (train_batch_nb, train_sample_nb))
Example #22
0
    train=True,
    transform=torchvision.transforms.ToTensor(),
    download=True)
test_data = torchvision.datasets.MNIST(
    root='./mnist/', transform=torchvision.transforms.ToTensor(), train=False)

train_loader = Data.DataLoader(dataset=train_data,
                               batch_size=128,
                               shuffle=True)
test_loader = Data.DataLoader(dataset=test_data, batch_size=128, shuffle=False)

train_batch_num = len(train_loader)
test_batch_num = len(test_loader)

net = Network(adjustable_mode=True,
              adj_kernel_size=4,
              adj_stride=4,
              adj_avgpool=True)
if torch.cuda.is_available():
    net = nn.DataParallel(net)
    net.cuda()

opt = torch.optim.Adam(net.parameters(), lr=0.001)
loss_func = nn.CrossEntropyLoss()

for epoch_index in range(10):
    st = time.time()

    torch.set_grad_enabled(True)
    net.train()
    for train_batch_index, (img_batch, label_batch) in enumerate(train_loader):
        if torch.cuda.is_available():
Example #23
0
 def setUp(self):
     self.network = Network(2, 2)
Example #24
0
    du_dt = g.batch_jacobian(u, tx)[..., 0]
    return du_dt


if __name__ == '__main__':
    """
    Test the physics informed neural network (PINN) model for the wave equation.
    """

    # number of training samples
    num_train_samples = 10000
    # number of test samples
    num_test_samples = 1000

    # build a core network model
    network = Network.build()
    network.summary()
    # build a PINN model
    pinn = PINN(network).build()

    # create training input
    tx_eqn = np.random.rand(num_train_samples, 2)
    tx_eqn[..., 0] = 4 * tx_eqn[..., 0]  # t =  0 ~ +4
    tx_eqn[..., 1] = 2 * tx_eqn[..., 1] - 1  # x = -1 ~ +1
    tx_ini = np.random.rand(num_train_samples, 2)
    tx_ini[..., 0] = 0  # t = 0
    tx_ini[..., 1] = 2 * tx_ini[..., 1] - 1  # x = -1 ~ +1
    tx_bnd = np.random.rand(num_train_samples, 2)
    tx_bnd[..., 0] = 4 * tx_bnd[..., 0]  # t =  0 ~ +4
    tx_bnd[..., 1] = 2 * np.round(tx_bnd[..., 1]) - 1  # x = -1 or +1
    # create training output

train_data = torchvision.datasets.MNIST(root='./mnist', train=True,
                                        transform=torchvision.transforms.ToTensor(),
                                        download=True)
test_data = torchvision.datasets.MNIST(root='./mnist/',
                                       transform=torchvision.transforms.ToTensor(),
                                       train=False)

train_loader = Data.DataLoader(dataset=train_data, batch_size=128, shuffle=True)
test_loader = Data.DataLoader(dataset=test_data, batch_size=128, shuffle=False)

train_batch_num = len(train_loader)
test_batch_num = len(test_loader)

net = Network()
if torch.cuda.is_available():
    net = nn.DataParallel(net)
    net.cuda()

opt = torch.optim.Adam(net.parameters(), lr=0.001)
loss_func = nn.CrossEntropyLoss()


for epoch_index in range(20):
    st = time.time()
    for train_batch_index, (img_batch, label_batch) in enumerate(train_loader):
        img_batch = Variable(img_batch)
        label_batch = Variable(label_batch)

        if torch.cuda.is_available():
Example #26
0
    train=True,
    transform=torchvision.transforms.ToTensor(),
    download=True)
test_data = torchvision.datasets.MNIST(
    root='./mnist/', transform=torchvision.transforms.ToTensor(), train=False)
train_loader = Data.DataLoader(dataset=train_data,
                               batch_size=cfg.batch_size,
                               shuffle=True)
test_loader = Data.DataLoader(dataset=test_data,
                              batch_size=cfg.batch_size,
                              shuffle=False)

train_batch_num = len(train_loader)
test_batch_num = len(test_loader)

net = Network()
if torch.cuda.is_available():
    net.cuda()

opt = torch.optim.Adam(net.parameters(),
                       lr=cfg.LR,
                       weight_decay=cfg.weight_decay)
loss_func = nn.CrossEntropyLoss()

if cfg.load_model:
    net.load_state_dict(torch.load(cfg.model_path))

for epoch_index in range(cfg.epoch):
    for train_batch_index, (img_batch, label_batch) in enumerate(train_loader):
        img_batch = Variable(img_batch)
        label_batch = Variable(label_batch)
Example #27
0
from flask import Flask

from lib.chain import Chain
from lib.network import Network
from lib.qbc_utils import QbcUtils

from modules.transactions.controllers import transactions_blueprint
from modules.mining.controllers import mining_blueprint
from modules.network.controllers import network_blueprint
from modules.chain.controllers import chain_blueprint

node = Flask(__name__)

QBC = Chain()
QBCN = Network()
QBCU = QbcUtils()
port = QBCU.get_port()

# Registering all the modules
node.register_blueprint(transactions_blueprint)

node.register_blueprint(mining_blueprint)

node.register_blueprint(network_blueprint)

node.register_blueprint(chain_blueprint)

# Discover full network and register on each of the nodes
network = QBCN.discover_network()
live_nodes = network["registered_nodes"]
Example #28
0
import torch
from torch import nn
import config as cfg
from lib.test_loader import TestDataSet
from tqdm import tqdm
from lib.metric import roc, f1_score_calc
import os
from lib.network import Network
import time
from lib.utils import count_parameters_in_MB

torch.cuda.set_device(cfg.CUDA_NUM)

test_set = TestDataSet(cfg)

net = Network()

print('Net contain %.2fMB' % count_parameters_in_MB(net))
net.cuda()

weight_path = os.path.join(cfg.weights_dir, 'net.pth')
net.load_state_dict(torch.load(weight_path))

# ------------------- Testing ------------------------
torch.set_grad_enabled(False)
net.eval()

pre_list = []
label_list = []

for test_img, test_label \
Example #29
0
class LndNode(Node):
    """
    Implements an interface to an lnd node.
    """
    def __init__(self):
        super().__init__()
        self._stub = self.connect()
        self.network = Network(self)
        self.update_blockheight()
        self.set_info()
        self.public_active_channels = self.get_open_channels(public_only=True, active_only=True)

    @staticmethod
    def connect():
        """
        Establishes a connection to lnd using the hostname, tls certificate,
        and admin macaroon defined in settings.
        """
        macaroons = True
        os.environ['GRPC_SSL_CIPHER_SUITES'] = '' + \
                                               'ECDHE-RSA-AES128-GCM-SHA256:' + \
                                               'ECDHE-RSA-AES128-SHA256:' + \
                                               'ECDHE-RSA-AES256-SHA384:' + \
                                               'ECDHE-RSA-AES256-GCM-SHA384:' + \
                                               'ECDHE-ECDSA-AES128-GCM-SHA256:' + \
                                               'ECDHE-ECDSA-AES128-SHA256:' + \
                                               'ECDHE-ECDSA-AES256-SHA384:' + \
                                               'ECDHE-ECDSA-AES256-GCM-SHA384'

        cert = open(os.path.expanduser(_settings.config['network']['tls_cert_file']), 'rb').read()

        if macaroons:
            with open(os.path.expanduser(_settings.config['network']['admin_macaroon_file']), 'rb') as f:
                macaroon_bytes = f.read()
                macaroon = codecs.encode(macaroon_bytes, 'hex')

            def metadata_callback(context, callback):
                # for more info see grpc docs
                callback([('macaroon', macaroon)], None)

            cert_creds = grpc.ssl_channel_credentials(cert)
            auth_creds = grpc.metadata_call_credentials(metadata_callback)
            creds = grpc.composite_channel_credentials(cert_creds, auth_creds)

        else:
            creds = grpc.ssl_channel_credentials(cert)

        channel = grpc.secure_channel(_settings.config['network']['lnd_grpc_host'], creds, options=[
            ('grpc.max_receive_message_length', 50 * 1024 * 1024)  # necessary to circumvent standard size limitation
        ])

        return lnrpc.LightningStub(channel)

    def update_blockheight(self):
        info = self._stub.GetInfo(ln.GetInfoRequest())
        self.blockheight = int(info.block_height)

    def get_channel_info(self, channel_id):
        channel = self._stub.GetChanInfo(ln.ChanInfoRequest(chan_id=channel_id))
        channel_dict = MessageToDict(channel, including_default_value_fields=True)
        channel_dict = convert_dictionary_number_strings_to_ints(channel_dict)
        return channel_dict

    @staticmethod
    def lnd_hops(hops):
        return [ln.Hop(**hop) for hop in hops]

    def lnd_route(self, route):
        """
        Converts a cleartext route to an lnd route.

        :param route: :class:`lib.route.Route`
        :return:
        """
        hops = self.lnd_hops(route.hops)
        return ln.Route(
            total_time_lock=route.total_time_lock,
            total_fees=route.total_fee_msat // 1000,
            total_amt=route.total_amt_msat // 1000,
            hops=hops,
            total_fees_msat=route.total_fee_msat,
            total_amt_msat=route.total_amt_msat
        )

    def self_payment(self, routes, amt_msat):
        """
        Do a self-payment along routes with amt_msat.

        :param routes: list of :class:`lib.route.Route` objects
        :param amt_msat:
        :return: payment result
        """
        invoice = self._stub.AddInvoice(ln.Invoice(value=amt_msat // 1000))
        result = self.send_to_route(routes, invoice.r_hash)
        logger.debug(result)
        return result

    def self_payment_zero_invoice(self, routes, memo):
        """
        Do a self-payment along routes with an invoice of zero satoshis.
        This helps to use one invoice for several rebalancing attempts.
        Adds a memo to the invoice, which can later be parsed for bookkeeping.

        :param routes: list of :class:`lib.route.Route` objects
        :param memo: str, Comment field for an invoice.
        :return: payment result
        """
        invoice = self._stub.AddInvoice(ln.Invoice(value=0, memo=memo))
        result = self.send_to_route(routes, invoice.r_hash)
        logger.debug(result)
        return result

    def get_invoice(self, amt_msat):
        """
        Fetches an already created invoice from lnd.

        :param amt_msat:
        :return: Hash of invoice preimage.
        """
        invoice = self._stub.AddInvoice(ln.Invoice(value=amt_msat // 1000))
        return invoice.r_hash

    def get_rebalance_invoice(self, memo):
        """
        Creates a zero amount invoice and gives back it's hash.

        :param memo: Comment for the invoice.
        :return: Hash of the invoice preimage.
        """
        invoice = self._stub.AddInvoice(ln.Invoice(value=0, memo=memo))
        return invoice.r_hash

    def send_to_route(self, routes, r_hash_bytes):
        """
        Takes bare route (list) and tries to send along it,
        trying to fulfill the invoice labeled by the given hash.

        :param routes: (list) of :class:`lib.routes.Route`
        :param r_hash_bytes: invoice identifier
        :return:
        """
        if type(routes) == list:
            lnd_routes = [self.lnd_route(route) for route in routes]
        else:
            lnd_routes = [self.lnd_route(routes)]
        request = ln.SendToRouteRequest(
            routes=lnd_routes,
            payment_hash_string=r_hash_bytes.hex(),
        )
        try:
            return self._stub.SendToRouteSync(request, timeout=5*60)  # timeout after 5 minutes
        except _Rendezvous:
            raise PaymentTimeOut

    def get_raw_network_graph(self):
        graph = self._stub.DescribeGraph(ln.ChannelGraphRequest())
        return graph

    def get_raw_info(self):
        """
        Returns specific information about this node.

        :return: node information
        """
        return self._stub.GetInfo(ln.GetInfoRequest())

    def set_info(self):
        """
        Fetches information about this node and computes total capacity,
        local and remote total balance, how many satoshis were sent and received,
        and some networking peer stats.
        """

        raw_info = self.get_raw_info()
        self.pub_key = raw_info.identity_pubkey
        self.alias = raw_info.alias
        self.num_active_channels = raw_info.num_active_channels
        self.num_peers = raw_info.num_peers

        # TODO: remove the following code and implement an advanced status
        all_channels = self.get_open_channels(active_only=False, public_only=False)

        for k, c in all_channels.items():
            self.total_capacity += c['capacity']
            self.total_local_balance += c['local_balance']
            self.total_remote_balance += c['remote_balance']
            self.total_satoshis_received += c['total_satoshis_received']
            self.total_satoshis_sent += c['total_satoshis_sent']
            if c['active']:
                self.total_active_channels += 1
            if c['private']:
                self.total_private_channels += 1

    def get_open_channels(self, active_only=False, public_only=False):
        """
        Fetches information (fee settings of the counterparty, channel capacity, balancedness)
         about this node's open channels and saves it into the channels dict attribute.

        :param active_only: bool, only take active channels into account (default)
        :param public_only: bool, only take public channels into account (off by default)
        :return: list of channels sorted by remote pubkey
        """
        raw_channels = self._stub.ListChannels(ln.ListChannelsRequest(active_only=active_only, public_only=public_only))
        channels_data = raw_channels.ListFields()[0][1]
        channels = OrderedDict()

        for c in channels_data:
            # calculate age from blockheight
            blockheight, _, _ = convert_channel_id_to_short_channel_id(c.chan_id)
            age_days = (self.blockheight - blockheight) * 10 / (60 * 24)
            # calculate last update (days ago)
            try:
                last_update = (time.time() - self.network.edges[c.chan_id]['last_update']) / (60 * 60 * 24)
            except TypeError:
                last_update = float('nan')
            except KeyError:
                last_update = float('nan')

            sent_received_per_week = int((c.total_satoshis_sent + c.total_satoshis_received) / (age_days / 7))
            # determine policy

            try:
                edge_info = self.network.edges[c.chan_id]
                if edge_info['node1_pub'] == self.pub_key:  # interested in node2
                    policy = edge_info['node2_policy']
                else:  # interested in node1
                    policy = edge_info['node1_policy']
            except KeyError:
                # TODO: if channel is unknown in describegraph we need to set the fees to some error value
                policy = {'fee_base_msat': -1,
                          'fee_rate_milli_msat': -1}

            # define unbalancedness |ub| large means very unbalanced
            commit_fee = 0 if not c.initiator else c.commit_fee
            unbalancedness = -(float(c.local_balance + commit_fee) / c.capacity - 0.5) * 2
            # inverse of above formula:
            # c.local_balance = c.capacity * 0.5 * (-unbalancedness + 1) - commit_fee

            channels[c.chan_id] = {
                'active': c.active,
                'age': age_days,
                'alias': self.network.node_alias(c.remote_pubkey),
                'amt_to_balanced': int(unbalancedness * c.capacity / 2 - commit_fee),
                'capacity': c.capacity,
                'chan_id': c.chan_id,
                'channel_point': c.channel_point,
                'commit_fee': c.commit_fee,
                'fee_per_kw': c.fee_per_kw,
                'peer_base_fee': policy['fee_base_msat'],
                'peer_fee_rate': policy['fee_rate_milli_msat'],
                'initiator': c.initiator,
                'last_update': last_update,
                'local_balance': c.local_balance,
                'num_updates': c.num_updates,
                'private': c.private,
                'remote_balance': c.remote_balance,
                'remote_pubkey': c.remote_pubkey,
                'sent_received_per_week': sent_received_per_week,
                'total_satoshis_sent': c.total_satoshis_sent,
                'total_satoshis_received': c.total_satoshis_received,
                'unbalancedness': unbalancedness,
            }
        sorted_dict = OrderedDict(sorted(channels.items(), key=lambda x: x[1]['alias']))
        return sorted_dict

    def get_inactive_channels(self):
        channels = self.get_open_channels(public_only=False, active_only=False)
        return {k: c for k, c in channels.items() if not c['active']}

    def get_unbalanced_channels(self, unbalancedness_greater_than=0.0):
        """
        Gets all channels which have an absolute unbalancedness
        (-1...1, -1 for outbound unbalanced, 1 for inbound unbalanced)
        larger than unbalancedness_greater_than.

        :param unbalancedness_greater_than: unbalancedness interval, default returns all channels
        :return: all channels which are more unbalanced than the specified interval
        """
        unbalanced_channels = {
            k: c for k, c in self.public_active_channels.items()
            if abs(c['unbalancedness']) >= unbalancedness_greater_than
        }
        return unbalanced_channels

    @staticmethod
    def timestamp_from_now(offset_days=0):
        """
        Determines the Unix timestamp from offset_days ago.

        :param offset_days: int
        :return: int, Unix timestamp
        """
        now = datetime.datetime.now()
        then = now - datetime.timedelta(days=offset_days)
        then = time.mktime(then.timetuple())
        return int(then)

    def get_forwarding_events(self, offset_days=300):
        """
        Fetches all forwarding events between now and offset_days ago.

        :param offset_days: int
        :return: lnd fowarding events
        """
        now = self.timestamp_from_now()
        then = self.timestamp_from_now(offset_days)

        forwardings = self._stub.ForwardingHistory(ln.ForwardingHistoryRequest(
            start_time=then,
            end_time=now,
            num_max_events=NUM_MAX_FORWARDING_EVENTS))

        events = [{
            'timestamp': f.timestamp,
            'chan_id_in': f.chan_id_in,
            'chan_id_out': f.chan_id_out,
            'amt_in': f.amt_in,
            'amt_out': f.amt_out,
            'fee_msat': f.fee_msat,
            'effective_fee': f.fee_msat / (f.amt_in * 1000)
        } for f in forwardings.forwarding_events]

        return events

    def get_closed_channels(self):
        """
        Fetches all closed channels.

        :return: dict, channel list
        """
        request = ln.ClosedChannelsRequest()
        closed_channels = self._stub.ClosedChannels(request)
        closed_channels_dict = {}
        for c in closed_channels.channels:
            closed_channels_dict[c.chan_id] = {
                'channel_point': c.channel_point,
                'chain_hash': c.chain_hash,
                'closing_tx_hash': c.closing_tx_hash,
                'remote_pubkey': c.remote_pubkey,
                'capacity': c.capacity,
                'close_height': c.close_height,
                'settled_balance': c.settled_balance,
                'time_locked_balance': c.time_locked_balance,
                'close_type': c.close_type,
            }
        return closed_channels_dict

    @staticmethod
    def handle_payment_error(payment_error):
        """
        Handles payment errors and determines the failed channel.

        :param payment_error:
        :return: int, channel_id of the failed channel.
        """
        if "TemporaryChannelFailure" in payment_error:
            logger.error("   Encountered temporary channel failure.")
            short_channel_groups = extract_short_channel_id_from_string(payment_error)
            channel_id = convert_short_channel_id_to_channel_id(*short_channel_groups)
            return channel_id

    def queryroute_external(self, source_pubkey, target_pubkey, amt_msat, ignored_nodes=(), ignored_channels=()):
        """
        Queries the lnd node for a route. Channels and nodes can be ignored if they failed before.

        :param source_pubkey: str
        :param target_pubkey: str
        :param amt_msat: int
        :param ignored_nodes: list of node pub keys
        :param ignored_channels: list of channel_ids
        :return: list of channel_ids
        """
        amt_sat = amt_msat // 1000

        # we want to see all routes:
        max_fee = 10000

        # convert ignored nodes to api format
        if ignored_nodes:
            ignored_nodes_api = [bytes.fromhex(n) for n in ignored_nodes]
        else:
            ignored_nodes_api = []

        # convert ignored channels to api format
        if ignored_channels:
            ignored_channels_api = [ln.EdgeLocator(channel_id=c) for c in ignored_channels]
        else:
            ignored_channels_api = []

        logger.debug(f"Ignored for queryroutes: channels: {ignored_channels_api}, nodes: {ignored_nodes_api}")

        request = ln.QueryRoutesRequest(
            pub_key=target_pubkey,
            amt=amt_sat,
            num_routes=1,
            final_cltv_delta=0,
            fee_limit=ln.FeeLimit(fixed=max_fee),
            ignored_nodes=ignored_nodes_api,
            ignored_edges=ignored_channels_api,
            source_pub_key=source_pubkey,
        )
        try:
            response = self._stub.QueryRoutes(request)
        except _Rendezvous:
            raise NoRouteError
        # print(response)
        # We give back only one route, as multiple routes will be deprecated
        channel_route = [h.chan_id for h in response.routes[0].hops]

        return channel_route

    def print_status(self):
        logger.info("-------- Node status --------")
        balancedness_local = self.total_local_balance / self.total_capacity
        balancedness_remote = self.total_remote_balance / self.total_capacity
        logger.info(f"alias: {self.alias}")
        logger.info(f"pub key: {self.pub_key}")
        logger.info(f"blockheight: {self.blockheight}")
        logger.info(f"peers: {self.num_peers}")
        logger.info(f"channels: {self.total_channels}")
        logger.info(f"active channels: {self.total_active_channels}")
        logger.info(f"private channels: {self.total_private_channels}")
        logger.info(f"capacity: {self.total_capacity}")
        logger.info(f"balancedness: l:{balancedness_local:.2%} r:{balancedness_remote:.2%}")
        logger.info(f"total satoshis received (current channels): {self.total_satoshis_received}")
        logger.info(f"total satoshis sent (current channels): {self.total_satoshis_sent}")

train_data = torchvision.datasets.MNIST(root='./mnist', train=True,
                                        transform=torchvision.transforms.ToTensor(),
                                        download=True)
test_data = torchvision.datasets.MNIST(root='./mnist/',
                                       transform=torchvision.transforms.ToTensor(),
                                       train=False)

train_loader = Data.DataLoader(dataset=train_data, batch_size=128, shuffle=True)
test_loader = Data.DataLoader(dataset=test_data, batch_size=128, shuffle=False)

train_batch_num = len(train_loader)
test_batch_num = len(test_loader)

net = Network()
print('net: ', net)
# if torch.cuda.is_available():
if False:
    net = nn.DataParallel(net)
    net.cuda()

opt = torch.optim.Adam(net.parameters(), lr=0.001)
loss_func = nn.CrossEntropyLoss()

for epoch_index in range(10):
    st = time.time()

    torch.set_grad_enabled(True)
    net.train()
    for train_batch_index, (img_batch, label_batch) in enumerate(train_loader):