示例#1
0
    def setup(self, window_size=20, t_in=2, w=10, h=10, d=1, t_out=1, hidden_layers_sizes=[100], pretrain_step=1):
        self.bed = TestBed(window_size=window_size, t_in=t_in, w=w, h=h, d=d, t_out=t_out, hidden_layers_sizes=hidden_layers_sizes)
        self.gen = SinGenerator(w=w, h=h, d=1)
        # self.gen = RadarGenerator('../data/radar', w=w, h=h, left=0, top=80)
        self.vis = Visualizer(w=w, h=h, t_out=t_out)
        self.pretrain_step = pretrain_step

        # fill the window with data
        for i in xrange(window_size):
            y = self.gen.next()
            self.bed.supply(y)
示例#2
0
        x, mask, _ = self.model.prepare_data(x, None)
        y = self.f_predict(x, mask) # f_predict returns output of (n_timesteps, 1, n_feature_maps, height, width)
        y = y.swapaxes(0,1)[0]      # so we need to swap axes and get (n_timesteps, n_feature_maps, height, width)
        print('y.shape={0}'.format(y.shape))
        y = y.reshape((self.t_out, self.d, self.h, self.w))
        return y

    def save_params(self):
        params = self.model.params
        # TODO


if __name__ == '__main__':
    bed = TestBed()
    # gen = ConstantGenerator(w=bed.w, h=bed.h, d=bed.d)
    gen = SinGenerator(w=bed.w, h=bed.h, d=bed.d)
    # gen = RadarGenerator("../data/radar", w=bed.w, h=bed.h)

    # fill the window with data
    for i in xrange(bed.window_size):
        y = gen.next()
        bed.supply(y)

    for i,y in enumerate(gen):
        # predict
        y_pred = bed.predict()
        print("{0}: y={1}, y_pred={2}".format(i, y, y_pred))

        bed.supply(y)

        # if i % pretrain_step == 0 and 0 < self.pretrain_epochs:
示例#3
0
class Worker(QtCore.QThread):

    started = QtCore.Signal()
    updated = QtCore.Signal(numpy.ndarray, numpy.ndarray)
    stopped = QtCore.Signal()

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        self.bed = None
        self.gen = None

        self.delay = 1.0
        self.stop_flg = False
        self.mutex = QtCore.QMutex()

    def setup(self, window_size=20, t_in=2, w=10, h=10, d=1, t_out=1, hidden_layers_sizes=[100], pretrain_step=1):
        self.bed = TestBed(window_size=window_size, t_in=t_in, w=w, h=h, d=d, t_out=t_out, hidden_layers_sizes=hidden_layers_sizes)
        self.gen = SinGenerator(w=w, h=h, d=1)
        # self.gen = RadarGenerator('../data/radar', w=w, h=h, left=0, top=80)
        self.vis = Visualizer(w=w, h=h, t_out=t_out)
        self.pretrain_step = pretrain_step

        # fill the window with data
        for i in xrange(window_size):
            y = self.gen.next()
            self.bed.supply(y)

    def setGeneratorParams(self, k, n):
        pass

    def setDelay(self, delay):
        self.delay = delay

    def setLearningParams(self, params):
        self.finetune_epochs = params['finetune_epochs']
        self.finetune_lr = params['finetune_lr']
        self.finetune_batch_size = params['finetune_batch_size']
        self.pretrain_epochs = params['pretrain_epochs']
        self.pretrain_lr = params['pretrain_lr']
        self.pretrain_batch_size = params['pretrain_batch_size']

    def stop(self):
        with QtCore.QMutexLocker(self.mutex):
            self.stop_flg = True

    def run(self):
        print("Worker: started")
        with QtCore.QMutexLocker(self.mutex):
            self.stop_flg = False
        self.started.emit()

        for i,yt in enumerate(self.gen):
            # predict
            y_preds = self.bed.predict()
            print("{0}: yt={1}, y_pred={2}".format(i, yt, y_preds))

            self.bed.supply(yt)
            self.vis.append_data(yt, y_preds)

            if i % self.pretrain_step == 0 and 0 < self.pretrain_epochs:
                # pretrain
                avg_cost = self.bed.pretrain(self.pretrain_epochs, learning_rate=self.pretrain_lr, batch_size=self.pretrain_batch_size)
                print("   pretrain cost: {0}".format(avg_cost))
                pass

            # finetune
            costs = self.bed.finetune(self.finetune_epochs, learning_rate=self.finetune_lr, batch_size=self.finetune_batch_size)
            train_cost, valid_cost, test_cost = costs
            print("   train cost: {0}".format(train_cost))

            self.vis.append_cost(train_cost, valid_cost, test_cost)

            self.updated.emit(yt, y_preds)

            time.sleep(self.delay)

            if self.stop_flg:
                print(' --- iteration end ---')
                break

        self.stopped.emit()