Esempio n. 1
0
def fit2():
    print("fit2")
    model = Model([
        nn.Dense(128, inshape=1, activation='relu'),
        nn.Dense(256, activation='relu'),
        nn.Dropout(0.80),
        nn.Dense(1)
    ])
    model.assemble()

    sess = Session(
        model,
        loss=losses.Mse(),
        optimizer=optimizers.Fixed(),
    )

    history = sess.fit(ds,
                       200000,
                       val_data=val_ds,
                       val_epochs=1000,
                       listeners=[
                           FitListener('val_end',
                                       callback=lambda h: on_val_end(sess, h))
                       ])

    fit_report(history, report_path + '02.png', 15)
Esempio n. 2
0
    def setUpClass(cls):
        model = Model(
            [Simplelayer(4, inshape=3),
             Simplelayer(5),
             Simplelayer(1)])
        model.assemble()

        cls.sess = Session(model,
                           loss=losses.Mse(),
                           optimizer=optimizers.Fixed())
Esempio n. 3
0
def fit(name, optimizer):
    inshape = ds_train.data.shape[1:]
    #pdb.set_trace()
    model = Model([
        cnn.Conv2D(32, (3, 3), inshape=inshape),
        cnn.MaxPool2D((2, 2), strides=(2, 2)),
        cnn.Conv2D(64, (3, 3)),
        cnn.MaxPool2D((2, 2), strides=(2, 2)),
        cnn.Conv2D(64, (3, 3)),
        nn.Flatten(),
        nn.Dense(64),
        nn.Dropout(0.5),
        nn.Dense(10)
    ])
    model.assemble()

    sess = Session(model,
                   loss=losses.CategoricalCrossentropy(),
                   optimizer=optimizer)

    stop_fit = session.condition_callback(lambda: sess.stop_fit(), 'val_loss',
                                          30)

    accuracy = lambda h: fit_tools.accuracy(sess, ds_test, h)

    def save_and_report(history):
        #pdb.set_trace()
        fit_tools.fit_report(history, report_path + name + ".png")
        model.save(model_path + name)

    #pdb.set_trace()
    history = sess.fit(ds_train,
                       200,
                       val_data=ds_test,
                       val_steps=100,
                       listeners=[
                           stop_fit,
                           session.FitListener('val_end', callback=accuracy),
                           session.FitListener('val_end',
                                               callback=save_and_report)
                       ])

    save_and_report(history)
Esempio n. 4
0
def fit(name, model):
    sess = Session(model,
                loss = losses.SparseCategoricalCrossentropy(),
                optimizer = optimizers.Adam()
            )

    stop_fit = session.condition_callback(lambda :sess.stop_fit(), 'loss', 100)

    def save_and_report(history):
        #pdb.set_trace()
        fit_tools.fit_report(history, report_path+name+".png")
        model.save(model_path+name)

    #pdb.set_trace()
    history = sess.fit(ds_train, 1000,
                        listeners=[
                            stop_fit,
                            session.FitListener('epoch_end', callback=save_and_report)
                        ]
                    )

    save_and_report(history)
Esempio n. 5
0
def fit(name, model):
    sess = Session(model,
                loss = losses.BinaryCrossentropy(),
                optimizer = optimizers.Adam()
            )

    stop_fit = session.condition_callback(lambda :sess.stop_fit(), 'val_loss', 10)

    def save_and_report(history):
        #pdb.set_trace()
        fit_tools.fit_report(history, report_path+name+".png")
        model.save(model_path+name)

    #pdb.set_trace()
    history = sess.fit(ds_train, 100, val_data=ds_test, val_batches=20,
                        listeners=[
                            stop_fit,
                            session.FitListener('val_end', callback=fit_tools.binary_accuracy),
                            session.FitListener('epoch_end', callback=save_and_report)
                        ]
                    )

    save_and_report(history)
Esempio n. 6
0
def fit():
    inshape = ds_train.data.shape[1]
    model = Model([nn.Dense(10, inshape=inshape, activation='relu')])
    model.assemble()

    sess = Session(model,
                   loss=losses.CategoricalCrossentropy(),
                   optimizer=optimizers.Fixed(0.001))

    stop_fit = session.condition_callback(lambda: sess.stop_fit(), 'val_loss',
                                          10)

    #pdb.set_trace()
    history = sess.fit(ds_train,
                       20000,
                       val_epochs=5,
                       val_data=ds_test,
                       listeners=[
                           stop_fit,
                           session.FitListener('val_end', callback=accuracy)
                       ])

    fit_report(history, report_path + "0.png")
Esempio n. 7
0
    def test_save_load(self):
        fpath = "models/test_m1"
        self.sess.save(fpath)

        sess = Session.load(fpath)
def train(epochs, ds, model=None, batch_size=64, record_epochs=1):
    #加载/构建session
    sess = None
    if model is None:
        sess = Session.load(model_path)
    else:
        sess = Session(model, loss=losses.Mse(), optimizer=optimizers.Fixed())

    train_x = ds['train_x']
    train_y = ds['train_y']
    test_x = ds['test_x']
    test_y = ds['test_y']

    batchs = int(train_x.shape[0] / batch_size)
    print("epochs:%d, batchs=%d" % (epochs, batchs))

    #记录训练历史
    history = {
        'loss': [],
        'val_loss': [],
        'epochs': [],
        'val_x': test_x,
        'val_y': test_y,
        'val_pred': None
    }

    print("start training ")
    t_start = time.time()
    steps = epochs * batchs

    epoch = 1
    #循环训练
    for step in range(steps):
        start = (step % batchs) * batch_size
        end = start + batch_size
        batch_x = train_x[start:end]
        batch_y = train_y[start:end]

        loss = sess.batch_train(batch_x, batch_y)

        cur_epoch = int(step / batchs) + 1

        #每轮打印一次
        if step > 0 and step % batchs == 0:
            print((('epoch:%05d/%d loss=%f' % (cur_epoch, epochs, loss)) +
                   ' ' * 50)[:50],
                  end='\r')

        #记录
        if step % batchs == 0 and (cur_epoch - epoch == record_epochs
                                   or cur_epoch == epochs):
            epoch = cur_epoch

            y_pred = sess.model.predict(test_x)
            val_loss = sess.loss(test_y, y_pred)

            history['loss'].append(loss)
            history['val_loss'].append(val_loss)
            history['epochs'].append(epoch)
            history['val_pred'] = y_pred

            print((('epoch:%05d/%d loss=%f, val_loss=%f' %
                    (cur_epoch, epochs, loss, val_loss)) + ' ' * 50)[:50],
                  end='\r')
            print("")

    sess.save(model_path)
    print("training finished cost:%f" % (time.time() - t_start))

    return history