Beispiel #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)
Beispiel #2
0
    def test_mse(self):
        print("test Mse")
        mse = losses.Mse()

        y_true = np.array([[3], [5], [7]])
        y_pred = np.array([[1], [2], [3]])

        loss = mse(y_true, y_pred)
        print("loss: ", loss)
Beispiel #3
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())
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