-
Notifications
You must be signed in to change notification settings - Fork 12
[WIP] Add part of the Keras API #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Avasil
wants to merge
2
commits into
shadaj:master
Choose a base branch
from
Avasil:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.2.8 | ||
sbt.version=1.3.3 |
21 changes: 21 additions & 0 deletions
21
src/main/scala/me/shadaj/scalapy/tensorflow/keras/Keras.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.tensorflow.keras.backend.Backend | ||
import me.shadaj.scalapy.tensorflow.keras.datasets.Datasets | ||
import me.shadaj.scalapy.tensorflow.keras.models.Models | ||
import me.shadaj.scalapy.tensorflow.keras.optimizers.Optimizers | ||
import me.shadaj.scalapy.tensorflow.keras.utils.Utils | ||
import me.shadaj.scalapy.tensorflow.keras.losses.Losses | ||
import me.shadaj.scalapy.tensorflow.keras.layers.Layers | ||
|
||
|
||
@py.native trait Keras extends py.Object { | ||
def models: Models = py.native | ||
def datasets: Datasets = py.native | ||
def backend: Backend = py.native | ||
def utils: Utils = py.native | ||
def optimizers: Optimizers = py.native | ||
def losses: Losses = py.native | ||
def layers: Layers = py.native | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/scala/me/shadaj/scalapy/tensorflow/keras/backend/Backend.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.backend | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Backend extends py.Object { | ||
def image_data_format(): String = py.native | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
src/main/scala/me/shadaj/scalapy/tensorflow/keras/datasets/Datasets.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.datasets | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Datasets extends py.Object { | ||
def mnist: Mnist = py.native | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/scala/me/shadaj/scalapy/tensorflow/keras/datasets/Mnist.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.datasets | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.numpy.NDArray | ||
|
||
@py.native trait Mnist extends py.Object { | ||
def load_data(path: String = "mnist.npz"): ((NDArray[Long], NDArray[Long]), (NDArray[Long], NDArray[Long])) = py.native | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Conv2D.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
package layers | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Conv2D extends Layer { | ||
def filters: Int = py.native | ||
def kernel_size: py.|[Int, (Int, Int)] = py.native | ||
def strides: py.|[Int, (Int, Int)] = py.native | ||
def padding: String = py.native | ||
def data_format: py.NoneOr[String] = py.native | ||
def dilation_rate: py.|[Int, (Int, Int)] = py.native | ||
def activation: py.NoneOr[String] = py.native | ||
def use_bias: Boolean = py.native | ||
def kernel_initializer: String = py.native | ||
def bias_initializer: String = py.native | ||
def kernel_regularizer: py.NoneOr[String] = py.native | ||
def bias_regularizer: py.NoneOr[String] = py.native | ||
def activity_regularizer: py.NoneOr[String] = py.native | ||
def kernel_constraint: py.NoneOr[String] = py.native | ||
def bias_constraint: py.NoneOr[String] = py.native | ||
def input_shape: py.NoneOr[(Int, Int, Int)] = py.native | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Dense.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
package layers | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Dense extends Layer { | ||
def units: Int = py.native | ||
def activation: py.NoneOr[String] = py.native | ||
def use_bias: Boolean = py.native | ||
def kernel_initializer: String = py.native | ||
def bias_initializer: String = py.native | ||
def kernel_regularizer: py.NoneOr[String] = py.native | ||
def bias_regularizer: py.NoneOr[String] = py.native | ||
def activity_regularizer: py.NoneOr[String] = py.native | ||
def kernel_constraint: py.NoneOr[String] = py.native | ||
def bias_constraint: py.NoneOr[String] = py.native | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Dropout.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
package layers | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.tensorflow.Tensor | ||
|
||
@py.native trait Dropout extends Layer { | ||
def rate: Double = py.native | ||
def noise_shape: py.NoneOr[Tensor] = py.native | ||
def seed: py.NoneOr[Int] = py.native | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Flatten.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.layers | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Flatten extends Layer { | ||
def data_format: py.NoneOr[String] = py.native | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Layer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.layers | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Layer extends py.Object |
49 changes: 49 additions & 0 deletions
49
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/Layers.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.layers | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.tensorflow.Tensor | ||
|
||
@py.native trait Layers extends py.Object { | ||
def Conv2D(filters: Int, | ||
kernel_size: py.|[Int, (Int, Int)], | ||
strides: py.|[Int, (Int, Int)] = (1, 1), | ||
padding: String = "valid", | ||
data_format: py.NoneOr[String] = py.None, | ||
dilation_rate: py.|[Int, (Int, Int)] = (1, 1), | ||
activation: py.NoneOr[String] = py.None, | ||
use_bias: Boolean = true, | ||
kernel_initializer: String = "glorot_uniform", | ||
bias_initializer: String = "zeros", | ||
kernel_regularizer: py.NoneOr[String] = py.None, | ||
bias_regularizer: py.NoneOr[String] = py.None, | ||
activity_regularizer: py.NoneOr[String] = py.None, | ||
kernel_constraint: py.NoneOr[String] = py.None, | ||
bias_constraint: py.NoneOr[String] = py.None, | ||
input_shape: py.NoneOr[(Int, Int, Int)] = py.None | ||
): Conv2D = py.nativeNamed | ||
|
||
def Dropout(rate: Double, | ||
noise_shape: py.NoneOr[Tensor] = py.None, | ||
seed: py.NoneOr[Int] = py.None | ||
): Dropout = py.nativeNamed | ||
|
||
def MaxPooling2D(pool_size: (Int, Int), | ||
strides: py.NoneOr[py.|[Int, (Int, Int)]] = py.None, | ||
padding: String = "valid", | ||
data_format: py.NoneOr[String] = py.None | ||
): MaxPooling2D = py.nativeNamed | ||
|
||
def Flatten(data_format: py.NoneOr[String] = py.None): Flatten = py.nativeNamed | ||
|
||
def Dense(units: Int, | ||
activation: py.NoneOr[String] = py.None, | ||
use_bias: Boolean = true, | ||
kernel_initializer: String = "glorot_uniform", | ||
bias_initializer: String = "zeros", | ||
kernel_regularizer: py.NoneOr[String] = py.None, | ||
bias_regularizer: py.NoneOr[String] = py.None, | ||
activity_regularizer: py.NoneOr[String] = py.None, | ||
kernel_constraint: py.NoneOr[String] = py.None, | ||
bias_constraint: py.NoneOr[String] = py.None | ||
): Dense = py.nativeNamed | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/MaxPooling2D.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.layers | ||
|
||
import me.shadaj.scalapy.tensorflow.Tensor | ||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait MaxPooling2D extends Layer { | ||
def pool_size: (Int, Int) = py.native | ||
def strides: py.NoneOr[py.|[Int, (Int, Int)]] = py.native | ||
def padding: String = py.native | ||
def data_format: py.NoneOr[String] = py.native | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/scala/me/shadaj/scalapy/tensorflow/keras/layers/package.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
|
||
import scala.util.control.NonFatal | ||
import me.shadaj.scalapy.py.{PyValue, Reader} | ||
import me.shadaj.scalapy.py | ||
|
||
package object layers { | ||
implicit val tupleReader: Reader[py.|[Int, (Int, Int)]] = new Reader[py.|[Int, (Int, Int)]] { | ||
override def read(v: PyValue): py.|[Int, (Int, Int)] = { | ||
try { | ||
v.getLong.toInt | ||
} catch { | ||
case NonFatal(_) => | ||
val tuple = v.getTuple | ||
(tuple(0).getLong.toInt, tuple(1).getLong.toInt) | ||
} | ||
} | ||
} | ||
|
||
implicit def noneReader[T](implicit reader: Reader[T]): Reader[py.NoneOr[T]] = new Reader[py.NoneOr[T]] { | ||
override def read(v: PyValue): py.NoneOr[T] = { | ||
try { | ||
reader.read(v) | ||
} catch { | ||
// TODO: read None | ||
case NonFatal(_) => py.None | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/scala/me/shadaj/scalapy/tensorflow/keras/losses/Losses.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.losses | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.py.PyFunction | ||
|
||
@py.native trait Losses extends py.Object { | ||
def categorical_crossentropy: PyFunction = py.native | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/scala/me/shadaj/scalapy/tensorflow/keras/models/Models.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.models | ||
|
||
import me.shadaj.scalapy.py | ||
|
||
@py.native trait Models extends py.Object { | ||
def Sequential(): Sequential = py.native | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/scala/me/shadaj/scalapy/tensorflow/keras/models/Sequential.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package me.shadaj.scalapy.tensorflow.keras | ||
package models | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.tensorflow.keras.layers.Layer | ||
import me.shadaj.scalapy.numpy.NDArray | ||
import me.shadaj.scalapy.py.PyFunction | ||
import me.shadaj.scalapy.tensorflow.keras.optimizers.Optimizer | ||
|
||
@py.native trait Sequential extends py.Object { | ||
private val origDynamic = this.as[py.Dynamic] | ||
|
||
def add(layer: Layer): Unit = origDynamic.add(layer = layer).as[Unit] | ||
|
||
def compile(optimizer: py.|[String, Optimizer] = "rmsprop", | ||
loss: py.NoneOr[PyFunction] = py.None, | ||
metrics: Seq[String] = Seq.empty, | ||
loss_weights: Seq[(Double, Double)] = Seq.empty, | ||
sample_weight_mode: py.NoneOr[String] = py.None, | ||
weighted_metrics: Seq[String] = Seq.empty, | ||
target_tensors: py.NoneOr[String] = py.None | ||
) = origDynamic.compile(optimizer = optimizer, loss = loss, metrics = metrics, loss_weights = loss_weights, sample_weight_mode = sample_weight_mode, weighted_metrics = weighted_metrics, target_tensors = target_tensors).as[Unit] | ||
|
||
def fit(x: NDArray[Long], | ||
y: NDArray[Long], | ||
batch_size: py.NoneOr[Int] = py.None, | ||
epochs: Int = 1, | ||
verbose: Int = 1, | ||
validation_data: py.NoneOr[(NDArray[Long], NDArray[Long])] = py.None | ||
): Unit = origDynamic.fit(x = x, y = y, batch_size = batch_size, epochs = epochs, verbose = verbose, validation_data = validation_data).as[Unit] | ||
|
||
def evaluate(x: NDArray[Long], | ||
y: NDArray[Long], | ||
batch_size: py.NoneOr[Int] = py.None, | ||
verbose: Int = 1 | ||
): Seq[Double] = origDynamic.evaluate(x = x, y = y, batch_size = batch_size, verbose = verbose).as[Seq[Double]] | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/scala/me/shadaj/scalapy/tensorflow/keras/optimizers/Optimizers.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.optimizers | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.py.{PyValue, Reader} | ||
|
||
@py.native trait Optimizers extends py.Object { | ||
def Adadelta(): Adadelta = py.native | ||
} | ||
|
||
@py.native trait Optimizer extends py.Object | ||
|
||
@py.native class Adadelta(val value: PyValue) extends Optimizer | ||
|
||
object Adadelta { | ||
implicit val reader: Reader[Adadelta] = new Reader[Adadelta] { | ||
override def read(v: PyValue): Adadelta = new Adadelta(v) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/scala/me/shadaj/scalapy/tensorflow/keras/utils/Utils.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.shadaj.scalapy.tensorflow.keras.utils | ||
|
||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.numpy.NDArray | ||
|
||
@py.native trait Utils extends py.Object { | ||
def to_categorical(y: NDArray[Long], num_classes: py.NoneOr[Long] = py.None, dtype: String = "float32"): NDArray[Long] = py.native | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input_shape
should be provided via**kwargs
(https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D) but I didn't know how to do it withscalapy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is something I need to work on supporting, probably with an annotation. In the meantime, you should be able to handle this by calling
this.as[py.Dynamic].Conv2D(..., input_shape=input_shape, ...)
since the dynamic API does support kwargs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried the following in dynamic API:
And it fails with:
It casts
32
toDouble
somewhere. I didn't have this issue in previous versions (with JEP) so perhaps there is a bug on master?