I read PyTorch is better and faster, so I'm trying to see if I want to transition to PyTorch from Keras.
I made the simple testing code below, but Keras consistently runs faster for me, and usually (not always) gets better score than PyTorch.
Also is there a better way to write code for PyTorch more concise like Keras?
#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()