Beispiel #1
0
 def __init__(
         self,
         call_freq,
         x,  # other params
 ):
     self._call_freq = call_freq
     x = K.format_data(x)
     self._x = [x]
     self._counter = 0
Beispiel #2
0
    def __init__(self,
                 tr_x=None,
                 tr_y=None,
                 va_x=None,
                 va_y=None,
                 te_x=None,
                 te_y=None,
                 batch_size=100,
                 call_freq=3,
                 metrics=['categorical_error'],
                 dump_path='validation.p',
                 verbose=1):
        # init values
        self._batch_size_ = batch_size
        self._call_freq_ = call_freq
        self._metrics_ = to_list(metrics)
        self._dump_path_ = dump_path
        self._verbose_ = verbose
        self._r_ = self._init_result()

        # judge if train or valid or test data exists
        self._is_tr_ = self._is_data(tr_x, tr_y)
        self._is_va_ = self._is_data(va_x, va_y)
        self._is_te_ = self._is_data(te_x, te_y)

        # format data, to list
        if self._is_tr_:
            self._tr_x_ = [K.format_data(e) for e in to_list(tr_x)]
            self._tr_y_ = [K.format_data(e) for e in to_list(tr_y)]
        if self._is_va_:
            self._va_x_ = [K.format_data(e) for e in to_list(va_x)]
            self._va_y_ = [K.format_data(e) for e in to_list(va_y)]
        if self._is_te_:
            self._te_x_ = [K.format_data(e) for e in to_list(te_x)]
            self._te_y_ = [K.format_data(e) for e in to_list(te_y)]
Beispiel #3
0
    def predict(self, x, batch_size=100):
        # format data
        x = to_list(x)
        x = [K.format_data(e) for e in x]

        # compile predict model
        if not hasattr(self, '_f_predict'):
            self._f_predict = K.function_no_given(self._in_nodes_,
                                                  self._tr_phase_node_,
                                                  self._out_nodes_)

        # do predict
        # put all data in GPU
        if batch_size is None:
            in_list = x + [0.]
            y_out = self._f_predict(*in_list)
        # put batch data in GPU
        else:
            N = len(x[0])
            batch_num = int(np.ceil(float(N) / batch_size))
            n_out_nodes = len(self._out_nodes_)
            y_out = [[] for e in self._out_nodes_]
            for i1 in xrange(batch_num):
                in_list = [
                    e[i1 * batch_size:min((i1 + 1) * batch_size, N)] for e in x
                ] + [0.]
                batch_y_out = self._f_predict(*in_list)
                for j1 in xrange(n_out_nodes):
                    y_out[j1].append(batch_y_out[j1])

            # get y_out
            y_out = [np.concatenate(e, axis=0) for e in y_out]

        if len(y_out) == 1:
            return y_out[0]
        else:
            return y_out
Beispiel #4
0
    def fit(self,
            x,
            y,
            batch_size=100,
            n_epochs=10,
            loss_func='categorical_crossentropy',
            optimizer=SGD(lr=0.01, rho=0.9),
            clip=None,
            callbacks=[],
            shuffle=True,
            verbose=1):
        x = to_list(x)
        y = to_list(y)

        # format
        x = [K.format_data(e) for e in x]
        y = [K.format_data(e) for e in y]

        # shuffle data
        if shuffle:
            x, y = supports.shuffle(x, y)

        # check data
        self._check_data(y, loss_func)

        # init gt_nodes
        self._gt_nodes_ = [K.placeholder(e.ndim) for e in y]

        # memory usage
        print "Train",
        self._show_memory_usage(self._layer_list_, batch_size)

        # default objective
        if type(loss_func) is str:
            assert len(self._out_nodes_)==len(self._gt_nodes_), "If you are using default objectives, " \
                                            + "out_node of out_layers must match ground truth!"
            loss_node = sum([
                obj.get(loss_func)(pred_node,
                                   gt_node) for pred_node, gt_node in zip(
                                       self._out_nodes_, self._gt_nodes_)
            ])
        # user defined objective
        else:
            loss_node = loss_func(self._out_nodes_, self._any_nodes_,
                                  self._gt_nodes_)
            #loss_node = loss_func( self )

        # gradient
        gparams = K.grad(loss_node + self._reg_value_, self._params_)

        # todo clip gradient
        if clip is not None:
            gparams = [K.clip(gparam, -clip, clip) for gparam in gparams]

        # gradient based opt
        param_updates = optimizer.get_updates(self._params_, gparams)

        # get all updates
        updates = param_updates + self._inner_updates_

        # compile for callback
        if callbacks is not None:
            callbacks = to_list(callbacks)
            for callback in callbacks:
                callback.compile(self)

        # compile model
        input_nodes = self._in_nodes_ + self._gt_nodes_
        output_nodes = [loss_node]
        f = K.function_no_given(input_nodes, self._tr_phase_node_,
                                output_nodes, updates)

        # train
        N = len(x[0])
        batch_num = int(np.ceil(float(N) / batch_size))
        n_abs_epoch = n_epochs + self._epoch_

        # callback
        print '\n0th epoch:'
        for callback in callbacks:
            if (self._epoch_ % callback.call_freq == 0):
                callback.call()

        while self._epoch_ < n_abs_epoch:
            self._epoch_ += 1

            # train
            t1 = time.time()
            loss_list = []
            for i2 in xrange(batch_num):
                batch_x = [
                    e[i2 * batch_size:min((i2 + 1) * batch_size, N)] for e in x
                ]
                batch_y = [
                    e[i2 * batch_size:min((i2 + 1) * batch_size, N)] for e in y
                ]
                in_list = batch_x + batch_y + [1.]
                loss = f(*in_list)[0]  # training phase
                loss_list.append(loss)
                if verbose == 1:
                    self._print_progress(self._epoch_, batch_num, i2)
                if verbose == 2:
                    self._print_progress_loss(self._epoch_, batch_num, i2,
                                              loss)
            t2 = time.time()
            self._tr_time_ += (t2 - t1)
            if verbose != 0:
                print '\n', '    tr_time: ', "%.2f" % (
                    t2 - t1), 's'  # print an empty line

            # callback
            for callback in callbacks:
                if (self._epoch_ % callback.call_freq == 0):
                    callback.call()
Beispiel #5
0
def format_data_list(x):
    return [K.format_data(e) for e in x]