Skip to content
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

Keras-like API Advanced Activations, dropout and noise layers #2222

Merged
merged 36 commits into from
Feb 8, 2018

Conversation

Quincy2014
Copy link

@Quincy2014 Quincy2014 commented Jan 24, 2018

Add Keras-like API for some layers.

layers:
ELU,
LeakyReLU,
ThresholdedReLU,
SReLU,
Masking,
GaussianDropout,
GaussianNoise,
SpatialDropout1D,
SpatialDropout2D,
SpatialDropout3D.

test:
Each layer has a corresponding unit test in xxxSpec.scala.

import scala.reflect.ClassTag


@SerialVersionUID( - 6274543584907751212L)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you re-generated the UID?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.

val seq = KSequential[Float]()
val elu = ELU[Float](1.0, inputShape = Shape(3))
seq.add(elu)
def weightConverter(in: Array[Tensor[Float]]): Array[Tensor[Float]] = Array(in(0).t(), in(1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does ELU need a weightconverter? I don't think ELU has weights.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix it.

kerasCode, weightConverter)

}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line at the end of a file

""".stripMargin
val seq = KSequential[Float]()
val elu = ELU[Float](2.7, inputShape = Shape(3, 24, 24))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the blank line.

@Quincy2014
Copy link
Author

@hkvision hkvision changed the title [WIP] Keras API for ELU [WIP] More layers for Keras-like API Jan 24, 2018


@SerialVersionUID( - 1470253389268877486L)
class LeakyReLU[T: ClassTag](private val alpha: Double = 0.01,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this alpha is private? cc @zhichao-li

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls confirm this with the original author. we can open it if no objections.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original nn/LeakyReLU, the negval is private so I set the alpha as private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @psyyz10 @qiuxin2012 Any comments on this?

}
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove useless empty lines

}
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove useless empty lines


package com.intel.analytics.bigdl.nn.keras

import com.intel.analytics.bigdl._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. This is unnecessary.


import scala.reflect.ClassTag

@SerialVersionUID( 5198738230229027831L)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for serialUID for layers cc @yiheng #2224

* Created by intel on 2018/1/25.
*/
class GaussianDropoutSpec {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test?

@@ -0,0 +1,8 @@
package com.intel.analytics.bigdl.keras.nn

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??


package com.intel.analytics.bigdl.nn.keras

import com.intel.analytics.bigdl._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary.


package com.intel.analytics.bigdl.nn.keras

import com.intel.analytics.bigdl._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary


import scala.reflect.ClassTag

@SerialVersionUID( - 2224693793797534699L)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

"GaussianDropout forward and backward" should "work properly" in {
val seq = KSequential[Float]()
val input = Tensor[Float](Array(2, 28, 28, 1)).rand()
val gaussiandropout = GaussianDropout[Float](0.6, inputShape = Shape(3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input tensor is of shape (2, 28, 28, 1) but in the layer you specify inputShape=Shape(3)??

"GaussianNoise forward and backward" should "work properly" in {
val seq = KSequential[Float]()
val input = Tensor[Float](Array(2, 28, 28, 1)).rand()
val gaussiannoise = GaussianNoise[Float](0.6, inputShape = Shape(3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem as gaussiandropout.

@zhichao-li
Copy link
Contributor

Please enrich the description for the layers you added.

import scala.reflect.ClassTag


class LeakyReLU[T: ClassTag](private val alpha: Double = 0.01,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems can delete private cc @psyyz10

@@ -15,8 +15,10 @@
*/
package com.intel.analytics.bigdl.nn

import com.intel.analytics.bigdl.nn.abstractnn.TensorModule

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty line.

import com.intel.analytics.bigdl.tensor._

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty line.

@@ -32,12 +34,15 @@ import scala.reflect.ClassTag
* using extra state memory
*/


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

import LeakyReLU._
private val negVal = ev.fromType[Double](negval)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you add this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone else add this. There's a conflict so I fix the conflict.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you have a look in the latest code of this class? I don't think this line is still there.

@@ -34,10 +34,9 @@ import scala.reflect.ClassTag
* @param ip inplace mode
*/

@SerialVersionUID(3953292249027271493L)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the UID in old layers unchanged. Others may propose a PR to remove all of them.
For new layers, we simply don't add UID.

}
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty lines.

extends KerasLayer[Tensor[T], Tensor[T], T](KerasLayer.addBatch(inputShape)) {

override def doBuild(inputShape: Shape): AbstractModule[Tensor[T], Tensor[T], T] = {
if (round(alpha_init * beta_init) == 1.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only support alpha_init * beta_init == 1.0, so in this case, we just need one parameter, not two.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In converter.py, there are also alpha and beta two parameters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In converter, we have to convert Keras layer into BigDL layer, so we should follow the parameters in Keras. But in new API, if we don't support every parameter in Keras, we need to make modifications instead of throwing exceptions.

layer.asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]
}
else {
throw new Exception("Only alpha_init = 1/beta_init is supported for now")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't throw exception here. If the arguments we support is less than Keras, we can just eliminate the unsupported arguments.

import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric
import com.intel.analytics.bigdl.utils.Shape

import scala.reflect.ClassTag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file for now. Remove its tests. I will fix and add it afterwards.


import scala.reflect.ClassTag

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this layer.

* `f(x) = x for t^r > x > t^l`,
* `f(x) = t^l + a^l(x - t^l) for x <= t^l`.
*
* @param SharedAxes the axes along which to share learnable parameters for the activation function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize & Align

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Array of int.

* with output shape `(batch, height, width, channels)`,
* and you wish to share parameters across space
* so that each filter only has one set of parameters,
* set `shared_axes=[1, 2]`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array(1, 2)

val shape = inputShape.toSingle().toArray
if (SharedAxes == null) {
val layer = com.intel.analytics.bigdl.nn.SReLU(shape.slice(1, shape.length))
layer.asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These if else seems can be integrated?

* decrease. In this case, SpatialDropout1D will help promote independence
* between feature maps and should be used instead.
*
* @param p float between 0 and 1. Fraction of the input units to drop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double.

* between feature maps and should be used instead.
*
* @param p float between 0 and 1. Fraction of the input units to drop.
* @param format 'NCHW' or 'NHWC'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify this according to Convolution2D. Change to dimOrdering.

* between feature maps and should be used instead.
*
* @param p float between 0 and 1. Fraction of the input units to drop.
* @param format 'NCHW' or 'NHWC'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modify.

* decrease. In this case, SpatialDropout3D will help promote independence
* between feature maps and should be used instead.
*
* @param p float between 0 and 1. Fraction of the input units to drop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double.

* @param format 'NCHW' or 'NHWC'.
* In 'NCHW' mode, the channels dimension (the depth)
* is at index 1, in 'NHWC' mode is it at index 4.
* @tparam T The numeric type in the criterion, usually which are [[Float]] or [[Double]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

s"$format is not supported")

override def doBuild(inputShape: Shape): AbstractModule[Tensor[T], Tensor[T], T] = {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty lines.

* `f(x) = x for x > theta`,
* `f(x) = 0 otherwise`.
*
* @param theta float >= 0. Threshold location of activation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double.

@@ -0,0 +1,57 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file.

@@ -0,0 +1,57 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file.

"""
|input_tensor = Input(shape=[2, 3])
|input = np.random.uniform(-1, 1, [1, 2, 3])
|# input = np.array([[[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comments....

