Python tensorflow.python.keras.models.Model() Examples
The following are 30
code examples of tensorflow.python.keras.models.Model().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.keras.models
, or try the search function
.
Example #1
Source File: model.py From progressive-neural-architecture-search with MIT License | 6 votes |
def __init__(self, actions): ''' Utility Model class to construct child models provided with an action list. # Args: actions: list of [input; action] pairs that define the cell. ''' super(ModelGenerator, self).__init__() self.B = len(actions) // 4 self.action_list = np.split(np.array(actions), len(actions) // 2) self.global_counter = 0 self.cell_1 = self.build_cell(self.B, self.action_list, filters=32, stride=(2, 2)) self.cell_2 = self.build_cell(self.B, self.action_list, filters=64, stride=(2, 2)) self.gap = GlobalAveragePooling2D() self.logits = Dense(10, activation='softmax') # only logits
Example #2
Source File: gcn.py From GraphNeuralNetwork with MIT License | 6 votes |
def GCN(adj_dim,feature_dim,n_hidden, num_class, num_layers=2,activation=tf.nn.relu,dropout_rate=0.5, l2_reg=0, feature_less=True, ): Adj = Input(shape=(None,), sparse=True) if feature_less: X_in = Input(shape=(1,), ) emb = Embedding(adj_dim, feature_dim, embeddings_initializer=Identity(1.0), trainable=False) X_emb = emb(X_in) h = Reshape([X_emb.shape[-1]])(X_emb) else: X_in = Input(shape=(feature_dim,), ) h = X_in for i in range(num_layers): if i == num_layers - 1: activation = tf.nn.softmax n_hidden = num_class h = GraphConvolution(n_hidden, activation=activation, dropout_rate=dropout_rate, l2_reg=l2_reg)([h,Adj]) output = h model = Model(inputs=[X_in,Adj], outputs=output) return model
Example #3
Source File: prepare_model.py From camera-trap-classifier with MIT License | 6 votes |
def load_model_and_replace_output(model_old, model_new, new_output_layer): """ Load a model and replace the last layer """ new_input = model_old.input # get old model output before last layer old_output = model_old.layers[-2].output # create a new output layer new_output = (new_output_layer)(old_output) # combine old model with new output layer new_model = Model(inputs=new_input, outputs=new_output) logging.info("Replacing output layer of model") # print layers of old model for layer, i in zip(new_model.layers, range(0, len(new_model.layers))): logging.info("Old model - layer %s: %s" % (i, layer.name)) return new_model
Example #4
Source File: train_ner.py From MAX-Named-Entity-Tagger with Apache License 2.0 | 6 votes |
def _build_model(embedding_weights, char_bidirectional=False, concat_bidirectional=True): word_emb_input, word_emb_output, char_emb_input, char_emb_output = _build_embeddings(embedding_weights, char_bidirectional) # concatenate word embedding and character embedding x = concatenate([word_emb_output, char_emb_output]) x = Dropout(dropout)(x) # construct LSTM layers. Option to use 1 Bidirectonal layer, or one forward and one backward LSTM layer. # Empirical results appear better with bidirectional LSTM here, hence it is the default. if concat_bidirectional: x = Bidirectional(LSTM(hidden_size_lstm, return_sequences=True))(x) else: fw_LSTM_2 = LSTM(hidden_size_lstm, return_sequences=True)(x) bw_LSTM_2 = LSTM(hidden_size_lstm, return_sequences=True, go_backwards=True)(fw_LSTM_2) x = concatenate([fw_LSTM_2, bw_LSTM_2]) x = Dropout(dropout)(x) scores = Dense(n_labels)(x) # Activation Function x = Activation("softmax", name='predict_output')(scores) # create model model = Model([word_emb_input, char_emb_input], x) return model # === Build model ===
Example #5
Source File: sdne.py From GraphEmbedding with MIT License | 6 votes |
def create_model(node_size, hidden_size=[256, 128], l1=1e-5, l2=1e-4): A = Input(shape=(node_size,)) L = Input(shape=(None,)) fc = A for i in range(len(hidden_size)): if i == len(hidden_size) - 1: fc = Dense(hidden_size[i], activation='relu', kernel_regularizer=l1_l2(l1, l2), name='1st')(fc) else: fc = Dense(hidden_size[i], activation='relu', kernel_regularizer=l1_l2(l1, l2))(fc) Y = fc for i in reversed(range(len(hidden_size) - 1)): fc = Dense(hidden_size[i], activation='relu', kernel_regularizer=l1_l2(l1, l2))(fc) A_ = Dense(node_size, 'relu', name='2nd')(fc) model = Model(inputs=[A, L], outputs=[A_, Y]) emb = Model(inputs=A, outputs=Y) return model, emb
Example #6
Source File: test_explanation_model.py From cxplain with MIT License | 5 votes |
def test_mnist_unet_with_shape_valid(self): num_subsamples = 100 (x_train, y_train), (x_test, y_test) = TestUtil.get_mnist(flattened=False, num_subsamples=num_subsamples) explained_model_builder = MLPModelBuilder(num_layers=2, num_units=64, activation="relu", p_dropout=0.2, verbose=0, batch_size=256, learning_rate=0.001, num_epochs=2, early_stopping_patience=128) input_shape = x_train.shape[1:] input_layer = Input(shape=input_shape) last_layer = Flatten()(input_layer) last_layer = explained_model_builder.build(last_layer) last_layer = Dense(y_train.shape[-1], activation="softmax")(last_layer) explained_model = Model(input_layer, last_layer) explained_model.compile(loss="categorical_crossentropy", optimizer="adam") explained_model.fit(x_train, y_train) masking_operation = ZeroMasking() loss = categorical_crossentropy downsample_factors = [(2, 2), (4, 4), (4, 7), (7, 4), (7, 7)] with_bns = [True if i % 2 == 0 else False for i in range(len(downsample_factors))] for downsample_factor, with_bn in zip(downsample_factors, with_bns): model_builder = UNetModelBuilder(downsample_factor, num_layers=2, num_units=64, activation="relu", p_dropout=0.2, verbose=0, batch_size=256, learning_rate=0.001, num_epochs=2, early_stopping_patience=128, with_bn=with_bn) explainer = CXPlain(explained_model, model_builder, masking_operation, loss, downsample_factors=downsample_factor) explainer.fit(x_train, y_train) eval_score = explainer.score(x_test, y_test) train_score = explainer.get_last_fit_score() median = explainer.predict(x_test) self.assertTrue(median.shape == x_test.shape)
Example #7
Source File: tsne_grid.py From tsne-grid with MIT License | 5 votes |
def build_model(): base_model = VGG16(weights='imagenet') top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) return Model(inputs=base_model.input, outputs=top_model(base_model.output))
Example #8
Source File: test_utils.py From models with Apache License 2.0 | 5 votes |
def trivial_model(num_classes): """Trivial model for ImageNet dataset.""" input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape) x = layers.Lambda(lambda x: backend.reshape(x, [-1, 224 * 224 * 3]), name='reshape')(img_input) x = layers.Dense(1, name='fc1')(x) x = layers.Dense(num_classes, name='fc1000')(x) x = layers.Activation('softmax', dtype='float32')(x) return models.Model(img_input, x, name='trivial')
Example #9
Source File: mlr.py From DeepCTR with Apache License 2.0 | 5 votes |
def MLR(region_feature_columns, base_feature_columns=None, region_num=4, l2_reg_linear=1e-5, seed=1024, task='binary', bias_feature_columns=None): """Instantiates the Mixed Logistic Regression/Piece-wise Linear Model. :param region_feature_columns: An iterable containing all the features used by region part of the model. :param base_feature_columns: An iterable containing all the features used by base part of the model. :param region_num: integer > 1,indicate the piece number :param l2_reg_linear: float. L2 regularizer strength applied to weight :param seed: integer ,to use as random seed. :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss :param bias_feature_columns: An iterable containing all the features used by bias part of the model. :return: A Keras model instance. """ if region_num <= 1: raise ValueError("region_num must > 1") if base_feature_columns is None or len(base_feature_columns) == 0: base_feature_columns = region_feature_columns if bias_feature_columns is None: bias_feature_columns = [] features = build_input_features(region_feature_columns + base_feature_columns + bias_feature_columns) inputs_list = list(features.values()) region_score = get_region_score(features, region_feature_columns, region_num, l2_reg_linear, seed) learner_score = get_learner_score(features, base_feature_columns, region_num, l2_reg_linear, seed, task=task) final_logit = dot([region_score, learner_score], axes=-1) if bias_feature_columns is not None and len(bias_feature_columns) > 0: bias_score = get_learner_score(features, bias_feature_columns, 1, l2_reg_linear, seed, prefix='bias_', task='binary') final_logit = dot([final_logit, bias_score], axes=-1) model = Model(inputs=inputs_list, outputs=final_logit) return model
Example #10
Source File: wdl.py From DeepCTR with Apache License 2.0 | 5 votes |
def WDL(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(128, 128), l2_reg_linear=1e-5, l2_reg_embedding=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_activation='relu', task='binary'): """Instantiates the Wide&Deep Learning architecture. :param linear_feature_columns: An iterable containing all the features used by linear part of the model. :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN :param l2_reg_linear: float. L2 regularizer strength applied to wide part :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector :param l2_reg_dnn: float. L2 regularizer strength applied to DNN :param seed: integer ,to use as random seed. :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate. :param dnn_activation: Activation function to use in DNN :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss :return: A Keras model instance. """ features = build_input_features( linear_feature_columns + dnn_feature_columns) inputs_list = list(features.values()) linear_logit = get_linear_logit(features, linear_feature_columns, seed=seed, prefix='linear', l2_reg=l2_reg_linear) sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns, l2_reg_embedding, seed) dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list) dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed)(dnn_input) dnn_logit = Dense( 1, use_bias=False, activation=None)(dnn_out) final_logit = add_func([dnn_logit, linear_logit]) output = PredictionLayer(task)(final_logit) model = Model(inputs=inputs_list, outputs=output) return model
Example #11
Source File: gat.py From GraphNeuralNetwork with MIT License | 5 votes |
def GAT(adj_dim,feature_dim,num_class,num_layers=2,n_attn_heads = 8,att_embedding_size=8,dropout_rate=0.0,l2_reg=0.0,use_bias=True): X_in = Input(shape=(feature_dim,)) A_in = Input(shape=(adj_dim,)) h = X_in for _ in range(num_layers-1): h = GATLayer(att_embedding_size=att_embedding_size, head_num=n_attn_heads, dropout_rate=dropout_rate, l2_reg=l2_reg, activation=tf.nn.elu, use_bias=use_bias, )([h, A_in]) h = GATLayer(att_embedding_size=num_class, head_num=1, dropout_rate=dropout_rate, l2_reg=l2_reg, activation=tf.nn.softmax, use_bias=use_bias, reduction='mean')([h, A_in]) model = Model(inputs=[X_in, A_in], outputs=h) return model
Example #12
Source File: graphsage.py From GraphNeuralNetwork with MIT License | 5 votes |
def GraphSAGE(feature_dim, neighbor_num, n_hidden, n_classes, use_bias=True, activation=tf.nn.relu, aggregator_type='mean', dropout_rate=0.0, l2_reg=0): features = Input(shape=(feature_dim,)) node_input = Input(shape=(1,), dtype=tf.int32) neighbor_input = [Input(shape=(l,), dtype=tf.int32) for l in neighbor_num] if aggregator_type == 'mean': aggregator = MeanAggregator else: aggregator = PoolingAggregator h = features for i in range(0, len(neighbor_num)): if i > 0: feature_dim = n_hidden if i == len(neighbor_num) - 1: activation = tf.nn.softmax n_hidden = n_classes h = aggregator(units=n_hidden, input_dim=feature_dim, activation=activation, l2_reg=l2_reg, use_bias=use_bias, dropout_rate=dropout_rate, neigh_max=neighbor_num[i], aggregator=aggregator_type)( [h, node_input, neighbor_input[i]]) # output = h input_list = [features, node_input] + neighbor_input model = Model(input_list, outputs=output) return model
Example #13
Source File: utils.py From camera-trap-classifier with MIT License | 5 votes |
def get_gpu_base_model(model): """ get multi_gpu base model """ for layer in model.layers: if isinstance(layer, Model): return layer return None
Example #14
Source File: utils.py From camera-trap-classifier with MIT License | 5 votes |
def is_multi_gpu_model(model): """ Check if a specific model is a multi_gpu model by checking if one of the layers is a keras model itself """ for layer in model.layers: if isinstance(layer, Model): return True return False
Example #15
Source File: trivial_model.py From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 | 5 votes |
def trivial_model(num_classes): """Trivial model for ImageNet dataset.""" input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape) x = layers.Lambda(lambda x: backend.reshape(x, [-1, 224 * 224 * 3]), name='reshape')(img_input) x = layers.Dense(1, name='fc1')(x) x = layers.Dense(num_classes, name='fc1000')(x) x = layers.Activation('softmax', dtype='float32')(x) return models.Model(img_input, x, name='trivial')
Example #16
Source File: ae.py From BVAE-tf with The Unlicense | 5 votes |
def __init__(self, encoderArchitecture, decoderArchitecture): self.encoder = encoderArchitecture.model self.decoder = decoderArchitecture.model self.ae = Model(self.encoder.inputs, self.decoder(self.encoder.outputs))
Example #17
Source File: backend.py From FATE with Apache License 2.0 | 5 votes |
def export_model(self): with tempfile.TemporaryDirectory() as tmp_path: # Comment this block because tf 1.15 is not supporting Keras Customized Layer # try: # # LOGGER.info("Model saved with model.save method.") # tf.keras.models.save_model(self._model, filepath=tmp_path, save_format="tf") # except NotImplementedError: # import warnings # warnings.warn('Saving the model as SavedModel is still in experimental stages. ' # 'trying tf.keras.experimental.export_saved_model...') tf.keras.experimental.export_saved_model(self._model, saved_model_path=tmp_path) model_bytes = zip_dir_as_bytes(tmp_path) return model_bytes
Example #18
Source File: backend.py From FATE with Apache License 2.0 | 5 votes |
def export_model(self): with tempfile.TemporaryDirectory() as tmp_path: # try: # # LOGGER.info("Model saved with model.save method.") # tf.keras.models.save_model(self._model, filepath=tmp_path, save_format="tf") # except NotImplementedError: # import warnings # warnings.warn('Saving the model as SavedModel is still in experimental stages. ' # 'trying tf.keras.experimental.export_saved_model...') tf.keras.experimental.export_saved_model(self._model, saved_model_path=tmp_path) model_bytes = zip_dir_as_bytes(tmp_path) return model_bytes
Example #19
Source File: model_builder.py From BCNN-keras-clean with MIT License | 5 votes |
def save_model( size_height=448, size_width=448, no_class=200 ): '''Save Bilinear CNN to current directory. The model will be saved as `model.json`. Args: size_height: default 448. size_width: default 448. no_class: number of prediction classes. Returns: Bilinear CNN model. ''' model = buil_bcnn( size_height=size_height, size_width=size_width, no_class=no_class) # Save model json model_json = model.to_json() with open('./model.json', 'w') as f: f.write(model_json) print('Model is saved to ./model.json') return True
Example #20
Source File: __init__.py From ImageAI with MIT License | 5 votes |
def save_model_to_tensorflow(self, new_model_folder, new_model_name=""): """ 'save_model_to_tensorflow' function allows you to save your loaded Keras (.h5) model and save it to the Tensorflow (.pb) model format. - new_model_folder (required), the path to the folder you want the converted Tensorflow model to be saved - new_model_name (required), the desired filename for your converted Tensorflow model e.g 'my_new_model.pb' :param new_model_folder: :param new_model_name: :return: """ if(self.__modelLoaded == True): out_prefix = "output_" output_dir = new_model_folder if os.path.exists(output_dir) == False: os.mkdir(output_dir) model_name = os.path.join(output_dir, new_model_name) keras_model = self.__model_collection[0] out_nodes = [] for i in range(len(keras_model.outputs)): out_nodes.append(out_prefix + str(i + 1)) tf.identity(keras_model.output[i], out_prefix + str(i + 1)) sess = K.get_session() from tensorflow.python.framework import graph_util, graph_io init_graph = sess.graph.as_graph_def() main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes) graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False) print("Tensorflow Model Saved")
Example #21
Source File: line.py From GraphEmbedding with MIT License | 5 votes |
def create_model(numNodes, embedding_size, order='second'): v_i = Input(shape=(1,)) v_j = Input(shape=(1,)) first_emb = Embedding(numNodes, embedding_size, name='first_emb') second_emb = Embedding(numNodes, embedding_size, name='second_emb') context_emb = Embedding(numNodes, embedding_size, name='context_emb') v_i_emb = first_emb(v_i) v_j_emb = first_emb(v_j) v_i_emb_second = second_emb(v_i) v_j_context_emb = context_emb(v_j) first = Lambda(lambda x: tf.reduce_sum( x[0]*x[1], axis=-1, keep_dims=False), name='first_order')([v_i_emb, v_j_emb]) second = Lambda(lambda x: tf.reduce_sum( x[0]*x[1], axis=-1, keep_dims=False), name='second_order')([v_i_emb_second, v_j_context_emb]) if order == 'first': output_list = [first] elif order == 'second': output_list = [second] else: output_list = [first, second] model = Model(inputs=[v_i, v_j], outputs=output_list) return model, {'first': first_emb, 'second': second_emb}
Example #22
Source File: model.py From speaker-recognition-3d-cnn with MIT License | 4 votes |
def _3d_cnn_model(input_shape, num_classes): # Define Model inputs = Input(shape=input_shape, name="input-layer") # Conv 1 X = Conv3D(filters=16, kernel_size=(3, 1, 5), strides=(1, 1, 1), name="conv1-1")(inputs) X = PReLU(name="activation1-1")(X) X = Conv3D(filters=16, kernel_size=(3, 9, 1), strides=(1, 2, 1), name="conv1-2")(X) X = PReLU(name="activation1-2")(X) X = MaxPool3D(pool_size=(1, 1, 2), strides=(1, 1, 2), padding="valid", name="pool-1")(X) # X = Dropout(0.2)(X) # Conv 2 X = Conv3D(filters=32, kernel_size=(3, 1, 4), strides=(1, 1, 1), name="conv2-1")(X) X = PReLU(name="activation2-1")(X) X = Conv3D(filters=32, kernel_size=(3, 8, 1), strides=(1, 2, 1), name="conv2-2")(X) X = PReLU(name="activation2-2")(X) X = MaxPool3D(pool_size=(1, 1, 2), strides=(1, 1, 2), padding="valid", name="pool-2")(X) # X = Dropout(0.2)(X) # Conv 3 X = Conv3D(filters=64, kernel_size=(3, 1, 3), strides=(1, 1, 1), name="conv3-1")(X) X = PReLU(name="activation3-1")(X) X = Conv3D(filters=64, kernel_size=(3, 7, 1), strides=(1, 1, 1), name="conv3-2")(X) X = PReLU(name="activation3-2")(X) # X = Dropout(0.2)(X) # Conv 4 X = Conv3D(filters=128, kernel_size=(3, 1, 3), strides=(1, 1, 1), name="conv4-1")(X) X = PReLU(name="activation4-1")(X) X = Conv3D(filters=128, kernel_size=(3, 7, 1), strides=(1, 1, 1), name="conv4-2")(X) X = PReLU(name="activation4-2")(X) # X = Dropout(0.2)(X) # Flaten X = Flatten()(X) # FC X = Dense(units=128, name="fc", activation='relu')(X) # Final Activation X = Dense(units=num_classes, activation='softmax', name="ac_softmax")(X) model = Model(inputs=inputs, outputs=X) return model
Example #23
Source File: model.py From attention_keras with MIT License | 4 votes |
def define_nmt(hidden_size, batch_size, en_timesteps, en_vsize, fr_timesteps, fr_vsize): """ Defining a NMT model """ # Define an input sequence and process it. if batch_size: encoder_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inputs') decoder_inputs = Input(batch_shape=(batch_size, fr_timesteps - 1, fr_vsize), name='decoder_inputs') else: encoder_inputs = Input(shape=(en_timesteps, en_vsize), name='encoder_inputs') if fr_timesteps: decoder_inputs = Input(shape=(fr_timesteps - 1, fr_vsize), name='decoder_inputs') else: decoder_inputs = Input(shape=(None, fr_vsize), name='decoder_inputs') # Encoder GRU encoder_gru = GRU(hidden_size, return_sequences=True, return_state=True, name='encoder_gru') encoder_out, encoder_state = encoder_gru(encoder_inputs) # Set up the decoder GRU, using `encoder_states` as initial state. decoder_gru = GRU(hidden_size, return_sequences=True, return_state=True, name='decoder_gru') decoder_out, decoder_state = decoder_gru(decoder_inputs, initial_state=encoder_state) # Attention layer attn_layer = AttentionLayer(name='attention_layer') attn_out, attn_states = attn_layer([encoder_out, decoder_out]) # Concat attention input and decoder GRU output decoder_concat_input = Concatenate(axis=-1, name='concat_layer')([decoder_out, attn_out]) # Dense layer dense = Dense(fr_vsize, activation='softmax', name='softmax_layer') dense_time = TimeDistributed(dense, name='time_distributed_layer') decoder_pred = dense_time(decoder_concat_input) # Full model full_model = Model(inputs=[encoder_inputs, decoder_inputs], outputs=decoder_pred) full_model.compile(optimizer='adam', loss='categorical_crossentropy') full_model.summary() """ Inference model """ batch_size = 1 """ Encoder (Inference) model """ encoder_inf_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inf_inputs') encoder_inf_out, encoder_inf_state = encoder_gru(encoder_inf_inputs) encoder_model = Model(inputs=encoder_inf_inputs, outputs=[encoder_inf_out, encoder_inf_state]) """ Decoder (Inference) model """ decoder_inf_inputs = Input(batch_shape=(batch_size, 1, fr_vsize), name='decoder_word_inputs') encoder_inf_states = Input(batch_shape=(batch_size, en_timesteps, hidden_size), name='encoder_inf_states') decoder_init_state = Input(batch_shape=(batch_size, hidden_size), name='decoder_init') decoder_inf_out, decoder_inf_state = decoder_gru(decoder_inf_inputs, initial_state=decoder_init_state) attn_inf_out, attn_inf_states = attn_layer([encoder_inf_states, decoder_inf_out]) decoder_inf_concat = Concatenate(axis=-1, name='concat')([decoder_inf_out, attn_inf_out]) decoder_inf_pred = TimeDistributed(dense)(decoder_inf_concat) decoder_model = Model(inputs=[encoder_inf_states, decoder_init_state, decoder_inf_inputs], outputs=[decoder_inf_pred, attn_inf_states, decoder_inf_state]) return full_model, encoder_model, decoder_model
Example #24
Source File: model.py From attention_keras with MIT License | 4 votes |
def define_nmt(hidden_size, batch_size, en_timesteps, en_vsize, fr_timesteps, fr_vsize): """ Defining a NMT model """ # Define an input sequence and process it. if batch_size: encoder_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inputs') decoder_inputs = Input(batch_shape=(batch_size, fr_timesteps - 1, fr_vsize), name='decoder_inputs') else: encoder_inputs = Input(shape=(en_timesteps, en_vsize), name='encoder_inputs') decoder_inputs = Input(shape=(fr_timesteps - 1, fr_vsize), name='decoder_inputs') # Encoder GRU encoder_gru = Bidirectional(GRU(hidden_size, return_sequences=True, return_state=True, name='encoder_gru'), name='bidirectional_encoder') encoder_out, encoder_fwd_state, encoder_back_state = encoder_gru(encoder_inputs) # Set up the decoder GRU, using `encoder_states` as initial state. decoder_gru = GRU(hidden_size*2, return_sequences=True, return_state=True, name='decoder_gru') decoder_out, decoder_state = decoder_gru( decoder_inputs, initial_state=Concatenate(axis=-1)([encoder_fwd_state, encoder_back_state]) ) # Attention layer attn_layer = AttentionLayer(name='attention_layer') attn_out, attn_states = attn_layer([encoder_out, decoder_out]) # Concat attention input and decoder GRU output decoder_concat_input = Concatenate(axis=-1, name='concat_layer')([decoder_out, attn_out]) # Dense layer dense = Dense(fr_vsize, activation='softmax', name='softmax_layer') dense_time = TimeDistributed(dense, name='time_distributed_layer') decoder_pred = dense_time(decoder_concat_input) # Full model full_model = Model(inputs=[encoder_inputs, decoder_inputs], outputs=decoder_pred) full_model.compile(optimizer='adam', loss='categorical_crossentropy') full_model.summary() """ Inference model """ batch_size = 1 """ Encoder (Inference) model """ encoder_inf_inputs = Input(batch_shape=(batch_size, en_timesteps, en_vsize), name='encoder_inf_inputs') encoder_inf_out, encoder_inf_fwd_state, encoder_inf_back_state = encoder_gru(encoder_inf_inputs) encoder_model = Model(inputs=encoder_inf_inputs, outputs=[encoder_inf_out, encoder_inf_fwd_state, encoder_inf_back_state]) """ Decoder (Inference) model """ decoder_inf_inputs = Input(batch_shape=(batch_size, 1, fr_vsize), name='decoder_word_inputs') encoder_inf_states = Input(batch_shape=(batch_size, en_timesteps, 2*hidden_size), name='encoder_inf_states') decoder_init_state = Input(batch_shape=(batch_size, 2*hidden_size), name='decoder_init') decoder_inf_out, decoder_inf_state = decoder_gru( decoder_inf_inputs, initial_state=decoder_init_state) attn_inf_out, attn_inf_states = attn_layer([encoder_inf_states, decoder_inf_out]) decoder_inf_concat = Concatenate(axis=-1, name='concat')([decoder_inf_out, attn_inf_out]) decoder_inf_pred = TimeDistributed(dense)(decoder_inf_concat) decoder_model = Model(inputs=[encoder_inf_states, decoder_init_state, decoder_inf_inputs], outputs=[decoder_inf_pred, attn_inf_states, decoder_inf_state]) return full_model, encoder_model, decoder_model
Example #25
Source File: pbt_tune_cifar10_with_keras.py From ray with Apache License 2.0 | 4 votes |
def _build_model(self, input_shape): x = Input(shape=(32, 32, 3)) y = x y = Convolution2D( filters=64, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = Convolution2D( filters=64, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = MaxPooling2D(pool_size=2, strides=2, padding="same")(y) y = Convolution2D( filters=128, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = Convolution2D( filters=128, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = MaxPooling2D(pool_size=2, strides=2, padding="same")(y) y = Convolution2D( filters=256, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = Convolution2D( filters=256, kernel_size=3, strides=1, padding="same", activation="relu", kernel_initializer="he_normal")(y) y = MaxPooling2D(pool_size=2, strides=2, padding="same")(y) y = Flatten()(y) y = Dropout(self.config.get("dropout", 0.5))(y) y = Dense( units=10, activation="softmax", kernel_initializer="he_normal")(y) model = Model(inputs=x, outputs=y, name="model1") return model
Example #26
Source File: models.py From BVAE-tf with The Unlicense | 4 votes |
def Build(self): # input layer is from GlobalAveragePooling: inLayer = Input([self.latentSize], self.batchSize) # reexpand the input from flat: net = Reshape((1, 1, self.latentSize))(inLayer) # darknet downscales input by a factor of 32, so we upsample to the second to last output shape: net = UpSampling2D((self.inputShape[0]//32, self.inputShape[1]//32))(net) # TODO try inverting num filter arangement (e.g. 512, 1204, 512, 1024, 512) # and also try (1, 3, 1, 3, 1) for the filter shape net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(512, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(512, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) net = UpSampling2D((2, 2))(net) net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(256, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(256, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) net = UpSampling2D((2, 2))(net) net = ConvBnLRelu(256, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(128, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(256, kernelSize=3)(net, training=self.training) net = UpSampling2D((2, 2))(net) net = ConvBnLRelu(128, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(64, kernelSize=1)(net, training=self.training) net = ConvBnLRelu(128, kernelSize=3)(net, training=self.training) net = UpSampling2D((2, 2))(net) net = ConvBnLRelu(64, kernelSize=3)(net, training=self.training) net = UpSampling2D((2, 2))(net) net = ConvBnLRelu(32, kernelSize=3)(net, training=self.training) net = ConvBnLRelu(64, kernelSize=1)(net, training=self.training) # net = ConvBnLRelu(3, kernelSize=1)(net, training=self.training) net = Conv2D(filters=self.inputShape[-1], kernel_size=(1, 1), padding='same', activation="tanh")(net) return Model(inLayer, net)
Example #27
Source File: models.py From BVAE-tf with The Unlicense | 4 votes |
def Build(self): # create the input layer for feeding the netowrk inLayer = Input(self.inputShape, self.batchSize) net = ConvBnLRelu(32, kernelSize=3)(inLayer, training=self.training) # 1 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(64, kernelSize=3)(net, training=self.training) # 2 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(128, kernelSize=3)(net, training=self.training) # 3 net = ConvBnLRelu(64, kernelSize=1)(net, training=self.training) # 4 net = ConvBnLRelu(128, kernelSize=3)(net, training=self.training) # 5 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(256, kernelSize=3)(net, training=self.training) # 6 net = ConvBnLRelu(128, kernelSize=1)(net, training=self.training) # 7 net = ConvBnLRelu(256, kernelSize=3)(net, training=self.training) # 8 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) # 9 net = ConvBnLRelu(256, kernelSize=1)(net, training=self.training) # 10 net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) # 11 net = ConvBnLRelu(256, kernelSize=1)(net, training=self.training) # 12 net = ConvBnLRelu(512, kernelSize=3)(net, training=self.training) # 13 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) # 14 net = ConvBnLRelu(512, kernelSize=1)(net, training=self.training) # 15 net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) # 16 net = ConvBnLRelu(512, kernelSize=1)(net, training=self.training) # 17 net = ConvBnLRelu(1024, kernelSize=3)(net, training=self.training) # 18 # variational encoder output (distributions) mean = Conv2D(filters=self.latentSize, kernel_size=(1, 1), padding='same')(net) mean = GlobalAveragePooling2D()(mean) logvar = Conv2D(filters=self.latentSize, kernel_size=(1, 1), padding='same')(net) logvar = GlobalAveragePooling2D()(logvar) sample = SampleLayer(self.latentConstraints, self.beta)([mean, logvar], training=self.training) return Model(inputs=inLayer, outputs=sample)
Example #28
Source File: backend.py From FATE with Apache License 2.0 | 4 votes |
def _build(self, lamda_u=0.0001, lamda_v=0.0001, optimizer='rmsprop', loss='mse', metrics='mse', initializer='uniform'): # init session on first time ref sess = self.session # user embedding user_InputLayer = Input(shape=(1,), dtype='int32', name='user_input') user_EmbeddingLayer = Embedding(input_dim=self.user_num, output_dim=self.embedding_dim, input_length=1, name='user_embedding', embeddings_regularizer=l2(lamda_u), embeddings_initializer=initializer)(user_InputLayer) user_EmbeddingLayer = Flatten(name='user_flatten')(user_EmbeddingLayer) # user bias user_BiasLayer = Embedding(input_dim=self.user_num, output_dim=1, input_length=1, name='user_bias', embeddings_regularizer=l2(lamda_u), embeddings_initializer=Zeros())(user_InputLayer) user_BiasLayer = Flatten(name='user_bias_flatten')(user_BiasLayer) # item embedding item_InputLayer = Input(shape=(1,), dtype='int32', name='item_input') item_EmbeddingLayer = Embedding(input_dim=self.item_num, output_dim=self.embedding_dim, input_length=1, name='item_embedding', embeddings_regularizer=l2(lamda_v), embeddings_initializer=initializer)(item_InputLayer) item_EmbeddingLayer = Flatten(name='item_flatten')(item_EmbeddingLayer) # item bias item_BiasLayer = Embedding(input_dim=self.item_num, output_dim=1, input_length=1, name='item_bias', embeddings_regularizer=l2(lamda_v), embeddings_initializer=Zeros())(item_InputLayer) item_BiasLayer = Flatten(name='item_bias_flatten')(item_BiasLayer) # rating prediction dotLayer = Dot(axes=-1, name='dot_layer')([user_EmbeddingLayer, item_EmbeddingLayer]) # add mu, user bias and item bias dotLayer = ConstantLayer(mu=self.mu)(dotLayer) dotLayer = Add()([dotLayer, user_BiasLayer]) dotLayer = Add()([dotLayer, item_BiasLayer]) # create model self._model = Model(inputs=[user_InputLayer, item_InputLayer], outputs=[dotLayer]) # compile model optimizer_instance = getattr(tf.keras.optimizers, optimizer.optimizer)(**optimizer.kwargs) losses = getattr(tf.keras.losses, loss) self._model.compile(optimizer=optimizer_instance, loss=losses, metrics=metrics) # pick user_embedding and user_bias for aggregating self._trainable_weights = {v.name.split("/")[0]: v for v in self._model.trainable_weights} LOGGER.debug(f"trainable weights {self._trainable_weights}") self._aggregate_weights = {"user_embedding": self._trainable_weights["user_embedding"], "user_bias": self._trainable_weights["user_bias"]}
Example #29
Source File: backend.py From FATE with Apache License 2.0 | 4 votes |
def build(self, lambda_u=0.0001, lambda_v=0.0001, optimizer='rmsprop', loss='mse', metrics='mse', initializer='uniform'): """ Init session and create model architecture. :param lambda_u: lambda value of l2 norm for user embeddings. :param lambda_v: lambda value of l2 norm for item embeddings. :param optimizer: optimizer type. :param loss: loss type. :param metrics: evaluation metrics. :param initializer: initializer of embedding :return: """ # init session on first time ref sess = self.session # user embedding user_input_layer = Input(shape=(1,), dtype='int32', name='user_input') user_embedding_layer = Embedding( input_dim=self.user_num, output_dim=self.embedding_dim, input_length=1, name='user_embedding', embeddings_regularizer=l2(lambda_u), embeddings_initializer=initializer)(user_input_layer) user_embedding_layer = Flatten(name='user_flatten')(user_embedding_layer) # item embedding item_input_layer = Input(shape=(1,), dtype='int32', name='item_input') item_embedding_layer = Embedding( input_dim=self.item_num, output_dim=self.embedding_dim, input_length=1, name='item_embedding', embeddings_regularizer=l2(lambda_v), embeddings_initializer=initializer)(item_input_layer) item_embedding_layer = Flatten(name='item_flatten')(item_embedding_layer) # rating prediction dot_layer = Dot(axes=-1, name='dot_layer')([user_embedding_layer, item_embedding_layer]) self._model = Model( inputs=[user_input_layer, item_input_layer], outputs=[dot_layer]) # compile model optimizer_instance = getattr( tf.keras.optimizers, optimizer.optimizer)(**optimizer.kwargs) losses = getattr(tf.keras.losses, loss) self._model.compile(optimizer=optimizer_instance, loss=losses, metrics=metrics) # pick user_embedding for aggregating self._trainable_weights = {v.name.split( "/")[0]: v for v in self._model.trainable_weights} self._aggregate_weights = { "user_embedding": self._trainable_weights["user_embedding"]}
Example #30
Source File: fibinet.py From DeepCTR with Apache License 2.0 | 4 votes |
def FiBiNET(linear_feature_columns, dnn_feature_columns, bilinear_type='interaction', reduction_ratio=3, dnn_hidden_units=(128, 128), l2_reg_linear=1e-5, l2_reg_embedding=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_activation='relu', task='binary'): """Instantiates the Feature Importance and Bilinear feature Interaction NETwork architecture. :param linear_feature_columns: An iterable containing all the features used by linear part of the model. :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. :param bilinear_type: str,bilinear function type used in Bilinear Interaction Layer,can be ``'all'`` , ``'each'`` or ``'interaction'`` :param reduction_ratio: integer in [1,inf), reduction ratio used in SENET Layer :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN :param l2_reg_linear: float. L2 regularizer strength applied to wide part :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector :param l2_reg_dnn: float. L2 regularizer strength applied to DNN :param seed: integer ,to use as random seed. :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate. :param dnn_activation: Activation function to use in DNN :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss :return: A Keras model instance. """ features = build_input_features(linear_feature_columns + dnn_feature_columns) inputs_list = list(features.values()) linear_logit = get_linear_logit(features, linear_feature_columns, seed=seed, prefix='linear', l2_reg=l2_reg_linear) sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns, l2_reg_embedding, seed) senet_embedding_list = SENETLayer( reduction_ratio, seed)(sparse_embedding_list) senet_bilinear_out = BilinearInteraction( bilinear_type=bilinear_type, seed=seed)(senet_embedding_list) bilinear_out = BilinearInteraction( bilinear_type=bilinear_type, seed=seed)(sparse_embedding_list) dnn_input = combined_dnn_input( [Flatten()(concat_func([senet_bilinear_out, bilinear_out]))], dense_value_list) dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed)(dnn_input) dnn_logit = Dense( 1, use_bias=False, activation=None)(dnn_out) final_logit = add_func([linear_logit, dnn_logit]) output = PredictionLayer(task)(final_logit) model = Model(inputs=inputs_list, outputs=output) return model