Python Machine learning
keras
Apr 8, 2021     2 minutes read

1. What is keras and why would you still use pytorch?

2. Example usage

An absoolutely basic example

Yes, it’s going to be MNIST ;)

Based on Deep learning with Python, chapter 2.

You can use Sequential API or Model API (there is also Functional API, but I will not cover it as it is almost exactly the same Sequential API), they both work exactly the same, but as you can see, Sequential API's syntax is much shorter and Model API's syntax looks almost exactly the same as pytorch.

from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape((60000, 28 * 28)).astype('float32') / 255
X_test = X_test.reshape((10000, 28 * 28)).astype('float32') / 255

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)


# you can use Sequential API
import keras
network = keras.models.Sequential()
network.add(keras.layers.Dense(512, activation='relu'))
network.add(keras.layers.Dense(10, activation='softmax'))

# or Model API - up to you!
from tensorflow import nn, keras

class Network(keras.Model):

  def __init__(self):
    super(Network, self).__init__()
    self.d1 = keras.layers.Dense(512, activation=nn.relu)
    self.d2 = keras.layers.Dense(10, activation=nn.softmax)

  def call(self, inputs):
    x = self.d1(inputs)
    return self.d2(x)

network = Network()
####
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
network.fit(X_train, y_train, epochs=5, batch_size=128)

test_loss, test_acc = network.evaluate(X_test, y_test)
print(test_acc)
y_hat = network.predict(X_test)  # and this is how you make predictions

GPU support

The easiest way to run keras on GPU is to pull a tensorflow-gpu image with:

docker pull tensorflow/tensorflow:latest-gpu

start it up with:

docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu bash

and run your code in it. But first you should configure GPU on the machine you’re working at. Just follow this procedure (basically you should get to the point, when you can run nvidia-smi from your terminal).

3. Interesting resources

4. Subjects still to cover