|input_tensor = Input(shape=[2, 3])
|input = np.random.uniform(-1, 1, [1, 2, 3])
|# input = np.array([[[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]])
|output_tensor = SReLU(a_left_init='one', t_right_init='one')(input_tensor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove a_left_init and t_right_init

kerasCode)
}

"SReLU 3D" should "be the same as Keras" in {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both ut are 3d input. This is OK. Change name of ut to SReLU with shared axes

*/
class SpatialDropout2D[T: ClassTag](
val p: Double = 0.5,
val format: DataFormat = DataFormat.NCHW,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dimOrdering

Copy link
Contributor

@hkvision hkvision left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hkvision
Copy link
Contributor

hkvision commented Feb 8, 2018

@hkvision hkvision merged commit 99c6410 into intel-analytics:master Feb 8, 2018
psyyz10 pushed a commit to psyyz10/BigDL that referenced this pull request Mar 8, 2018
…analytics#2222)

* Keras API for ELU

* fix style check error

* fix weightConverter

* add one more unit test for ELU

* remove blank line

* Keras API for LeakyReLU

* remove useless empty lines in LeakyReLU

* add GaussianDropout

* add GaussianNoise

* remove UID and unnecessary import

* fix two Gaussian unit test

* add layer Masking

* add layer SpatialDropout1D

* change 3D to 4D

* Revert "change 3D to 4D"

This reverts commit 9efdb0a.

* change unit test from 4D to 3D

* add layer SpatialDropout2D

* add layer PReLU. Unit test success without weight

* add 3D unit test for PReLU

* add layer ParametricSoftPlus. Unit test success without weight

* add layer SpatialDropout3D

* add layer ThresholdedReLU

* fix the above problems

* fix problems

* add format lowercase to support both uppercase and lowercase

* fix format problem

* SReLU

* add documentation and serializer

* remove a blank in documentation and change inputshape from var to val

* delete four files

* update

* modify

* modify problem

* modify

* update

* modify style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants