Ejemplo n.º 1
0
 def __init__(self, rnn_type, consolidate_moment, consolidate_threshold, nin, nout, ninp, nhid, nlayers,
         max_memory_size, max_ltm_size, init_stm_size, lr, batch_size, clip, optimizer, ltm_reinstatement, stm_consolidation,
         train_weights_before_predict, weights_trainer, residual_weights_trainer=None, 
         tie_weights=False, w_window=20, recent_window_size=5, dropout=0.2, is_cuda = True):
     self.optimizers = []
     self.consolidate_moment = consolidate_moment
     self.consolidation_threshold = consolidate_threshold
     self.reinstatement_threshold = consolidate_threshold
     self.consolidate_moment_ticks = 1
     self.residual_weights_trainer = None
     self.ltm_size = 0
     self.memory_size = 0
     self.max_ltm_size = max_ltm_size
     self.init_stm_size = init_stm_size
     self.max_memory_size = max_memory_size
     self.stm_consolidation = stm_consolidation
     self.ltm_reinstatement = ltm_reinstatement
     self.recent_window_size = recent_window_size
     self.weight_history = []
     self.weights_copied = Observable()
     self.weights_moved = Observable()
     self.ltm_size_updated = Observable()
     super(CloneLearner, self).__init__(rnn_type, nin, nout, ninp, nhid, nlayers,
         max_memory_size, lr, batch_size, clip, optimizer, 
         train_weights_before_predict, weights_trainer, tie_weights, w_window, dropout, is_cuda)
     self.weights_updated.register(self.save_weights_history)
Ejemplo n.º 2
0
 def __init__(self, rnn_type, nin, nout, ninp, nhid, nlayers,
              max_memory_size, lr, batch_size, clip, optimizer,
              train_weights_before_predict, weights_trainer,
              learn_iterations, tie_weights, w_window, dropout, is_cuda):
     criterion = nn.CrossEntropyLoss()
     super(BaseMixtureOfRNNsLearner, self).__init__(criterion, nout,
                                                    learn_iterations)
     self.optimizer_algorithm = optimizer
     #general parameters
     self.rnn_type = rnn_type
     self.nin = nin
     self.nout = nout
     self.ninp = ninp
     self.nhid = nhid
     self.max_memory_size = max_memory_size
     self.dropout = dropout
     self.rnns = []
     self.ids = []
     self.id_count = 0
     self.lr = lr
     self.batch_size = batch_size
     self.hidden = []
     self.clip = clip
     self.window_size = w_window
     self.nlayers = nlayers
     self.train_weights_before_predict = train_weights_before_predict
     self.cuda_available = is_cuda
     self.outputs = torch.zeros(self.max_memory_size, 20, self.batch_size,
                                self.nout)
     self.weights_trainer = weights_trainer
     self.tie_weights = tie_weights
     self.weights_added = Observable()
     self.weights_removed = Observable()
     self.weights_updated = Observable()
     self._initialize_model()
Ejemplo n.º 3
0
def test_many_events_with_dictionary():
    subject = Observable()
    subject.on({  # setting events of an observable with dictionary
        'one': one_handler,
        'two': two_handler,
        'custom': CustomEvent(),
        'many': [one_handler, two_handler]})
    subject.trigger('one two custom many', 1, 2, a=3, b=4)
Ejemplo n.º 4
0
def test_custom_event():
    subject = Observable()
    custom_event = CustomEvent()
    subject.on('custom', custom_event)
    subject.on(['custom', 'custom_alias'], custom_event)  # same reference
    assert(subject.custom and subject.custom_alias is custom_event)
    assert(one_handler and two_handler in subject.custom.handlers)
    # update an event
    subject.on('custom', CustomEvent())
    assert(subject.custom is not custom_event)
Ejemplo n.º 5
0
def test_event_with_many_handlers_attached():
    subject = Observable()
    subject.on('many', [one_handler, two_handler])
    assert(subject.events['many'] is subject.many)
    assert(one_handler and two_handler in subject.many.handlers)
    # remove a handler
    subject.off('many', one_handler)
    assert(one_handler not in subject.many.handlers)
    # remove the event
    subject.off('many')
    assert('many' not in subject.events)
    with raises(AttributeError):
        subject.many
