-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
import scala.reflect.ClassTag | ||
|
||
|
||
@SerialVersionUID( - 6274543584907751212L) |
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.
have you re-generated the UID?
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.
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)) |
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.
Why does ELU need a weightconverter? I don't think ELU has weights.
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.
fix it.
kerasCode, weightConverter) | ||
|
||
} | ||
} |
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.
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)) | ||
|
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.
Remove the blank line.
Jenkins passed. http://172.168.2.101:8080/view/PR-V/job/BigDL-PR-V/836 |
|
||
|
||
@SerialVersionUID( - 1470253389268877486L) | ||
class LeakyReLU[T: ClassTag](private val alpha: Double = 0.01, |
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.
Why this alpha is private? cc @zhichao-li
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.
Pls confirm this with the original author. we can open it if no objections.
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.
In the original nn/LeakyReLU, the negval is private so I set the alpha as private.
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.
cc @psyyz10 @qiuxin2012 Any comments on this?
} | ||
} | ||
|
||
|
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.
Remove useless empty lines
} | ||
} | ||
|
||
|
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.
Remove useless empty lines
|
||
package com.intel.analytics.bigdl.nn.keras | ||
|
||
import com.intel.analytics.bigdl._ |
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.
Remove this. This is unnecessary.
|
||
import scala.reflect.ClassTag | ||
|
||
@SerialVersionUID( 5198738230229027831L) |
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.
* Created by intel on 2018/1/25. | ||
*/ | ||
class GaussianDropoutSpec { | ||
|
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.
Unit test?
@@ -0,0 +1,8 @@ | |||
package com.intel.analytics.bigdl.keras.nn | |||
|
|||
/** |
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.
??
|
||
package com.intel.analytics.bigdl.nn.keras | ||
|
||
import com.intel.analytics.bigdl._ |
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.
unnecessary.
|
||
package com.intel.analytics.bigdl.nn.keras | ||
|
||
import com.intel.analytics.bigdl._ |
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.
unnecessary
|
||
import scala.reflect.ClassTag | ||
|
||
@SerialVersionUID( - 2224693793797534699L) |
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.
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)) |
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 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)) |
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.
Same problem as gaussiandropout.
Please enrich the description for the layers you added. |
import scala.reflect.ClassTag | ||
|
||
|
||
class LeakyReLU[T: ClassTag](private val alpha: Double = 0.01, |
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.
Seems can delete private
cc @psyyz10
@@ -15,8 +15,10 @@ | |||
*/ | |||
package com.intel.analytics.bigdl.nn | |||
|
|||
import com.intel.analytics.bigdl.nn.abstractnn.TensorModule | |||
|
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.
Remove the empty line.
import com.intel.analytics.bigdl.tensor._ | ||
|
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.
Remove the empty line.
@@ -32,12 +34,15 @@ import scala.reflect.ClassTag | |||
* using extra state memory | |||
*/ | |||
|
|||
|
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.
Remove the empty line.
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.
??
import LeakyReLU._ | ||
private val negVal = ev.fromType[Double](negval) |
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.
Why do you add this?
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.
Someone else add this. There's a conflict so I fix the conflict.
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.
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) |
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.
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.
} | ||
} | ||
|
||
|
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.
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) { |
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.
We only support alpha_init * beta_init == 1.0
, so in this case, we just need one parameter, not two.
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.
In converter.py, there are also alpha and beta two parameters.
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.
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") |
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.
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 |
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.
Remove this file for now. Remove its tests. I will fix and add it afterwards.
|
||
import scala.reflect.ClassTag | ||
|
||
/** |
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.
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. |
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.
Capitalize & Align
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.
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]`. |
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.
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]] |
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.
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. |
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.
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'. |
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.
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'. |
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.
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. |
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.
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]] |
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.
???
s"$format is not supported") | ||
|
||
override def doBuild(inputShape: Shape): AbstractModule[Tensor[T], Tensor[T], T] = { | ||
|
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.
remove empty lines.
* `f(x) = x for x > theta`, | ||
* `f(x) = 0 otherwise`. | ||
* | ||
* @param theta float >= 0. Threshold location of activation. |
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.
Double.
@@ -0,0 +1,57 @@ | |||
/* |
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.
remove this file.
@@ -0,0 +1,57 @@ | |||
/* |
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.
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]]]) |
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.
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) |
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.
remove a_left_init and t_right_init
kerasCode) | ||
} | ||
|
||
"SReLU 3D" should "be the same as Keras" in { |
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.
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, |
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.
dimOrdering
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.
LGTM
…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
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.