Implementing Keras clone with pytorch backend.
pip install keratorch
from keraTorch.model import Sequential
from keraTorch.layers import *
from keraTorch.losses import *
The data:
x_train.shape, y_train.shape, x_valid.shape, y_valid.shape
Model definition:
model = Sequential()
model.add(Dense(100, x_train.shape[1], activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
Doesn't actually compile anything but to look like keras we specify the loss as below. ce4softmax
means crossentropy for softmax loss.
model.compile(ce4softmax)
Burrow for Fastai's learning rate finder to find best learning rate:
bs = 256
model.lr_find(x_train, y_train, bs=bs)
We have the same .fit
and .predict
functions:
model.fit(x_train, y_train, bs, epochs=10, lr=1e-2)
preds = model.predict(x_valid)
accuracy = (preds.argmax(axis=-1) == y_valid).mean()
print(f'Predicted accuracy is {accuracy:.2f}')