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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
24bc07b
Keras API for ELU
Quincy2014 Jan 24, 2018
cb68227
fix style check error
Quincy2014 Jan 24, 2018
8866a38
fix weightConverter
Quincy2014 Jan 24, 2018
0c92054
add one more unit test for ELU
Quincy2014 Jan 24, 2018
ac28817
remove blank line
Quincy2014 Jan 24, 2018
ecf21de
Keras API for LeakyReLU
Quincy2014 Jan 24, 2018
03e1ed5
remove useless empty lines in LeakyReLU
Quincy2014 Jan 25, 2018
866735d
add GaussianDropout
Quincy2014 Jan 25, 2018
c64eff5
add GaussianNoise
Quincy2014 Jan 25, 2018
50296a2
remove UID and unnecessary import
Quincy2014 Jan 25, 2018
3054fec
fix two Gaussian unit test
Quincy2014 Jan 25, 2018
c90ab99
add layer Masking
Quincy2014 Jan 26, 2018
d263320
add layer SpatialDropout1D
Quincy2014 Jan 26, 2018
9538c76
change 3D to 4D
Quincy2014 Jan 26, 2018
3ebaa40
Revert "change 3D to 4D"
Quincy2014 Jan 26, 2018
8e83192
change unit test from 4D to 3D
Quincy2014 Jan 26, 2018
ed6f307
add layer SpatialDropout2D
Quincy2014 Jan 26, 2018
218cd41
add layer PReLU. Unit test success without weight
Quincy2014 Jan 26, 2018
07fce1d
add 3D unit test for PReLU
Quincy2014 Jan 26, 2018
daac88b
add layer ParametricSoftPlus. Unit test success without weight
Quincy2014 Jan 26, 2018
437f478
add layer SpatialDropout3D
Quincy2014 Jan 26, 2018
8d2ecb0
add layer ThresholdedReLU
Quincy2014 Jan 26, 2018
081649f
fix the above problems
Quincy2014 Jan 29, 2018
411465f
fix problems
Quincy2014 Jan 29, 2018
b133247
add format lowercase to support both uppercase and lowercase
Quincy2014 Jan 30, 2018
f9f3b81
fix format problem
Quincy2014 Jan 30, 2018
082a310
SReLU
Quincy2014 Feb 2, 2018
6ce745c
add documentation and serializer
Quincy2014 Feb 6, 2018
8bfc875
remove a blank in documentation and change inputshape from var to val
Quincy2014 Feb 7, 2018
4392d45
delete four files
Quincy2014 Feb 8, 2018
5a75157
update
Quincy2014 Feb 8, 2018
32ff46e
modify
Quincy2014 Feb 8, 2018
9c596f2
modify problem
Quincy2014 Feb 8, 2018
f8beee3
modify
Quincy2014 Feb 8, 2018
6fabd80
update
Quincy2014 Feb 8, 2018
1ada8b3
modify style
Quincy2014 Feb 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
SReLU
  • Loading branch information
Quincy2014 committed Feb 8, 2018
commit 082a31099ed2c472c6da59804a4a825a4d2fe52f
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.intel.analytics.bigdl.nn

import com.intel.analytics.bigdl.nn.abstractnn.{AbstractModule, Activity, Initializable, TensorModule}
import com.intel.analytics.bigdl.nn.abstractnn._
import com.intel.analytics.bigdl.tensor.Tensor
import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric
import com.intel.analytics.bigdl.utils.serializer._
Expand Down Expand Up @@ -48,7 +48,7 @@ import scala.reflect.ClassTag

@SerialVersionUID(7173457290010080259L)
class SReLU[T: ClassTag](val shape: Array[Int], val sharedAxes: Array[Int] = null)(
implicit ev: TensorNumeric[T]) extends TensorModule[T] with Initializable {
implicit ev: TensorNumeric[T]) extends TensorModule[T] with Initializable with IdentityOutputShape {
import SReLU._
val weightsLen = 4
val weights: Array[Tensor[T]] = Array.fill[Tensor[T]](4)(Tensor[T]())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2016 The BigDL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.intel.analytics.bigdl.nn.abstractnn._
import com.intel.analytics.bigdl.tensor.Tensor
import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric
import com.intel.analytics.bigdl.utils.Shape

import scala.reflect.ClassTag

class SReLU[T: ClassTag](SharedAxes: Array[Int] = null,
var inputShape: Shape = null
)(implicit ev: TensorNumeric[T])
extends KerasLayer[Tensor[T], Tensor[T], T](KerasLayer.addBatch(inputShape)) {

override def doBuild(inputShape: Shape): AbstractModule[Tensor[T], Tensor[T], T] = {
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?

} else
{
val layer = com.intel.analytics.bigdl.nn.SReLU(shape.slice(1, shape.length), SharedAxes)
layer.asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]
}
}
}


object SReLU {

def apply[@specialized(Float, Double) T: ClassTag](
SharedAxes: Array[Int] = null,
inputShape: Shape = null
)(implicit ev: TensorNumeric[T]) : SReLU[T] = {
new SReLU[T](
SharedAxes,
inputShape)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 The BigDL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.intel.analytics.bigdl.keras.KerasBaseSpec
import com.intel.analytics.bigdl.nn.keras.{Sequential => KSequential}
import com.intel.analytics.bigdl.nn.abstractnn.AbstractModule
import com.intel.analytics.bigdl.nn.keras.SReLU
import com.intel.analytics.bigdl.tensor.Tensor
import com.intel.analytics.bigdl.utils.Shape

class SReLUSpec extends KerasBaseSpec{

"SReLU" should "be the same as Keras" in {
val kerasCode =
"""
|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....

|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

|# output_tensor = SReLU()(input_tensor)
|model = Model(input=input_tensor, output=output_tensor)
""".stripMargin
val seq = KSequential[Float]()
val srelu = SReLU[Float](null, inputShape = Shape(2, 3))
seq.add(srelu)
checkOutputAndGrad(seq.asInstanceOf[AbstractModule[Tensor[Float], Tensor[Float], Float]],
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

val kerasCode =
"""
|input_tensor = Input(shape=[3, 24])
|input = np.random.random([2, 3, 24])
|output_tensor = SReLU(shared_axes=[1, 2])(input_tensor)
|model = Model(input=input_tensor, output=output_tensor)
""".stripMargin
val seq = KSequential[Float]()
val srelu = SReLU[Float](Array(1, 2), inputShape = Shape(3, 24))
seq.add(srelu)
checkOutputAndGrad(seq.asInstanceOf[AbstractModule[Tensor[Float], Tensor[Float], Float]],
kerasCode)
}
}