Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_globalaveragepooling2d(self):
bigdl_order = self.get_bdim_order()
if bigdl_order == "NCHW":
b_kw = int(self.input_shape[3])
b_kh = int(self.input_shape[2])
else:
b_kw = int(self.input_shape[2])
b_kh = int(self.input_shape[1])
seq = BLayer.Sequential()
blayer = BLayer.SpatialAveragePooling(
kw=b_kw,
kh=b_kh,
dw=b_kw,
dh=b_kh,
pad_w=0,
pad_h=0,
global_pooling=False,
ceil_mode=False,
count_include_pad=False,
divide=True,
format=bigdl_order,
bigdl_type="float"
)
seq.add(blayer)
if bigdl_order == "NCHW":
def create_averagepooling1d(self):
bpadW, bpadH = to_bigdl_2d_padding(self.klayer.border_mode)
seq = BLayer.Sequential()
seq.add(BLayer.Reshape([int(self.input_shape[1]), 1, int(self.input_shape[2])], True))
blayer = BLayer.SpatialAveragePooling(
kw=1,
kh=self.klayer.pool_length,
dw=1,
dh=self.klayer.stride,
pad_w=bpadW,
pad_h=bpadH,
global_pooling=False,
ceil_mode=False,
count_include_pad=False,
divide=True,
format="NHWC",
bigdl_type="float"
)
seq.add(blayer)
def fuse(self, src_blayer, activation): # activation is a layer
seq = BLayer.Sequential()
seq.add(src_blayer)
seq.add(activation)
seq.set_name(src_blayer.name())
return seq
def create_globalmaxpooling2d(self):
bigdl_order = self.get_bdim_order()
if bigdl_order == "NCHW":
b_kw = int(self.input_shape[3])
b_kh = int(self.input_shape[2])
else:
b_kw = int(self.input_shape[2])
b_kh = int(self.input_shape[1])
seq = BLayer.Sequential()
blayer = BLayer.SpatialMaxPooling(
kw=b_kw,
kh=b_kh,
dw=b_kw,
dh=b_kh,
pad_w=0,
pad_h=0,
to_ceil=False,
format=bigdl_order,
bigdl_type="float"
)
seq.add(blayer)
if bigdl_order == "NCHW":
seq.add(BLayer.Squeeze(3, num_input_dims=3))
seq.add(BLayer.Squeeze(2, num_input_dims=2))
else:
n_input_dims=len(self.input_shape[0]) - 1,
bigdl_type="float")
elif self.klayer.mode == "sum":
blayer = BLayer.CAddTable(
inplace=False,
bigdl_type="float")
elif self.klayer.mode == "mul":
blayer = BLayer.CMulTable(bigdl_type="float")
elif self.klayer.mode == "max":
blayer = BLayer.CMaxTable(bigdl_type="float")
elif self.klayer.mode == "dot":
if len(self.input_shape[0]) >= 3:
raise Exception("For merge mode dot, 3D input or above is not supported for now.")
if self.klayer.dot_axes != [1, 1]:
raise Exception("For merge mode dot, only dot_axes=1 is supported for now.")
model = BLayer.Sequential()
blayer = model.add(BLayer.DotProduct(bigdl_type="float"))\
.add(BLayer.Reshape([1], True))
elif self.klayer.mode == "ave":
blayer = BLayer.CAveTable(
inplace=False,
bigdl_type="float")
elif self.klayer.mode in ['cos']:
if len(self.input_shape[0]) >= 3:
raise Exception("For merge mode cos, 3D input or above is not supported for now.")
if self.klayer.dot_axes != [1, 1]:
raise Exception("For merge mode cos, only dot_axes=1 is supported for now.")
blayer = BLayer.Sequential()
blayer.add(BLayer.CosineDistance(bigdl_type="float")).add(BLayer.Reshape([1, 1], True))
else: # invalid mode or lambda functions
raise Exception("Invalid merge mode: `%s`. Lambda/function as merge mode is not supported for now."
% self.klayer.mode)
def create_globalmaxpooling1d(self):
b_kw = 1
b_kh = int(self.input_shape[1])
seq = BLayer.Sequential()
seq.add(BLayer.Reshape([int(self.input_shape[1]), 1, int(self.input_shape[2])], True))
blayer = BLayer.SpatialMaxPooling(
kw=b_kw,
kh=b_kh,
dw=1,
dh=1,
pad_w=0,
pad_h=0,
to_ceil=False,
format="NHWC",
bigdl_type="float"
)
seq.add(blayer)
seq.add(BLayer.Squeeze(3))
seq.add(BLayer.Squeeze(2))
return seq
def create_globalmaxpooling3d(self):
if self.klayer.dim_ordering == "th":
b_kt = int(self.input_shape[2])
b_kw = int(self.input_shape[4])
b_kh = int(self.input_shape[3])
else:
raise Exception("Please use `th` for dim_ordering. `%s` is not supported for now." % self.klayer.dim_ordering)
seq = BLayer.Sequential()
blayer = BLayer.VolumetricMaxPooling(
k_t=b_kt,
k_w=b_kw,
k_h=b_kh,
d_t=1,
d_w=1,
d_h=1,
pad_t=0,
pad_w=0,
pad_h=0,
bigdl_type="float"
)
seq.add(blayer)
seq.add(BLayer.Squeeze(5))
seq.add(BLayer.Squeeze(4))
seq.add(BLayer.Squeeze(3))
def __process_recurrent_layer(self, return_sequences, go_backwards, blayer):
# For recurrent layers,
# handle whether to return the last output sentence or the full sequence;
# handle whether the input will go backwards
model = BLayer.Sequential()
if go_backwards:
model.add(BLayer.Reverse(2))
model.add(blayer)
if not return_sequences:
model.add(BLayer.Select(2, -1))
return model
def create_activation(self):
blayer = get_activation_by_name(self.config["activation"], self.klayer.name)
# SoftMax is different between Keras and BigDL for 3D inputs
if self.config["activation"] == "softmax" and len(self.input_shape) == 3:
model = BLayer.Sequential()
model.add(BLayer.Transpose([(1, 3)]))
model.add(blayer)
model.add(BLayer.Transpose([(1, 3)]))
return model
return blayer