for gcn in self.gcns: x = gcn(x, edge_index) x = F.relu(x) x, _, _, batch, _, _ = self.graph_pooling(x, edge_index, None, batch) x = global_max_pool(x, batch) outputs = self.dense(x) outputs = F.relu(outputs) outputs = self.out(outputs) return outputs model = Model(dataset.item_num, 2) opt = optim.SGD(model.parameters(), lr=0.5) # 3. learner learner = Learner(model, opt, F.cross_entropy, train_db, metrics=[accuracy]) # 4. fit learner.fit(3) # 5. test learner.test(test_dl) # 6. predict pred = learner.predict(test_dl) print(pred.size()) # 7. plot learner.recorder.plot_metrics() plt.show()
in_dim = data.train_ds.x.shape[1] h_dim = 128 model = nn.Sequential(nn.Linear(in_dim, h_dim), nn.ReLU(), nn.Linear(h_dim, 10)) opt = optim.SGD(model.parameters(), lr=1.5) loss_func = F.cross_entropy learner = Learner(model, opt, loss_func, data, metrics=[accuracy], callbacks=EarlyStopping(patience=3)) learner.fit(10) learner.test(test_dl) pred = learner.predict(x_valid) print(pred.size()) learner.recorder.plot_loss() learner.recorder.plot_metrics() plt.show() # # 1. ------ 数据 # x_train, y_train, x_valid, y_valid = mnist_data() # train_ds, valid_ds = Dataset(x_train, y_train), Dataset(x_valid, y_valid) # bs = 128 # train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True) # valid_dl = DataLoader(valid_ds, batch_size=bs, shuffle=True)
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.gcn1 = GCN(1433, 16, F.relu) self.gcn2 = GCN(16, 7, None) def forward(self, g, features): x = self.gcn1(g, features) x = self.gcn2(g, x) return x net = Net() optimizer = th.optim.Adam(net.parameters(), lr=1e-3) # 3. learner learner = Learner(net, optimizer, masked_cross_entropy, data, metrics=masked_accuracy) # 4. fit learner.fit(50) # 5. test learner.test(test_dl) # 6. predict learner.predict(test_dl) # 7. plot learner.recorder.plot_metrics() plt.show()
nn.Conv2d(8, 16, 3, padding=1, stride=2), nn.ReLU(), # 7 nn.Conv2d(16, 32, 3, padding=1, stride=2), nn.ReLU(), # 4 nn.Conv2d(32, 32, 3, padding=1, stride=2), nn.ReLU(), # 2 nn.AdaptiveAvgPool2d(1), Lambda(dp.flatten), nn.Linear(32, out_dim)) x_train, y_train, x_valid, y_valid, x_test, y_test = mnist() x_train, x_valid, x_test = dp.normalize_to(x_train, x_valid, x_test) train_ds, valid_ds, test_ds = Dataset(x_train, y_train), Dataset( x_valid, y_valid), Dataset(x_test, y_test) bs = 512 train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True) valid_dl = DataLoader(valid_ds, batch_size=bs) test_dl = DataLoader(test_ds, batch_size=bs) data = DataBunch(train_dl, valid_dl) model = cnn_model(10) opt = optim.Adam(model.parameters(), lr=0.005) loss_func = F.cross_entropy learner = Learner(model, opt, loss_func, data, metrics=accuracy) learner.fit(5) plt.show()
model = Classifier(1, 256, train_ds.num_classes) optimizer = optim.Adam(model.parameters(), lr=0.001) # 3. learne loss_func = nn.CrossEntropyLoss() learner = Learner(model, optimizer, loss_func, data, metrics=accuracy, callbacks=DGLInterpreter) # 4. fit learner.fit(80) # 5. test learner.test(test_dl) loss = nn.CrossEntropyLoss(reduction='none') scores, xs, ys, preds, indecies = learner.interpreter.top_data(loss, k=10, phase='train', largest=True) plt.figure(figsize=[10, 5]) for i in range(10): plt.subplot(2, 5, i + 1) learner.interpreter.plot_graph(xs[i], node_size=50) plt.title(f'loss-{scores[i]:0.6f}')
train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True) valid_dl = DataLoader(valid_ds, batch_size=bs) test_dl = DataLoader(test_ds, batch_size=bs) data = DataBunch(train_dl, valid_dl) in_dim = data.train_ds.x.shape[1] h_dim = 128 model = nn.Sequential(nn.Linear(in_dim, h_dim), nn.ReLU(), nn.Linear(h_dim, 10)) opt = optim.SGD(model.parameters(), lr=0.35) loss_func = F.cross_entropy cbks = Checkpoints(3) # save checkpoint each 3 epochs learner = Learner(model, opt, loss_func, data, metrics=[accuracy], callbacks=cbks) learner.fit(3) learner.load_checkpoint() # load the latest checkpoint learner.fit(2) # go on training learner.test(test_dl) pred = learner.predict(x_valid) print(pred.size()) learner.recorder.plot_loss() plt.show()