Skip to main content

A keras like wrapper for pytorch

Project description

KeraTorch

Implementing Keras clone with pytorch backend.

Install

pip install keratorch

How to use

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
((50000, 784), (50000,), (10000, 784), (10000,))

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:

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)
<div>
    <style>
        /* Turns off some styling */
        progress {
            /* gets rid of default border in Firefox and Opera. */
            border: none;
            /* Needs to be in here for Safari polyfill so background images work as expected. */
            background-size: auto;
        }
        .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {
            background: #F44336;
        }
    </style>
  <progress value='25' class='' max='34' style='width:300px; height:20px; vertical-align: middle;'></progress>
  73.53% [25/34 00:00<00:00]
</div>
epoch train_loss valid_loss time
0 2.301613 #na# 00:00
1 2.301608 #na# 00:00
2 2.301600 #na# 00:00
3 2.301588 #na# 00:00
4 2.301569 #na# 00:00
5 2.301540 #na# 00:00
6 2.301493 #na# 00:00
7 2.301417 #na# 00:00
8 2.301294 #na# 00:00
9 2.301092 #na# 00:00
10 2.300761 #na# 00:00
11 2.300216 #na# 00:00
12 2.299308 #na# 00:00
13 2.297771 #na# 00:00
14 2.295127 #na# 00:00
15 2.290489 #na# 00:00
16 2.281993 #na# 00:00
17 2.265558 #na# 00:00
18 2.230882 #na# 00:00
19 2.157919 #na# 00:00
20 2.041476 #na# 00:00
21 1.920061 #na# 00:00
22 1.823919 #na# 00:00
23 1.768780 #na# 00:00
24 1.723490 #na# 00:00
25 1.641395 #na# 00:00
26 1.776127 #na# 00:00
27 2.319300 #na# 00:00
28 3.339199 #na# 00:00
29 4.307324 #na# 00:00
30 5.229871 #na# 00:00
31 6.128597 #na# 00:00

<div>
    <style>
        /* Turns off some styling */
        progress {
            /* gets rid of default border in Firefox and Opera. */
            border: none;
            /* Needs to be in here for Safari polyfill so background images work as expected. */
            background-size: auto;
        }
        .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {
            background: #F44336;
        }
    </style>
  <progress value='1' class='' max='3' style='width:300px; height:20px; vertical-align: middle;'></progress>
  33.33% [1/3 00:00<00:00 6.4181]
</div>



LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Min numerical gradient: 9.12E-03
Min loss divided by 10: 1.74E-02

png

We have the same .fit and .predict functions:

model.fit(x_train, y_train, bs, epochs=5, lr=1e-2)
epoch train_loss valid_loss time
0 2.294942 2.212783 00:01
1 2.163815 1.622664 00:01
2 1.856739 1.071931 00:01
3 1.548884 0.879702 00:01
4 1.325464 0.846312 00:01
preds = model.predict(x_valid)
accuracy = (preds.argmax(axis=-1) == y_valid).mean()
print(f'Predicted accuracy is {accuracy:.2f}')
Predicted accuracy is 0.77

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keraTorch-0.0.2.tar.gz (15.0 kB view hashes)

Uploaded Source

Built Distribution

keraTorch-0.0.2-py3-none-any.whl (14.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page