Keras Vs PyTorch – Key Differences

In this Python tutorial, we will learn about Keras Vs PyTorch in Python and we will also cover different examples related to Keras Vs PyTorch. And these are the following topics that we are going to discuss in this tutorial.

  • Introduction to Keras
  • Introduction to PyTorch
  • Keras Vs PyTorch
Keras vs PyTorch
Keras vs PyTorch

Introduction to Keras

In this section, we will learn about What is Keras and how its works in python.

  • Keras is a high-level neural network API designed for human beings written in python. It is an open-source library that is planned to provide fast experimentation.
  • Keras was also used to decrease the cognitive load and also merged into TensorFlow and users can access it as tf.Keras.
  • Keras act as an interface for the Tensorflow library.


In this example, we will import some Keras libraries for building the model using the mnist dataset.

  • input_shape = (28, 28, 1) is used as a data parameters.
  • (xtrain, ytrain), (xtest, ytest) = keras.datasets.mnist.load_data() is used to split the data into train and test dataset.
  • ytrain = keras.utils.to_categorical(ytrain, num_classes) is used to convert the class vector to binary class matrices.
  • model.summary() is used to define the summary of the model.
  • batchsize = 126 is used for giving the batch size.
  • model.compile(loss=”categorical_crossentropy”, optimizer=”adam”, metrics=[“accuracy”]) is used to compile the model.
  •, ytrain, batch_size=batchsize, epochs=epoch, validation_split=0.1) is used to fit the model.
  • scores = model.evaluate(xtest, ytest, verbose=0) is used to evaluate the model score.
  • print(“Test loss:”, scores[0]) is used to print the test loss score on the screen.
  • print(“Test accuracy:”, scores[1]) is used to print the test accuracy score on the screen.
import numpy as num
from tensorflow import keras
from tensorflow.keras import layers
# Model 
num_classes = 10
input_shape = (28, 28, 1)

(xtrain, ytrain), (xtest, ytest) = keras.datasets.mnist.load_data()

# Scale images to the [0, 1] range
xtrain = xtrain.astype("float32") / 255
xtest = xtest.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
xtrain = num.expand_dims(xtrain, -1)
xtest = num.expand_dims(xtest, -1)
print("xtrain shape:", xtrain.shape)
print(xtrain.shape[0], "Train samples")
print(xtest.shape[0], "Test samples")

ytrain = keras.utils.to_categorical(ytrain, num_classes)
ytest = keras.utils.to_categorical(ytest, num_classes)
model = keras.Sequential(
        layers.Conv2D(30, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(62, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Dense(num_classes, activation="softmax"),

batchsize = 126
epoch = 8

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]), ytrain, batch_size=batchsize, epochs=epoch, validation_split=0.1)
scores = model.evaluate(xtest, ytest, verbose=0)
print("Test loss:", scores[0])
print("Test accuracy:", scores[1])


After running the above code, we get the following output in which we can see that the model with test loss score and test accuracy scores are printed on the screen.

What is Keras
Keras Example

Also, check: PyTorch Save Model – Complete Guide

Introduction to PyTorch

In this section, we will learn about What is PyTorch and how we can work with PyTorch in python.

  • PyTorch is an open-source machine learning library. It is released under the modified BSD license.
  • PyTorch is used for computer versions and natural language processing applications and it was developed by the Facebook AI research lab.


In the following example, we will import the torch library to build a PyTorch model.

  • nn.Conv2d() is used to apply 2d convolution over the input.
  • nn.Linear() is used to make the feed-forward neural network.
  • nn.MaxPool2d() is used to apply over an input signal.
  • model = Net() is used to describe the net of the model.
  • print(model) is used to print the model output.
import torch
from torch import nn
import torch.nn.functional as fun
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv = nn.Conv2d(6, 35, 6)
        self.conv1 = nn.Conv2d(35, 19, 6)
        self.fc = nn.Linear(16 * 6 * 6, 10)
        self.pool = nn.MaxPool2d(2, 2)
    def forward(self, y):
        y = self.pool(fun.relu(self.conv(y)))
        y = self.pool(fun.relu(self.conv1(y)))
        y = y.view(-1, 16 * 6 * 6)
        y = fun.log_softmax(self.fc(y), dim=-1)
        return y
model = Net()


After running the above code, we get the following output in which we can see that the PyTorch model is printed on the screen.

What is PyTorch
What is PyTorch

Read: Cross Entropy Loss PyTorch

Keras Vs PyTorch

In this section, we will learn about the difference between Keras Vs PyTorch in python.

1. Keras was released in March 2015.1. PyTorch was released in October 2016.
2. Keras has a high level API2. PyTorch has a low-level API.
3. Keras has smaller datasets3. PyTorch has large datasets, high performance.
4. Keras provide static computation graphs.4. PyTorch provide dynamic computation graphs.
5. Keras does not often need debugging because of the simple networks.5. PyTorch has good debugging capabilities.
6. Keras has smaller community support.6. PyTorch has stronger community support.
7. Keras has a slow speed that’s why its performance is low.7. PyTorch has a high speed that’s why its performance is high.
8. Keras has backend implementation that includes Tensorflow.8. PyTorch has no backend implementation.
9. Keras is written in python.9. PyTorch is written in Lua.
10. In Keras debugging is difficult due to the existence of computational junks.10. In PyTorch debugging is easier and faster.
Keras vs PyTorch

You may like the following PyTorch tutorials

So, in this tutorial, we discussed Keras Vs PyTorch and we have also covered the examples related to this. Here is the list of examples that we have covered.

  • Introduction to Keras
  • Introduction to PyTorch
  • Keras Vs PyTorch