Ejemplo n.º 6
0
def test_event_with_handler_attached():
    subject = Observable()
    subject.on('one', one_handler)
    subject.on('two', two_handler)
    assert(subject.events['one'] is subject.one)
    assert(subject.events['two'] is subject.two)
    # trigger
    subject.one(1, 2, a=3, b=4)
    subject.one.trigger(1, 2, a=3, b=4)
    subject.trigger('one', 1, 2, a=3, b=4)
    subject.events['one'].trigger(1, 2, a=3, b=4)
    subject.trigger('one two', 1, 2, a=3, b=4)
    subject.trigger(['one', 'two'], 1, 2, a=3, b=4)
Ejemplo n.º 7
0
 def test_update(self):
     observable = Observable()
     observer = Observer(1)
     observable.addObserver(observer)
     observable.updateObservers(2)
     self.assertEqual(observer.data, 2)
Ejemplo n.º 8
0
"""Alarm Event names"""

from observer import Observable
import logging

LOGGER = logging.getLogger('alarm')

# Events
MOTION_DETECTED = 'motion_detected'

# Basic handlers
def log_it(*args, **kwargs):
    LOGGER.debug("%s, %s", args, kwargs)


# Setup events
observable = Observable()
observable.on(MOTION_DETECTED, [log_it])

Ejemplo n.º 9
0
def test_no_callable_handler():
    subject = Observable()
    with raises(TypeError):
        subject.on('error', NotCallable())
Ejemplo n.º 10
0
def test_events_with_same_handlers_attached():
    subject = Observable()
    subject.on('many many2', [one_handler, two_handler])
    assert(subject.many is not subject.many2)
    assert(one_handler and two_handler in subject.many.handlers)
    assert(one_handler and two_handler in subject.many2.handlers)
Ejemplo n.º 11
0
def test_events_with_same_handler_attached():
    subject = Observable()
    subject.on(['one', 'one2'], one_handler)
    assert(subject.one is not subject.one2)
    assert(one_handler in subject.one.handlers)
    assert(one_handler in subject.one2.handlers)
Ejemplo n.º 12
0
###############################################################################
# Build the model
###############################################################################

vocsize = len(corpora.vocabulary)
print('Vocabulary size:', vocsize)
print('Creating learner...')
learner = get_learner(args, vocsize)
print(f'Number of parameters: {learner.get_num_parameters()/1e6:1.2f}M')
if args.cuda:
    learner.cuda()
if args.load_from:
    learner.load_from(torch.load(args.load_from))

domain_switched = Observable()
timestep_updated = Observable()
learner.train_mode()
if args.log_weights:
    weight_logger = log_utils.WeightsLogger(learner, args.log_weights)
    domain_switched.register(weight_logger.domain_switched)
    timestep_updated.register(weight_logger.timestep_updated)

###############################################################################
# Training code
###############################################################################


def generate_text(sequence_id, position, sequence_type, sequence_type_name):
    learner.evaluate_mode()
    input = Variable(torch.rand(1, 1).mul(len(corpora.vocabulary)).long(),
Ejemplo n.º 13
0
            self.plotly_user_config['plotly_streaming_tokens'][0])
        self.stream.open()

    def notify(self, observable, *args, **kwargs):
        # write to plotly
        if 'value' in kwargs:
            # self.stream.write({'x': datetime.datetime.now(), 'y': kwargs['value']})
            self.stream.write({'x': kwargs['timestamp'], 'y': kwargs['value']})


# import plotly.plotly as py
# from plotly.graph_objs import *

# trace0 = Scatter(
#     x=[1, 2, 3, 4],
#     y=[10, 15, 13, 17]
# )
# trace1 = Scatter(
#     x=[1, 2, 3, 4],
#     y=[16, 5, 11, 9]
# )
# data = Data([trace0, trace1])

# unique_url = py.plot(data, filename = 'basic-line')

if __name__ == '__main__':
    subject = Observable()
    plyobs = plotlyObserver(subject, 'config.json', 'test stream plot')
    subject.notify_observers(value=45)
    subject.notify_observers(value=55)