From c454f69181b07eca6ed2b499f43a3b41bc951a5b Mon Sep 17 00:00:00 2001 From: Xin Qiu Date: Tue, 26 Nov 2019 13:52:40 +0800 Subject: [PATCH] add callZooFunc and change all callBigDlFunc to callZooFunc (#1793) --- python/dllib/src/bigdl/dllib/keras/base.py | 23 +-- .../src/bigdl/dllib/keras/engine/topology.py | 156 +++++++++--------- .../bigdl/dllib/keras/layers/embeddings.py | 24 +-- .../bigdl/dllib/keras/layers/normalization.py | 20 ++- .../dllib/keras/layers/self_attention.py | 34 ++-- python/dllib/src/bigdl/dllib/keras/models.py | 19 ++- .../dllib/src/bigdl/dllib/keras/optimizers.py | 10 +- 7 files changed, 153 insertions(+), 133 deletions(-) diff --git a/python/dllib/src/bigdl/dllib/keras/base.py b/python/dllib/src/bigdl/dllib/keras/base.py index ad5dbb1add4..5ac6d16f9f1 100644 --- a/python/dllib/src/bigdl/dllib/keras/base.py +++ b/python/dllib/src/bigdl/dllib/keras/base.py @@ -16,6 +16,7 @@ from bigdl.nn.layer import Layer from bigdl.util.common import * +from zoo.common.utils import callZooFunc if sys.version >= '3': long = int @@ -37,10 +38,10 @@ def __call__(self, x): :return: Variable containing current module """ from zoo.pipeline.api.autograd import Variable - return Variable.from_jvalue(callBigDlFunc(self.bigdl_type, - "connectInputs", - self, - to_list(x))) + return Variable.from_jvalue(callZooFunc(self.bigdl_type, + "connectInputs", + self, + to_list(x))) class InferShape(JavaValue): @@ -65,8 +66,8 @@ def get_input_shape(self): Return a list of shape tuples if there are multiple inputs. Return one shape tuple otherwise. """ - input = callBigDlFunc(self.bigdl_type, "getInputShape", - self.value) + input = callZooFunc(self.bigdl_type, "getInputShape", + self.value) return self.__process_shape(input) def get_output_shape(self): @@ -74,8 +75,8 @@ def get_output_shape(self): Return a list of shape tuples if there are multiple outputs. Return one shape tuple otherwise. """ - output = callBigDlFunc(self.bigdl_type, "getOutputShape", - self.value) + output = callZooFunc(self.bigdl_type, "getOutputShape", + self.value) return self.__process_shape(output) @@ -97,8 +98,8 @@ def get_weights_shape(self): """ :return: None if without weights """ - jshapes = callBigDlFunc(self.bigdl_type, "zooGetWeightsShape", - self.value) + jshapes = callZooFunc(self.bigdl_type, "zooGetWeightsShape", + self.value) return [tuple(jshape) for jshape in jshapes] def set_weights(self, weights): @@ -114,7 +115,7 @@ def set_weights(self, weights): "The shape of parameter should be the same, but got %s, %s" % (w.shape, cws) tensors = [JTensor.from_ndarray(param, self.bigdl_type) for param in to_list(weights)] - callBigDlFunc(self.bigdl_type, "zooSetWeights", self.value, tensors) + callZooFunc(self.bigdl_type, "zooSetWeights", self.value, tensors) @classmethod def of(cls, jvalue, bigdl_type="float"): diff --git a/python/dllib/src/bigdl/dllib/keras/engine/topology.py b/python/dllib/src/bigdl/dllib/keras/engine/topology.py index cf2d4f1e346..d20fbf7137b 100644 --- a/python/dllib/src/bigdl/dllib/keras/engine/topology.py +++ b/python/dllib/src/bigdl/dllib/keras/engine/topology.py @@ -21,6 +21,7 @@ from zoo.pipeline.api.keras.base import ZooKerasLayer from zoo.pipeline.api.keras.utils import * from bigdl.nn.layer import Layer +from zoo.common.utils import callZooFunc if sys.version >= '3': long = int @@ -69,11 +70,11 @@ def compile(self, optimizer, loss, metrics=None): criterion = CustomLoss(loss, self.get_output_shape()[1:]) if metrics and all(isinstance(metric, six.string_types) for metric in metrics): metrics = to_bigdl_metrics(metrics, loss) - callBigDlFunc(self.bigdl_type, "zooCompile", - self.value, - optimizer, - criterion, - metrics) + callZooFunc(self.bigdl_type, "zooCompile", + self.value, + optimizer, + criterion, + metrics) def set_tensorboard(self, log_dir, app_name): """ @@ -88,10 +89,10 @@ def set_tensorboard(self, log_dir, app_name): log_dir: The base directory path to store training and validation logs. app_name: The name of the application. """ - callBigDlFunc(self.bigdl_type, "zooSetTensorBoard", - self.value, - log_dir, - app_name) + callZooFunc(self.bigdl_type, "zooSetTensorBoard", + self.value, + log_dir, + app_name) def get_train_summary(self, tag=None): """ @@ -106,8 +107,8 @@ def get_train_summary(self, tag=None): raise TypeError('Only "Loss", "LearningRate", "Throughput"' + 'are supported in train summary') - return callBigDlFunc(self.bigdl_type, "zooGetScalarFromSummary", - self.value, tag, "Train") + return callZooFunc(self.bigdl_type, "zooGetScalarFromSummary", + self.value, tag, "Train") def get_validation_summary(self, tag=None): """ @@ -124,8 +125,8 @@ def get_validation_summary(self, tag=None): if tag not in validation_set: raise TypeError('Only subclasses of ValidationMethod are supported,' + 'which are ' + str(validation_set)) - return callBigDlFunc(self.bigdl_type, "zooGetScalarFromSummary", - self.value, tag, "Validation") + return callZooFunc(self.bigdl_type, "zooGetScalarFromSummary", + self.value, tag, "Validation") def set_checkpoint(self, path, over_write=True): """ @@ -136,18 +137,18 @@ def set_checkpoint(self, path, over_write=True): path: The path to save snapshots. Make sure this path exists beforehand. over_write: Whether to overwrite existing snapshots in the given path. Default is True. """ - callBigDlFunc(self.bigdl_type, "zooSetCheckpoint", - self.value, - path, - over_write) + callZooFunc(self.bigdl_type, "zooSetCheckpoint", + self.value, + path, + over_write) def clear_gradient_clipping(self): """ Clear gradient clipping parameters. In this case, gradient clipping will not be applied. In order to take effect, it needs to be called before fit. """ - callBigDlFunc(self.bigdl_type, "zooClearGradientClipping", - self.value) + callZooFunc(self.bigdl_type, "zooClearGradientClipping", + self.value) def set_constant_gradient_clipping(self, min, max): """ @@ -158,10 +159,10 @@ def set_constant_gradient_clipping(self, min, max): min: The minimum value to clip by. Float. max: The maximum value to clip by. Float. """ - callBigDlFunc(self.bigdl_type, "zooSetConstantGradientClipping", - self.value, - float(min), - float(max)) + callZooFunc(self.bigdl_type, "zooSetConstantGradientClipping", + self.value, + float(min), + float(max)) def set_gradient_clipping_by_l2_norm(self, clip_norm): """ @@ -171,16 +172,16 @@ def set_gradient_clipping_by_l2_norm(self, clip_norm): # Arguments clip_norm: Gradient L2-Norm threshold. Float. """ - callBigDlFunc(self.bigdl_type, "zooSetGradientClippingByL2Norm", - self.value, - float(clip_norm)) + callZooFunc(self.bigdl_type, "zooSetGradientClippingByL2Norm", + self.value, + float(clip_norm)) def set_evaluate_status(self): """ Set the model to be in evaluate status, i.e. remove the effect of Dropout, etc. """ - callBigDlFunc(self.bigdl_type, "zooSetEvaluateStatus", - self.value) + callZooFunc(self.bigdl_type, "zooSetEvaluateStatus", + self.value) return self def fit(self, x, y=None, batch_size=32, nb_epoch=10, @@ -212,31 +213,31 @@ def fit(self, x, y=None, batch_size=32, nb_epoch=10, x, y = x[:split_index], y[:split_index] validation_data = to_sample_rdd(*validation_data) training_data = to_sample_rdd(x, y) - elif (isinstance(x, RDD) or isinstance(x, ImageSet) or isinstance(x, TextSet))\ + elif (isinstance(x, RDD) or isinstance(x, ImageSet) or isinstance(x, TextSet)) \ or isinstance(x, FeatureSet) and not y: training_data = x else: raise TypeError("Unsupported training data type: %s" % type(x)) - callBigDlFunc(self.bigdl_type, "zooFit", - self.value, - training_data, - batch_size, - nb_epoch, - validation_data) + callZooFunc(self.bigdl_type, "zooFit", + self.value, + training_data, + batch_size, + nb_epoch, + validation_data) else: if validation_data: val_x = [JTensor.from_ndarray(x) for x in to_list(validation_data[0])] val_y = JTensor.from_ndarray(validation_data[1]) else: val_x, val_y = None, None - callBigDlFunc(self.bigdl_type, "zooFit", - self.value, - [JTensor.from_ndarray(x) for x in to_list(x)], - JTensor.from_ndarray(y), - batch_size, - nb_epoch, - val_x, - val_y) + callZooFunc(self.bigdl_type, "zooFit", + self.value, + [JTensor.from_ndarray(x) for x in to_list(x)], + JTensor.from_ndarray(y), + batch_size, + nb_epoch, + val_x, + val_y) def evaluate(self, x, y=None, batch_size=32): """ @@ -254,10 +255,10 @@ def evaluate(self, x, y=None, batch_size=32): data = x else: raise TypeError("Unsupported evaluation data type: %s" % type(x)) - return callBigDlFunc(self.bigdl_type, "zooEvaluate", - self.value, - data, - batch_size) + return callZooFunc(self.bigdl_type, "zooEvaluate", + self.value, + data, + batch_size) def forward(self, input): """ @@ -268,11 +269,11 @@ def forward(self, input): :return: ndarray or list of ndarray """ jinput, input_is_table = self.check_input(input) - output = callBigDlFunc(self.bigdl_type, - "zooForward", - self.value, - jinput, - input_is_table) + output = callZooFunc(self.bigdl_type, + "zooForward", + self.value, + jinput, + input_is_table) return self.convert_output(output) @staticmethod @@ -298,10 +299,10 @@ def predict(self, x, batch_per_thread=4, distributed=True): Default is True. In local mode, x must be a Numpy array. """ if isinstance(x, ImageSet) or isinstance(x, TextSet): - results = callBigDlFunc(self.bigdl_type, "zooPredict", - self.value, - x, - batch_per_thread) + results = callZooFunc(self.bigdl_type, "zooPredict", + self.value, + x, + batch_per_thread) return ImageSet(results) if isinstance(x, ImageSet) else TextSet(results) if distributed: if isinstance(x, np.ndarray): @@ -310,17 +311,17 @@ def predict(self, x, batch_per_thread=4, distributed=True): data_rdd = x else: raise TypeError("Unsupported prediction data type: %s" % type(x)) - results = callBigDlFunc(self.bigdl_type, "zooPredict", - self.value, - data_rdd, - batch_per_thread) + results = callZooFunc(self.bigdl_type, "zooPredict", + self.value, + data_rdd, + batch_per_thread) return results.map(lambda result: Layer.convert_output(result)) else: if isinstance(x, np.ndarray) or isinstance(x, list): - results = callBigDlFunc(self.bigdl_type, "zooPredict", - self.value, - self._to_jtensors(x), - batch_per_thread) + results = callZooFunc(self.bigdl_type, "zooPredict", + self.value, + self._to_jtensors(x), + batch_per_thread) return [Layer.convert_output(result) for result in results] else: raise TypeError("Unsupported prediction data type: %s" % type(x)) @@ -344,11 +345,11 @@ def predict_classes(self, x, batch_per_thread=4, zero_based_label=True): data_rdd = x else: raise TypeError("Unsupported prediction data type: %s" % type(x)) - return callBigDlFunc(self.bigdl_type, "zooPredictClasses", - self.value, - data_rdd, - batch_per_thread, - zero_based_label) + return callZooFunc(self.bigdl_type, "zooPredictClasses", + self.value, + data_rdd, + batch_per_thread, + zero_based_label) def get_layer(self, name): layer = [l for l in self.layers if l.name() == name] @@ -384,23 +385,23 @@ def summary(self, line_length=120, positions=[.33, .55, .67, 1.]): If the field has a larger length, the remaining part will be trimmed. If the field has a smaller length, the remaining part will be white spaces. """ - callBigDlFunc(self.bigdl_type, "zooKerasNetSummary", - self.value, - line_length, - [float(p) for p in positions]) + callZooFunc(self.bigdl_type, "zooKerasNetSummary", + self.value, + line_length, + [float(p) for p in positions]) def to_model(self): from zoo.pipeline.api.keras.models import Model - return Model.from_jvalue(callBigDlFunc(self.bigdl_type, "kerasNetToModel", self.value)) + return Model.from_jvalue(callZooFunc(self.bigdl_type, "kerasNetToModel", self.value)) @property def layers(self): - jlayers = callBigDlFunc(self.bigdl_type, "getSubModules", self) + jlayers = callZooFunc(self.bigdl_type, "getSubModules", self) layers = [Layer.of(jlayer) for jlayer in jlayers] return layers def flattened_layers(self, include_container=False): - jlayers = callBigDlFunc(self.bigdl_type, "getFlattenSubModules", self, include_container) + jlayers = callZooFunc(self.bigdl_type, "getFlattenSubModules", self, include_container) layers = [Layer.of(jlayer) for jlayer in jlayers] return layers @@ -417,6 +418,7 @@ class Input(autograd.Variable): >>> input = Input(name="input1", shape=(3, 5)) creating: createZooKerasInput """ + def __init__(self, shape=None, name=None, bigdl_type="float"): super(Input, self).__init__(input_shape=list(shape) if shape else None, node=None, jvalue=None, name=name) @@ -434,6 +436,7 @@ class InputLayer(ZooKerasLayer): >>> inputlayer = InputLayer(input_shape=(3, 5), name="input1") creating: createZooKerasInputLayer """ + def __init__(self, input_shape=None, **kwargs): super(InputLayer, self).__init__(None, list(input_shape) if input_shape else None, @@ -466,6 +469,7 @@ class Merge(ZooKerasLayer): >>> merge = Merge(layers=[l1, l2], mode='sum', name="merge1") creating: createZooKerasMerge """ + def __init__(self, layers=None, mode="sum", concat_axis=-1, input_shape=None, **kwargs): super(Merge, self).__init__(None, diff --git a/python/dllib/src/bigdl/dllib/keras/layers/embeddings.py b/python/dllib/src/bigdl/dllib/keras/layers/embeddings.py index 5cbf0628875..15938dbf8eb 100644 --- a/python/dllib/src/bigdl/dllib/keras/layers/embeddings.py +++ b/python/dllib/src/bigdl/dllib/keras/layers/embeddings.py @@ -16,7 +16,8 @@ import sys -from bigdl.util.common import callBigDlFunc, JTensor +from bigdl.util.common import JTensor +from zoo.common.utils import callZooFunc from ..engine.topology import ZooKerasLayer if sys.version >= '3': @@ -59,11 +60,12 @@ class Embedding(ZooKerasLayer): >>> embedding = Embedding(10, 200, weights=np.random.random([10, 200]), input_length=10) creating: createZooKerasEmbedding """ + def __init__(self, input_dim, output_dim, init="uniform", weights=None, trainable=True, input_length=None, W_regularizer=None, input_shape=None, mask_zero=False, padding_value=0, zero_based_id=True, **kwargs): if input_length: - input_shape = (input_length, ) + input_shape = (input_length,) super(Embedding, self).__init__(None, input_dim, output_dim, @@ -107,10 +109,11 @@ class WordEmbedding(ZooKerasLayer): name: String to set the name of the layer. If not specified, its name will by default to be a generated string. """ + def __init__(self, embedding_file, word_index=None, trainable=False, input_length=None, input_shape=None, **kwargs): if input_length: - input_shape = (input_length, ) + input_shape = (input_length,) super(WordEmbedding, self).__init__(None, embedding_file, word_index, @@ -134,8 +137,8 @@ def get_word_index(embedding_file, bigdl_type="float"): Dictionary of word (string) and its corresponding index (int) obtained from the given embedding file. """ - return callBigDlFunc(bigdl_type, "wordEmbeddingGetWordIndex", - embedding_file) + return callZooFunc(bigdl_type, "wordEmbeddingGetWordIndex", + embedding_file) def prepare_embedding(embedding_file, word_index=None, @@ -153,11 +156,11 @@ def prepare_embedding(embedding_file, word_index=None, # Return Pretrained embedding weights as a numpy array. """ - return callBigDlFunc("float", "prepareEmbedding", - embedding_file, - word_index, - randomize_unknown, - normalize).to_ndarray() + return callZooFunc("float", "prepareEmbedding", + embedding_file, + word_index, + randomize_unknown, + normalize).to_ndarray() class SparseEmbedding(ZooKerasLayer): @@ -192,6 +195,7 @@ class SparseEmbedding(ZooKerasLayer): >>> sparse_embedding = SparseEmbedding(input_dim=10, output_dim=4, input_shape=(10, )) creating: createZooKerasSparseEmbedding """ + def __init__(self, input_dim, output_dim, combiner="sum", max_norm=-1.0, init="uniform", W_regularizer=None, input_shape=None, **kwargs): super(SparseEmbedding, self).__init__(None, diff --git a/python/dllib/src/bigdl/dllib/keras/layers/normalization.py b/python/dllib/src/bigdl/dllib/keras/layers/normalization.py index 19ec089fc29..73e5bf00047 100644 --- a/python/dllib/src/bigdl/dllib/keras/layers/normalization.py +++ b/python/dllib/src/bigdl/dllib/keras/layers/normalization.py @@ -17,7 +17,8 @@ import sys from ..engine.topology import ZooKerasLayer -from bigdl.util.common import callBigDlFunc, JTensor +from bigdl.util.common import JTensor +from zoo.common.utils import callZooFunc if sys.version >= '3': long = int @@ -51,6 +52,7 @@ class BatchNormalization(ZooKerasLayer): >>> batchnormalization = BatchNormalization(input_shape=(3, 12, 12), name="bn1") creating: createZooKerasBatchNormalization """ + def __init__(self, epsilon=0.001, mode=0, axis=1, momentum=0.99, beta_init="zero", gamma_init="one", dim_ordering="th", input_shape=None, **kwargs): if mode != 0: @@ -75,8 +77,8 @@ def set_running_mean(self, running_mean): Set the running mean of the BatchNormalization layer. :param running_mean: a Numpy array. """ - callBigDlFunc(self.bigdl_type, "setRunningMean", - self.value, JTensor.from_ndarray(running_mean)) + callZooFunc(self.bigdl_type, "setRunningMean", + self.value, JTensor.from_ndarray(running_mean)) return self def set_running_std(self, running_std): @@ -84,20 +86,20 @@ def set_running_std(self, running_std): Set the running variance of the BatchNormalization layer. :param running_std: a Numpy array. """ - callBigDlFunc(self.bigdl_type, "setRunningStd", - self.value, JTensor.from_ndarray(running_std)) + callZooFunc(self.bigdl_type, "setRunningStd", + self.value, JTensor.from_ndarray(running_std)) return self def get_running_mean(self): """ Get the running meaning of the BatchNormalization layer. """ - return callBigDlFunc(self.bigdl_type, "getRunningMean", - self.value).to_ndarray() + return callZooFunc(self.bigdl_type, "getRunningMean", + self.value).to_ndarray() def get_running_std(self): """ Get the running variance of the BatchNormalization layer. """ - return callBigDlFunc(self.bigdl_type, "getRunningStd", - self.value).to_ndarray() + return callZooFunc(self.bigdl_type, "getRunningStd", + self.value).to_ndarray() diff --git a/python/dllib/src/bigdl/dllib/keras/layers/self_attention.py b/python/dllib/src/bigdl/dllib/keras/layers/self_attention.py index f0114d4bdaf..8874bea855f 100644 --- a/python/dllib/src/bigdl/dllib/keras/layers/self_attention.py +++ b/python/dllib/src/bigdl/dllib/keras/layers/self_attention.py @@ -20,7 +20,7 @@ from bigdl.nn.layer import Sum from bigdl.nn.layer import Layer -from bigdl.util.common import callBigDlFunc +from zoo.common.utils import callZooFunc from zoo.models.common import ZooModel from zoo.pipeline.api.keras.engine import ZooKerasLayer @@ -66,6 +66,7 @@ class TransformerLayer(ZooKerasLayer): embedding_layer: embedding layer input_shape: input shape """ + def __init__(self, n_block, hidden_drop, attn_drop, n_head, initializer_range, bidirectional, output_all_block, embedding_layer, input_shape, intermediate_size=0, bigdl_type="float"): @@ -89,15 +90,15 @@ def __init__(self, n_block, hidden_drop, attn_drop, n_head, initializer_range, b next_input = embedding - output = [None]*n_block + output = [None] * n_block output[0] = self.block(next_input, hidden_size, extended_attention_mask) - for index in range(n_block-1): + for index in range(n_block - 1): o = self.block(output[index], hidden_size, extended_attention_mask) - output[index+1] = o + output[index + 1] = o pooler_output = self.pooler(output[-1], hidden_size) - model = Model(inputs, output.append(pooler_output)) if output_all_block\ + model = Model(inputs, output.append(pooler_output)) if output_all_block \ else Model(inputs, [output[-1], pooler_output]) self.value = model.value @@ -125,10 +126,10 @@ def projection_layer(self, output_size): return Convolution1D(output_size, 1, "normal", (0.0, self.initializer_range)) def multi_head_self_attention(self, x, size, attention_mask=None): - c = self.projection_layer(size*3)(x) + c = self.projection_layer(size * 3)(x) query = c.slice(2, 0, size) key = c.slice(2, size, size) - value = c.slice(2, size*2, size) + value = c.slice(2, size * 2, size) q = self.split_heads(query, self.n_head) k = self.split_heads(key, self.n_head, k=True) v = self.split_heads(value, self.n_head) @@ -216,12 +217,12 @@ def init(cls, vocab=40990, seq_len=77, n_block=12, hidden_drop=0.1, postion_input = InputLayer(input_shape=(seq_len,)) embedding = Sequential() - embedding.add(Merge(layers=[word_input, postion_input], mode='concat'))\ - .add(Reshape([seq_len * 2]))\ + embedding.add(Merge(layers=[word_input, postion_input], mode='concat')) \ + .add(Reshape([seq_len * 2])) \ .add(Embedding(vocab, hidden_size, input_length=seq_len * 2, weights=np.random.normal(0.0, initializer_range, (vocab, hidden_size))))\ - .add(Dropout(embedding_drop))\ - .add(Reshape((seq_len, 2, hidden_size)))\ + .add(Dropout(embedding_drop)) \ + .add(Reshape((seq_len, 2, hidden_size))) \ .add(KerasLayerWrapper(Sum(dimension=3, squeeze=True))) # walk around for bug #1208, need remove this line after the bug fixed embedding.add(KerasLayerWrapper(Squeeze(dim=3))) @@ -258,6 +259,7 @@ class BERT(TransformerLayer): embedding_layer: embedding layer input_shape: input shape """ + def __init__(self, n_block, n_head, intermediate_size, hidden_drop, attn_drop, initializer_range, output_all_block, embedding_layer, input_shape, bigdl_type="float"): @@ -285,9 +287,9 @@ def __init__(self, n_block, n_head, intermediate_size, hidden_drop, attn_drop, model_output = [None] * n_block model_output[0] = self.block(next_input, self.hidden_size, extended_attention_mask) - for _ in range(n_block-1): + for _ in range(n_block - 1): output = self.block(model_output[_], self.hidden_size, extended_attention_mask) - model_output[_+1] = output + model_output[_ + 1] = output pooler_output = self.pooler(model_output[-1], self.hidden_size) @@ -304,7 +306,7 @@ def projection_layer(self, output_size): return Dense(output_size, "normal", (0.0, self.initializer_range)) def build_input(self, input_shape): - if any(not isinstance(i, list) and not isinstance(i, tuple) for i in input_shape)\ + if any(not isinstance(i, list) and not isinstance(i, tuple) for i in input_shape) \ and len(input_shape) != 4: raise TypeError('BERT input must be a list of 4 ndarray (consisting of input' ' sequence, sequence positions, segment id, attention mask)') @@ -376,8 +378,8 @@ def init_from_existing_model(path, weight_path=None, input_seq_len=-1.0, hidden_ Amazon S3 path should be like 's3a://bucket/xxx'. weight_path: The path for pre-trained weights if any. Default is None. """ - jlayer = callBigDlFunc(bigdl_type, "loadBERT", path, weight_path, input_seq_len, - hidden_drop, attn_drop, output_all_block) + jlayer = callZooFunc(bigdl_type, "loadBERT", path, weight_path, input_seq_len, + hidden_drop, attn_drop, output_all_block) model = Layer(jvalue=jlayer, bigdl_type=bigdl_type) model.__class__ = BERT diff --git a/python/dllib/src/bigdl/dllib/keras/models.py b/python/dllib/src/bigdl/dllib/keras/models.py index 76a14bc505d..ebcffea296f 100644 --- a/python/dllib/src/bigdl/dllib/keras/models.py +++ b/python/dllib/src/bigdl/dllib/keras/models.py @@ -18,7 +18,8 @@ from zoo.pipeline.api.utils import remove_batch from .engine.topology import KerasNet -from bigdl.util.common import to_list, callBigDlFunc +from bigdl.util.common import to_list +from zoo.common.utils import callZooFunc if sys.version >= '3': long = int @@ -35,6 +36,7 @@ class Sequential(KerasNet): >>> sequential = Sequential(name="seq1") creating: createZooKerasSequential """ + def __init__(self, jvalue=None, **kwargs): super(Sequential, self).__init__(jvalue, **kwargs) @@ -80,6 +82,7 @@ class Model(KerasNet): output: An output node or a list of output nodes. name: String to specify the name of the graph model. Default is None. """ + def __init__(self, input, output, jvalue=None, **kwargs): super(Model, self).__init__(jvalue, to_list(input), @@ -96,20 +99,20 @@ def save_graph_topology(self, log_path, backward=False): log_path: The path to save the model graph. backward: The name of the application. """ - callBigDlFunc(self.bigdl_type, "zooSaveGraphTopology", - self.value, - log_path, - backward) + callZooFunc(self.bigdl_type, "zooSaveGraphTopology", + self.value, + log_path, + backward) def new_graph(self, outputs): - value = callBigDlFunc(self.bigdl_type, "newGraph", self.value, outputs) + value = callZooFunc(self.bigdl_type, "newGraph", self.value, outputs) return self.from_jvalue(value) def freeze_up_to(self, names): - callBigDlFunc(self.bigdl_type, "freezeUpTo", self.value, names) + callZooFunc(self.bigdl_type, "freezeUpTo", self.value, names) def unfreeze(self, names): - callBigDlFunc(self.bigdl_type, "unFreeze", self.value, names) + callZooFunc(self.bigdl_type, "unFreeze", self.value, names) @staticmethod def from_jvalue(jvalue, bigdl_type="float"): diff --git a/python/dllib/src/bigdl/dllib/keras/optimizers.py b/python/dllib/src/bigdl/dllib/keras/optimizers.py index 516eae15c8b..1c1fe683159 100644 --- a/python/dllib/src/bigdl/dllib/keras/optimizers.py +++ b/python/dllib/src/bigdl/dllib/keras/optimizers.py @@ -17,6 +17,7 @@ from bigdl.util.common import * from bigdl.optim.optimizer import OptimMethod, Default from zoo.pipeline.api.keras.base import ZooKerasCreator +from zoo.common.utils import callZooFunc if sys.version >= '3': long = int @@ -30,6 +31,7 @@ class Adam(OptimMethod, ZooKerasCreator): creating: createZooKerasAdam creating: createDefault """ + def __init__(self, lr=1e-3, beta_1=0.9, @@ -51,7 +53,7 @@ def __init__(self, # 1. This class need to be a subclass of OptimMethod # 2. The constructor of OptimMethod invokes JavaValue.jvm_class_constructor() directly # and does not take the polymorphism. - self.value = callBigDlFunc( + self.value = callZooFunc( bigdl_type, ZooKerasCreator.jvm_class_constructor(self), lr, beta_1, @@ -68,6 +70,7 @@ class AdamWeightDecay(OptimMethod, ZooKerasCreator): >>> adam = AdamWeightDecay() creating: createZooKerasAdamWeightDecay """ + def __init__(self, lr=1e-3, warmup_portion=-1.0, @@ -94,7 +97,7 @@ def __init__(self, # 1. This class need to be a subclass of OptimMethod # 2. The constructor of OptimMethod invokes JavaValue.jvm_class_constructor() directly # and does not take the polymorphism. - self.value = callBigDlFunc( + self.value = callZooFunc( bigdl_type, ZooKerasCreator.jvm_class_constructor(self), lr, warmup_portion, @@ -120,5 +123,6 @@ class PolyEpochDecay(ZooKerasCreator): >>> poly = PolyEpochDecay(0.5, 5) creating: createZooKerasPolyEpochDecay """ + def __init__(self, power, max_epochs, bigdl_type="float"): - JavaValue.__init__(self, None, bigdl_type, power, max_epochs) + JavaValue.__init__(self, None, bigdl_type, power, max_epochs)