首页 > 解决方案 > 为什么 PyTorch 对我来说运行比 Keras 慢,以及如何使代码简洁?

问题描述

我读到 PyTorch 更好更快,所以我想看看我是否想从 Keras 过渡到 PyTorch。

我在下面编写了简单的测试代码,但 Keras 对我来说始终运行得更快,并且通常(并非总是)获得比 PyTorch 更好的分数。

还有比 Keras 更简洁的 PyTorch 代码还有更好的方法吗?

#output:
Keras:
Total runtime =  18.451340198516846
LRL: 0.145 LRAP: 0.493
PyTorch:
Total runtime =  19.641956329345703
LRL: 0.092 LRAP: 0.491


def score(true, pred):
    lrl = label_ranking_loss(true, pred)
    lrap = label_ranking_average_precision_score(true, pred)
    print('LRL:', round(lrl), 'LRAP:', round(lrap))

def main():
    x,y = load()
    x_train, x_test, y_train, y_test = train_test_split(x, y)
    scaler = StandardScaler()
    x_train= scaler.fit_transform(x_train)
    x_test= scaler.transform(x_test)
    epochs = 100
    batch_size = 32

    print("Keras:")
    t_start = time.time()
    model= Sequential()
    model.add(Dense(60, activation="relu", input_shape=(120,)))
    model.add(Dense(30, activation="relu"))
    model.add(Dense(10, activation="sigmoid"))
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs)
    pred = model.predict(x_test)
    t_finish = time.time()
    total_time = t_finish-t_start
    print('Total runtime = ', total_time)
    score(y_test, pred)

    print("PyTorch:")
    t_start = time.time()
    model = torch.nn.Sequential(
        torch.nn.Linear(120, 60),
        torch.nn.ReLU(),
        torch.nn.Linear(60, 30),
        torch.nn.ReLU(),
        torch.nn.Linear(30, 10),
        torch.nn. Sigmoid())
    loss_fn = torch.nn. BCELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    n_batch = int(x_train.shape[0]/batch_size)
    for epoch in range(epochs):
        avg_cost = 0
        for i in range(n_batch):
            x_batch = x_train[i*batch_size:(i+1)*batch_size]
            y_batch = y_train[i*batch_size:(i+1)*batch_size]
            x, y = Variable(torch.from_numpy(x_batch).float()), Variable(torch.from_numpy(y_batch).float(), requires_grad=False)
            pred = model(x)
            loss = loss_fn(pred, y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            avg_cost += loss.item()/n_batch
        print(epoch, avg_cost)
        x, y = Variable(torch.from_numpy(x_test).float()), Variable(torch.from_numpy(y_test).float(), requires_grad=False)
    pred = model(x).data.numpy()
    t_finish = time.time()
    total_time = t_finish-t_start
    print('Total runtime = ', total_time)
    score(y_test, pred)

if __name__ == '__main__':
    main()

标签: keraspytorch

解决方案


推荐阅读