def test_LayerNormalization(axis): with CustomObjectScope({'LayerNormalization': layers.LayerNormalization}): layer_test(layers.LayerNormalization, kwargs={ "axis": axis, }, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_CrossNet(layer_num, ): with CustomObjectScope({'CrossNet': layers.CrossNet}): layer_test(layers.CrossNet, kwargs={ 'layer_num': layer_num, }, input_shape=(2, 3))
def test_PredictionLayer(task, use_bias): with CustomObjectScope({'PredictionLayer': layers.PredictionLayer}): layer_test(layers.PredictionLayer, kwargs={ 'task': task, 'use_bias': use_bias }, input_shape=(BATCH_SIZE, 1))
def test_FwFM(reg_strength): with CustomObjectScope({'FwFMLayer': layers.FwFMLayer}): layer_test(layers.FwFMLayer, kwargs={ 'num_fields': FIELD_SIZE, 'regularizer': reg_strength }, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_BiLSTM(merge_mode): with CustomObjectScope({'BiLSTM': sequence.BiLSTM}): layer_test(sequence.BiLSTM, kwargs={ 'merge_mode': merge_mode, 'units': EMBEDDING_SIZE }, input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE))
def test_KMaxPooling(): with CustomObjectScope({'KMaxPooling': sequence.KMaxPooling}): layer_test(sequence.KMaxPooling, kwargs={ 'k': 3, 'axis': 1 }, input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE, 2))
def test_MLP(hidden_size, use_bn): with CustomObjectScope({'MLP': layers.MLP}): layer_test(layers.MLP, kwargs={ 'hidden_size': hidden_size, 'use_bn': use_bn }, input_shape=(BATCH_SIZE, EMBEDDING_SIZE))
def test_LocalActivationUnit(hidden_units, activation): if tf.__version__ >= '1.13.0' and activation != 'sigmoid': return with CustomObjectScope({'LocalActivationUnit': layers.LocalActivationUnit}): layer_test(layers.LocalActivationUnit, kwargs={'hidden_units': hidden_units, 'activation': activation, 'dropout_rate': 0.5}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE)])
def test_Linear(): with CustomObjectScope({'Linear': Linear}): layer_test(Linear, kwargs={ 'mode': 1, 'use_bias': True }, input_shape=(BATCH_SIZE, EMBEDDING_SIZE))
def test_test_PredictionLayer_invalid(): # with pytest.raises(ValueError): with CustomObjectScope({'PredictionLayer': layers.PredictionLayer}): layer_test(layers.PredictionLayer, kwargs={ 'use_bias': True, }, input_shape=(BATCH_SIZE, 2, 1))
def test_CIN(layer_size, split_half): with CustomObjectScope({'CIN': layers.CIN}): layer_test(layers.CIN, kwargs={ "layer_size": layer_size, "split_half": split_half }, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_CrossNet_invalid(): with pytest.raises(ValueError): with CustomObjectScope({'CrossNet': layers.CrossNet}): layer_test(layers.CrossNet, kwargs={ 'layer_num': 1, 'l2_reg': 0 }, input_shape=(2, 3, 4))
def test_DNN(hidden_units, use_bn): with CustomObjectScope({'DNN': layers.DNN}): layer_test(layers.DNN, kwargs={ 'hidden_units': hidden_units, 'use_bn': use_bn, 'dropout_rate': 0.5 }, input_shape=(BATCH_SIZE, EMBEDDING_SIZE))
def test_Transformer(): if tf.__version__ >= '2.0.0': tf.compat.v1.disable_eager_execution() # todo with CustomObjectScope({'Transformer': sequence.Transformer}): layer_test(sequence.Transformer, kwargs={'att_embedding_size': 1, 'head_num': 8, 'use_layer_norm': True, 'supports_masking': False, 'attention_type': 'additive', 'dropout_rate': 0.5, 'output_type': 'sum'}, input_shape=[(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, 1), (BATCH_SIZE, 1)])
def load_model(input_model_path, input_json_path=None, input_yaml_path=None): custom_objects = { "GlorotUniform": tf.keras.initializers.glorot_uniform, 'hard_swish': hard_swish, 'relu6': relu6 } with CustomObjectScope(custom_objects): if not Path(input_model_path).exists(): raise FileNotFoundError( 'Model file `{}` does not exist.'.format(input_model_path)) try: model = keras.models.load_model(input_model_path) return model except FileNotFoundError as err: logging.error('Input mode file (%s) does not exist.', FLAGS.input_model) raise err except ValueError as wrong_file_err: if input_json_path: if not Path(input_json_path).exists(): raise FileNotFoundError( 'Model description json file `{}` does not exist.'. format(input_json_path)) try: model = model_from_json(open(str(input_json_path)).read()) model.load_weights(input_model_path) return model except Exception as err: logging.error("Couldn't load model from json.") raise err elif input_yaml_path: if not Path(input_yaml_path).exists(): raise FileNotFoundError( 'Model description yaml file `{}` does not exist.'. format(input_yaml_path)) try: model = model_from_yaml(open(str(input_yaml_path)).read()) model.load_weights(input_model_path) return model except Exception as err: logging.error("Couldn't load model from yaml.") raise err else: logging.error( 'Input file specified only holds the weights, and not ' 'the model definition. Save the model using ' 'model.save(filename.h5) which will contain the network ' 'architecture as well as its weights. ' 'If the model is saved using the ' 'model.save_weights(filename) function, either ' 'input_model_json or input_model_yaml flags should be set to ' 'to import the network architecture prior to loading the ' 'weights. \n' 'Check the keras documentation for more details ' '(https://keras.io/getting-started/faq/)') raise wrong_file_err
def test_SequencePoolingLayer(seq_len_max, mode): with CustomObjectScope( {'SequencePoolingLayer': sequence.SequencePoolingLayer}): layer_test(sequence.SequencePoolingLayer, kwargs={ 'seq_len_max': seq_len_max, 'mode': mode }, input_shape=[(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, 1)])
def test_LocalActivationUnit(hidden_size, activation): with CustomObjectScope({'LocalActivationUnit': layers.LocalActivationUnit}): layer_test(layers.LocalActivationUnit, kwargs={ 'hidden_size': hidden_size, 'activation': activation }, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE)])
def test_SequencePoolingLayer(mode, supports_masking, input_shape): with CustomObjectScope( {'SequencePoolingLayer': sequence.SequencePoolingLayer}): layer_test(sequence.SequencePoolingLayer, kwargs={ 'mode': mode, 'supports_masking': supports_masking }, input_shape=input_shape, supports_masking=supports_masking)
def test_AttentionSequencePoolingLayer(weight_normalization): with CustomObjectScope({ 'AttentionSequencePoolingLayer': sequence.AttentionSequencePoolingLayer }): layer_test(sequence.AttentionSequencePoolingLayer, kwargs={'weight_normalization': weight_normalization}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, 1)])
def test_InteractingLayer( head_num, use_res, ): with CustomObjectScope({'InteractingLayer': layers.InteractingLayer}): layer_test(layers.InteractingLayer, kwargs={ "head_num": head_num, "use_res": use_res, }, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_PositionEncoding(pos_embedding_trainable, zero_pad): with CustomObjectScope({ 'PositionEncoding': sequence.PositionEncoding, "tf": tf }): layer_test(sequence.PositionEncoding, kwargs={ 'pos_embedding_trainable': pos_embedding_trainable, 'zero_pad': zero_pad }, input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE))
def test_SequencePoolingLayer(mode, supports_masking, input_shape): if tf.__version__ >= '2.0.0' and mode != 'sum': #todo check further version return with CustomObjectScope( {'SequencePoolingLayer': sequence.SequencePoolingLayer}): layer_test(sequence.SequencePoolingLayer, kwargs={ 'mode': mode, 'supports_masking': supports_masking }, input_shape=input_shape, supports_masking=supports_masking)
def test_Transformer(): with CustomObjectScope({'Transformer': sequence.Transformer}): layer_test(sequence.Transformer, kwargs={ 'att_embedding_size': 1, 'head_num': 8, 'use_layer_norm': True, 'supports_masking': False }, input_shape=[(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, 1), (BATCH_SIZE, 1)])
def test_FGCNNLayer(): with CustomObjectScope({'FGCNNLayer': layers.FGCNNLayer}): layer_test(layers.FGCNNLayer, kwargs={ 'filters': ( 4, 6, ), 'kernel_width': ( 7, 7, ) }, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_Hash(num_buckets, mask_zero, vocabulary_path, input_data, expected_output): if not hasattr(tf, 'version') or tf.version.VERSION < '2.0.0': return with CustomObjectScope({'Hash': Hash}): layer_test(Hash, kwargs={ 'num_buckets': num_buckets, 'mask_zero': mask_zero, 'vocabulary_path': vocabulary_path }, input_dtype=tf.string, input_data=np.array(input_data, dtype='str'), expected_output_dtype=tf.int64, expected_output=expected_output)
def qt_InceptionV3(weights): with CustomObjectScope({'softmax': softmax}): json_string = open("InceptionV3.json", "r").read() model = model_from_json(json_string) model.load_weights(weights) return model
def test_AFMLayer(): with CustomObjectScope({'AFMLayer': layers.AFMLayer}): layer_test(layers.AFMLayer, kwargs={'dropout_rate': 0.5}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
def test_FM(): with CustomObjectScope({'FM': layers.FM}): layer_test(layers.FM, kwargs={}, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_BiInteractionPooling(): with CustomObjectScope( {'BiInteractionPooling': layers.BiInteractionPooling}): layer_test(layers.BiInteractionPooling, kwargs={}, input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
def test_OutterProductLayer(kernel_type): with CustomObjectScope({'OutterProductLayer': layers.OutterProductLayer}): layer_test(layers.OutterProductLayer, kwargs={'kernel_type': kernel_type}, input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)