Skip to content

Commit

Permalink
Convlstm3d (intel#1361)
Browse files Browse the repository at this point in the history
* support convlstm3d
  • Loading branch information
dding3 authored Jul 26, 2017
1 parent 7ca4923 commit d42feec
Show file tree
Hide file tree
Showing 10 changed files with 874 additions and 57 deletions.
500 changes: 494 additions & 6 deletions docs/docs/APIdocs/Layers/Recurrent-Layers.md

Large diffs are not rendered by default.

38 changes: 32 additions & 6 deletions pyspark/bigdl/nn/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3617,7 +3617,7 @@ class Pack(Layer):
def __init__(self, dimension, bigdl_type="float"):
super(Pack, self).__init__(None, bigdl_type, dimension)

class ConvLSTMPeephole(Layer):
class ConvLSTMPeephole2D(Layer):
'''
| Convolution Long Short Term Memory architecture with peephole.
Expand All @@ -3632,19 +3632,45 @@ class ConvLSTMPeephole(Layer):
:param wRegularizer: instance of [[Regularizer]](eg. L1 or L2 regularization), applied to the input weights matrices
:param uRegularizer: instance [[Regularizer]](eg. L1 or L2 regularization), applied to the recurrent weights matrices
:param bRegularizer: instance of [[Regularizer]]applied to the bias.
:param with_peephold: whether use last cell status control a gate.
:param with_peephole: whether use last cell status control a gate.
>>> convlstm = ConvLSTMPeephole(4, 3, 3, 3, 1, L1Regularizer(0.5), L1Regularizer(0.5), L1Regularizer(0.5))
>>> convlstm = ConvLSTMPeephole2D(4, 3, 3, 3, 1, L1Regularizer(0.5), L1Regularizer(0.5), L1Regularizer(0.5))
creating: createL1Regularizer
creating: createL1Regularizer
creating: createL1Regularizer
creating: createConvLSTMPeephole
creating: createConvLSTMPeephole2D
'''

def __init__(self, input_size, output_size, kernel_i, kernel_c, stride, wRegularizer=None, uRegularizer=None,
bRegularizer=None, with_peephole=True, bigdl_type="float"):
super(ConvLSTMPeephole, self).__init__(None, bigdl_type, input_size, output_size, kernel_i, kernel_c, stride,
wRegularizer, uRegularizer, bRegularizer, with_peephole)
super(ConvLSTMPeephole2D, self).__init__(None, bigdl_type, input_size, output_size, kernel_i, kernel_c, stride,
wRegularizer, uRegularizer, bRegularizer, with_peephole)


class ConvLSTMPeephole3D(Layer):
'''
:param input_size: number of input planes in the image given into forward()
:param output_size: number of output planes the convolution layer will produce
:param kernel_i Convolutional filter size to convolve input
:param kernel_c Convolutional filter size to convolve cell
:param stride The step of the convolution
:param wRegularizer: instance of [[Regularizer]](eg. L1 or L2 regularization), applied to the input weights matrices
:param uRegularizer: instance [[Regularizer]](eg. L1 or L2 regularization), applied to the recurrent weights matrices
:param bRegularizer: instance of [[Regularizer]]applied to the bias.
:param with_peephole: whether use last cell status control a gate.
>>> convlstm = ConvLSTMPeephole3D(4, 3, 3, 3, 1, L1Regularizer(0.5), L1Regularizer(0.5), L1Regularizer(0.5))
creating: createL1Regularizer
creating: createL1Regularizer
creating: createL1Regularizer
creating: createConvLSTMPeephole3D
'''

def __init__(self, input_size, output_size, kernel_i, kernel_c, stride, wRegularizer=None, uRegularizer=None,
bRegularizer=None, with_peephole=True, bigdl_type="float"):
super(ConvLSTMPeephole3D, self).__init__(None, bigdl_type, input_size, output_size, kernel_i, kernel_c, stride,
wRegularizer, uRegularizer, bRegularizer, with_peephole)

def _test():
import doctest
Expand Down
21 changes: 13 additions & 8 deletions spark/dl/src/main/scala/com/intel/analytics/bigdl/nn/Cell.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.intel.analytics.bigdl.tensor.Tensor
import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric
import com.intel.analytics.bigdl.utils.{T, Table}

import scala.collection.mutable
import scala.reflect.ClassTag

/**
Expand Down Expand Up @@ -81,39 +82,43 @@ abstract class Cell[T : ClassTag](
* and recursively intialize all the tensors in the Table.
*
* @param hidden
* @param size batchSize
* @param batchSize batchSize
* @return
*/
def hidResize(hidden: Activity, size: Int, rows: Int = 1, columns: Int = 1): Activity = {
def hidResize(hidden: Activity, batchSize: Int, imageSize: Array[Int] = null): Activity = {
if (hidden == null) {
if (hiddensShape.length == 1) {
hidResize(Tensor[T](), size, rows, columns)
hidResize(Tensor[T](), batchSize)
} else {
val _hidden = T()
var i = 1
while (i <= hiddensShape.length) {
_hidden(i) = Tensor[T]()
i += 1
}
hidResize(_hidden, size, rows, columns)
hidResize(_hidden, batchSize, imageSize)
}
} else {
if (hidden.isInstanceOf[Tensor[T]]) {
require(hidden.isInstanceOf[Tensor[T]],
"Cell: hidden should be a Tensor")
hidden.toTensor.resize(size, hiddensShape(0))
hidden.toTensor.resize(batchSize, hiddensShape(0))
} else {
require(hidden.isInstanceOf[Table],
"Cell: hidden should be a Table")
var i = 1
if (rows== 1 && columns==1) {
if (null == imageSize) {
while (i <= hidden.toTable.length()) {
hidden.toTable[Tensor[T]](i).resize(size, hiddensShape(i - 1))
hidden.toTable[Tensor[T]](i).resize(batchSize, hiddensShape(i - 1))
i += 1
}
} else {
val sizes = new Array[Int](imageSize.length + 2)
sizes(0) = batchSize
Array.copy(imageSize, 0, sizes, 2, imageSize.size)
while (i <= hidden.toTable.length()) {
hidden.toTable[Tensor[T]](i).resize(size, hiddensShape(i - 1), rows, columns)
sizes(1) = hiddensShape(i - 1)
hidden.toTable[Tensor[T]](i).resize(sizes)
i += 1
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import scala.reflect.ClassTag
applied to the bias.
* @param withPeephole: whether use last cell status control a gate.
*/
class ConvLSTMPeephole[T : ClassTag] (
class ConvLSTMPeephole2D[T : ClassTag](
val inputSize: Int,
val outputSize: Int,
val kernelI: Int,
Expand Down Expand Up @@ -202,10 +202,10 @@ class ConvLSTMPeephole[T : ClassTag] (
convlstm
}

override def canEqual(other: Any): Boolean = other.isInstanceOf[ConvLSTMPeephole[T]]
override def canEqual(other: Any): Boolean = other.isInstanceOf[ConvLSTMPeephole2D[T]]

override def equals(other: Any): Boolean = other match {
case that: ConvLSTMPeephole[T] =>
case that: ConvLSTMPeephole2D[T] =>
super.equals(that) &&
(that canEqual this) &&
inputSize == that.inputSize &&
Expand All @@ -227,11 +227,11 @@ class ConvLSTMPeephole[T : ClassTag] (
cell.reset()
}

override def toString: String = s"ConvLSTMPeephole($inputSize, $outputSize," +
override def toString: String = s"ConvLSTMPeephole2D($inputSize, $outputSize," +
s"$kernelI, $kernelC, $stride)"
}

object ConvLSTMPeephole {
object ConvLSTMPeephole2D {
def apply[@specialized(Float, Double) T: ClassTag](
inputSize: Int,
outputSize: Int,
Expand All @@ -242,8 +242,8 @@ object ConvLSTMPeephole {
uRegularizer: Regularizer[T] = null,
bRegularizer: Regularizer[T] = null,
withPeephole: Boolean = true
)(implicit ev: TensorNumeric[T]): ConvLSTMPeephole[T] = {
new ConvLSTMPeephole[T](inputSize, outputSize, kernelI, kernelC, stride,
)(implicit ev: TensorNumeric[T]): ConvLSTMPeephole2D[T] = {
new ConvLSTMPeephole2D[T](inputSize, outputSize, kernelI, kernelC, stride,
wRegularizer, uRegularizer, bRegularizer, withPeephole)
}
}
Expand Down
Loading

0 comments on commit d42feec

Please sign in to comment.