def train(network, x, y, epochs=3): """Train network for given number of epochs""" for epoch in range(epochs): print('epoch', epoch + 1) losses = ( zip(x, y) >> nf.PrintProgress(x) >> vec2img >> augment >> nf.Shuffle(1000) >> build_batch >> network.train() >> nf.Collect()) print('train loss:', np.mean(losses))
def view_misclassified_images(network, x, y): """Show misclassified images""" make_label = nf.Map(lambda s: (s[0], 'true:%d pred:%d' % (s[1], s[2]))) filter_error = nf.Filter(lambda s: s[1] != s[2]) view_image = nm.ViewImageAnnotation(0, 1, pause=1) preds = (zip(x, y) >> vec2img >> build_pred_batch >> network.predict() >> nf.Map(np.argmax) >> nf.Collect()) (zip(x, y, preds) >> vec2img >> filter_error >> make_label >> view_image >> nf.Consume())
def train(network, epochs=3): """Train network for given number of epochs""" print('loading data...') filepath = download_mnist() x_train, y_train, x_test, y_test = load_mnist(filepath) plot = nm.PlotLines(None, every_sec=0.2) build_batch = (nm.BuildBatch(128, verbose=False).input( 0, 'vector', 'float32').output(1, 'number', 'int64')) for epoch in range(epochs): print('epoch', epoch + 1) losses = (zip(x_train, y_train) >> nf.PrintProgress(x_train) >> nf.Shuffle(1000) >> build_batch >> network.train() >> plot >> nf.Collect()) acc_test = evaluate(network, x_test, y_test) acc_train = evaluate(network, x_train, y_train) print('train loss : {:.6f}'.format(np.mean(losses))) print('train acc : {:.1f}'.format(acc_train)) print('test acc : {:.1f}'.format(acc_test))
def train(model, samples, epochs): """Train network for given number of epochs""" model.optimizer = optim.Adam(model.parameters(), amsgrad=False, lr=0.00057, weight_decay=1.55e-05) # plot = nm.PlotLines(None) for epoch in range(epochs): losses = (samples >> AdjChannels() >> BuildBatch(BATCHSIZE) >> nf.Shuffle(100) >> Train(model) >> nf.Collect()) loss = np.mean(losses) print(epoch, 'train loss:', loss) # plot(loss) model.save_best(loss, True, 'best_weights.pt') if loss > 1.1 * model.best_score: # reset weights model.load_weights('best_weights.pt') if loss < 0.001: break return model.best_score
def predict(network, x, y): """Compute network outputs and print accuracy""" preds = (zip(x, y) >> nf.PrintProgress(x) >> vec2img >> build_pred_batch >> network.predict() >> nf.Collect()) acc = accuracy(y, preds) print('test acc', acc)
def validate(network, x, y): """Compute validation/test loss (= mean over batch losses)""" losses = (zip(x, y) >> nf.PrintProgress(x) >> vec2img >> build_batch >> network.validate() >> nf.Collect()) print('val loss:', np.mean(losses))