我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.models.Model()。
def start_train(self): """ Starts to Train the entire Model Based on set Parameters """ # 1, Prep callback = [EarlyStopping(patience=self.Patience), ReduceLROnPlateau(patience=5, verbose=1), CSVLogger(filename=self.rnn_type+'log.csv'), ModelCheckpoint(self.rnn_type + '_' + self.dataset + '.check', save_best_only=True, save_weights_only=True)] # 2, Train self.model.fit(x = [self.train[0],self.train[1]], y = self.train[2], batch_size = self.BatchSize, epochs = self.MaxEpoch, validation_data=([self.test[0], self.test[1]], self.test[2]), callbacks = callback) # 3, Evaluate self.model.load_weights(self.rnn_type + '_' + self.dataset + '.check') # revert to the best model self.evaluate_on_test()
def create_lstm_autoencoder(input_dim, timesteps, latent_dim): """ Creates an LSTM Autoencoder (VAE). Returns Autoencoder, Encoder, Generator. (All code by fchollet - see reference.) # Arguments input_dim: int. timesteps: int, input timestep dimension. latent_dim: int, latent z-layer shape. # References - [Building Autoencoders in Keras](https://blog.keras.io/building-autoencoders-in-keras.html) """ inputs = Input(shape=(timesteps, input_dim,)) encoded = LSTM(latent_dim)(inputs) decoded = RepeatVector(timesteps)(encoded) decoded = LSTM(input_dim, return_sequences=True)(decoded) sequence_autoencoder = Model(inputs, decoded) encoder = Model(inputs, encoded) autoencoder = Model(inputs, decoded) autoencoder.compile(optimizer='adam', loss='mse') return autoencoder
def build_network(deepest=False): dropout = [0., 0.1, 0.2, 0.3, 0.4] conv = [(64, 3, 3), (128, 3, 3), (256, 3, 3), (512, 3, 3), (512, 2, 2)] input= Input(shape=(3, 32, 32)) output = fractal_net( c=3, b=5, conv=conv, drop_path=0.15, dropout=dropout, deepest=deepest)(input) output = Flatten()(output) output = Dense(NB_CLASSES, init='he_normal')(output) output = Activation('softmax')(output) model = Model(input=input, output=output) optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM) #optimizer = RMSprop(lr=LEARN_START) #optimizer = Adam() #optimizer = Nadam() model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) plot(model, to_file='model.png') return model
def create_Kao_Onet( weight_path = 'model48.h5'): input = Input(shape = [48,48,3]) x = Conv2D(32, (3, 3), strides=1, padding='valid', name='conv1')(input) x = PReLU(shared_axes=[1,2],name='prelu1')(x) x = MaxPool2D(pool_size=3, strides=2, padding='same')(x) x = Conv2D(64, (3, 3), strides=1, padding='valid', name='conv2')(x) x = PReLU(shared_axes=[1,2],name='prelu2')(x) x = MaxPool2D(pool_size=3, strides=2)(x) x = Conv2D(64, (3, 3), strides=1, padding='valid', name='conv3')(x) x = PReLU(shared_axes=[1,2],name='prelu3')(x) x = MaxPool2D(pool_size=2)(x) x = Conv2D(128, (2, 2), strides=1, padding='valid', name='conv4')(x) x = PReLU(shared_axes=[1,2],name='prelu4')(x) x = Permute((3,2,1))(x) x = Flatten()(x) x = Dense(256, name='conv5') (x) x = PReLU(name='prelu5')(x) classifier = Dense(2, activation='softmax',name='conv6-1')(x) bbox_regress = Dense(4,name='conv6-2')(x) landmark_regress = Dense(10,name='conv6-3')(x) model = Model([input], [classifier, bbox_regress, landmark_regress]) model.load_weights(weight_path, by_name=True) return model
def cnn_word_model(self): embed_input = Input(shape=(self.opt['max_sequence_length'], self.opt['embedding_dim'],)) outputs = [] for i in range(len(self.kernel_sizes)): output_i = Conv1D(self.opt['filters_cnn'], kernel_size=self.kernel_sizes[i], activation=None, kernel_regularizer=l2(self.opt['regul_coef_conv']), padding='same')(embed_input) output_i = BatchNormalization()(output_i) output_i = Activation('relu')(output_i) output_i = GlobalMaxPooling1D()(output_i) outputs.append(output_i) output = concatenate(outputs, axis=1) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(self.opt['dense_dim'], activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) output = Activation('relu')(output) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(1, activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) act_output = Activation('sigmoid')(output) model = Model(inputs=embed_input, outputs=act_output) return model
def get_model(): inputs = Input(shape=(64, 64, 3)) conv_1 = Conv2D(1, (3, 3), strides=(1, 1), padding='same')(inputs) act_1 = Activation('relu')(conv_1) conv_2 = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(act_1) act_2 = Activation('relu')(conv_2) deconv_1 = Conv2DTranspose(64, (3, 3), strides=(1, 1), padding='same')(act_2) act_3 = Activation('relu')(deconv_1) merge_1 = concatenate([act_3, act_1], axis=3) deconv_2 = Conv2DTranspose(1, (3, 3), strides=(1, 1), padding='same')(merge_1) act_4 = Activation('relu')(deconv_2) model = Model(inputs=[inputs], outputs=[act_4]) model.compile(optimizer='adadelta', loss=dice_coef_loss, metrics=[dice_coef]) return model
def define_attention_model(self): # Take necessary parts out of the entailment model to get OntoLSTM attention. if not self.model: raise RuntimeError, "Model not trained yet!" # We need just one input to get attention. input_shape_at(0) gives a list with two shapes. input_shape = self.model.get_input_shape_at(0)[0] input_layer = Input(input_shape[1:], dtype='int32') # removing batch size embedding_layer = None encoder_layer = None for layer in self.model.layers: if layer.name == "embedding": embedding_layer = layer elif layer.name == "encoder": # We need to redefine the OntoLSTM layer with the learned weights and set return attention to True. # Assuming we'll want attention values for all words (return_sequences = True) encoder_layer = OntoAttentionLSTM(input_dim=self.embed_dim, output_dim=self.embed_dim, num_senses=self.num_senses, num_hyps=self.num_hyps, use_attention=True, return_attention=True, return_sequences=True, weights=layer.get_weights()) if not embedding_layer or not encoder_layer: raise RuntimeError, "Required layers not found!" attention_output = encoder_layer(embedding_layer(input_layer)) self.attention_model = Model(input=input_layer, output=attention_output) self.attention_model.compile(loss="mse", optimizer="sgd") # Loss and optimizer do not matter!
def get_attention(self, C_ind): if not self.model: raise RuntimeError, "Model not trained!" model_embedding = None model_weights = None for layer in self.model.layers: if layer.name.lower() == "embedding": model_embedding = layer if layer.name.lower() == "sent_lstm": model_lstm = layer if model_embedding is None or model_lstm is None: raise RuntimeError, "Did not find expected layers" lstm_weights = model_lstm.get_weights() embedding_weights = model_embedding.get_weights() embed_in_dim, embed_out_dim = embedding_weights[0].shape att_embedding = HigherOrderEmbedding(input_dim=embed_in_dim, output_dim=embed_out_dim, weights=embedding_weights) onto_lstm = OntoAttentionLSTM(input_dim=embed_out_dim, output_dim=embed_out_dim, input_length=model_lstm.input_length, num_senses=self.num_senses, num_hyps=self.num_hyps, use_attention=True, return_attention=True, weights=lstm_weights) att_input = Input(shape=C_ind.shape[1:], dtype='int32') att_sent_rep = att_embedding(att_input) att_output = onto_lstm(att_sent_rep) att_model = Model(input=att_input, output=att_output) att_model.compile(optimizer='adam', loss='mse') # optimizer and loss are not needed since we are not going to train this model. C_att = att_model.predict(C_ind) print >>sys.stderr, "Got attention values. Input, output shapes:", C_ind.shape, C_att.shape return C_att
def build_network(deepest=False): dropout = [0., 0.1, 0.2, 0.3, 0.4] conv = [(64, 3, 3), (128, 3, 3), (256, 3, 3), (512, 3, 3), (512, 2, 2)] input= Input(shape=(3, 32, 32) if K._BACKEND == 'theano' else (32, 32,3)) output = fractal_net( c=3, b=5, conv=conv, drop_path=0.15, dropout=dropout, deepest=deepest)(input) output = Flatten()(output) output = Dense(NB_CLASSES, init='he_normal')(output) output = Activation('softmax')(output) model = Model(input=input, output=output) #optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM) #optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM, nesterov=True) optimizer = Adam() #optimizer = Nadam() model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) plot(model, to_file='model.png', show_shapes=True) return model
def get_unet0(num_start_filters=32): inputs = Input((img_rows, img_cols, num_channels)) conv1 = ConvBN2(inputs, num_start_filters) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = ConvBN2(pool1, 2 * num_start_filters) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = ConvBN2(pool2, 4 * num_start_filters) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = ConvBN2(pool3, 8 * num_start_filters) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = ConvBN2(pool4, 16 * num_start_filters) up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4]) conv6 = ConvBN2(up6, 8 * num_start_filters) up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3]) conv7 = ConvBN2(up7, 4 * num_start_filters) up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2]) conv8 = ConvBN2(up8, 2 * num_start_filters) up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1]) conv9 = Conv2D(num_start_filters, (3, 3), padding="same", kernel_initializer="he_uniform")(up9) conv9 = BatchNormalization()(conv9) conv9 = Activation('selu')(conv9) conv9 = Conv2D(num_start_filters, (3, 3), padding="same", kernel_initializer="he_uniform")(conv9) crop9 = Cropping2D(cropping=((16, 16), (16, 16)))(conv9) conv9 = BatchNormalization()(crop9) conv9 = Activation('selu')(conv9) conv10 = Conv2D(num_mask_channels, (1, 1))(conv9) model = Model(inputs=inputs, outputs=conv10) return model
def build_simpleCNN(input_shape = (32, 32, 3), num_output = 10): h, w, nch = input_shape assert h == w, 'expect input shape (h, w, nch), h == w' images = Input(shape = (h, h, nch)) x = Conv2D(64, (4, 4), strides = (1, 1), kernel_initializer = init, padding = 'same')(images) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size = (2, 2))(x) x = Conv2D(128, (4, 4), strides = (1, 1), kernel_initializer = init, padding = 'same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size = (2, 2))(x) x = Flatten()(x) outputs = Dense(num_output, kernel_initializer = init, activation = 'softmax')(x) model = Model(inputs = images, outputs = outputs) return model
def model_generator(): nch = 256 g_input = Input(shape=[100]) H = Dense(nch * 14 * 14)(g_input) H = BatchNormalization(mode=2)(H) H = Activation('relu')(H) H = dim_ordering_reshape(nch, 14)(H) H = UpSampling2D(size=(2, 2))(H) H = Convolution2D(int(nch / 2), 3, 3, border_mode='same')(H) H = BatchNormalization(mode=2, axis=1)(H) H = Activation('relu')(H) H = Convolution2D(int(nch / 4), 3, 3, border_mode='same')(H) H = BatchNormalization(mode=2, axis=1)(H) H = Activation('relu')(H) H = Convolution2D(1, 1, 1, border_mode='same')(H) g_V = Activation('sigmoid')(H) return Model(g_input, g_V)
def model_discriminator(input_shape=(1, 28, 28), dropout_rate=0.5): d_input = dim_ordering_input(input_shape, name="input_x") nch = 512 # nch = 128 H = Convolution2D(int(nch / 2), 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(d_input) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Convolution2D(nch, 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Flatten()(H) H = Dense(int(nch / 2))(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) d_V = Dense(1, activation='sigmoid')(H) return Model(d_input, d_V)
def create_model(self, rnn_layer): inputs = Input(shape=(self.max_length, self.feature_size)) masked_inputs = Masking(0.0)(inputs) outputs = RNNEncoder( RNNCell( rnn_layer( self.hidden_size ), Dense( self.encoding_size ), dense_dropout=0.1 ) )(masked_inputs) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self, rnn_layer): inputs = Input(shape=(self.max_length, self.feature_size)) masked_inputs = Masking(0.0)(inputs) outputs = RNNCell( recurrent_layer=rnn_layer( self.hidden_size, return_sequences=True ), dense_layer=Dense( units=self.encoding_size ), dense_dropout=0.1 )(masked_inputs) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self, rnn_layer): inputs = Input(shape=(self.max_length, self.feature_size)) masked_inputs = Masking(0.0)(inputs) encoded = RNNEncoder( rnn_layer( self.encoding_size, ) )(masked_inputs) outputs = RNNDecoder( rnn_layer( self.feature_size, ) )(encoded) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self, rnn_layer): inputs = Input(shape=(self.max_length, self.feature_size)) masked_inputs = Masking(0.0)(inputs) outputs = BidirectionalRNNEncoder( RNNCell( rnn_layer( self.hidden_units, ), Dense( self.cell_units ), dense_dropout=0.1 ) )(masked_inputs) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self): inputs = Input(shape=(self.x, self.y, self.channel_size)) masked_inputs = MaskConv(self.mask_value)(inputs) masked_seq = MaskToSeq(MaskConv(self.mask_value))(inputs) conv_outputs = MaskConvNet( Conv2D( self.filters, self.kernel, strides=self.strides, ) )(masked_inputs) pooling_outputs = MaskPooling( MaxPool2D( self.mask_kernel, self.mask_strides, self.padding, ) )(conv_outputs) outputs = ConvEncoder()( [pooling_outputs, masked_seq] ) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self): inputs = Input(shape=(self.x, self.y, self.channel_size)) masked_inputs = MaskConv(self.mask_value)(inputs) conv_outputs = MaskConvNet( Conv2D( self.filters, self.kernel, strides=self.strides ) )(masked_inputs) pooling_outputs = MaskPooling( MaxPool2D( self.mask_kernel, self.mask_strides, self.padding, ) )(conv_outputs) outputs = MaskFlatten()(pooling_outputs) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def create_model(self): inputs = Input( shape=( self.max_length, self.feature_size ) ) encoded_seq = RNNCell( LSTMPeephole( self.hidden_size, return_sequences=False ), Dense( self.encoding_size ) )(inputs) outputs = PaddingZero( self.max_length )(encoded_seq) model = Model(inputs, outputs) model.compile('sgd', 'mean_squared_error') return model
def __init__(self, annotation ='biGRU', dataset = 'snli'): # 1, Set Basic Model Parameters self.Layers = 1 self.EmbeddingSize = 300 self.BatchSize = 512 self.Patience = 8 self.MaxEpoch = 42 self.SentMaxLen = 42 self.DropProb = 0.4 self.L2Strength = 1e-5 self.Activate = 'relu' self.Optimizer = 'rmsprop' self.rnn_type = annotation self.dataset = dataset # 2, Define Class Variables self.Vocab = 0 self.model = None self.GloVe = defaultdict(np.array) self.indexer,self.Embed = None, None self.train, self.validation, self.test = [],[],[] self.Labels = {'contradiction': 0, 'neutral': 1, 'entailment': 2} self.rLabels = {0:'contradiction', 1:'neutral', 2:'entailment'}
def create_Kao_Rnet (weight_path = 'model24.h5'): input = Input(shape=[24, 24, 3]) # change this shape to [None,None,3] to enable arbitraty shape input x = Conv2D(28, (3, 3), strides=1, padding='valid', name='conv1')(input) x = PReLU(shared_axes=[1, 2], name='prelu1')(x) x = MaxPool2D(pool_size=3,strides=2, padding='same')(x) x = Conv2D(48, (3, 3), strides=1, padding='valid', name='conv2')(x) x = PReLU(shared_axes=[1, 2], name='prelu2')(x) x = MaxPool2D(pool_size=3, strides=2)(x) x = Conv2D(64, (2, 2), strides=1, padding='valid', name='conv3')(x) x = PReLU(shared_axes=[1, 2], name='prelu3')(x) x = Permute((3, 2, 1))(x) x = Flatten()(x) x = Dense(128, name='conv4')(x) x = PReLU( name='prelu4')(x) classifier = Dense(2, activation='softmax', name='conv5-1')(x) bbox_regress = Dense(4, name='conv5-2')(x) model = Model([input], [classifier, bbox_regress]) model.load_weights(weight_path, by_name=True) return model
def HAN1(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): #model = Sequential() wordInputs = Input(shape=(MAX_WORDS,), name='word1', dtype='float32') wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=True, trainable=True, name='emb1')(wordInputs) #Assuming all the sentences have same number of words. Check for input_length again. hij = Bidirectional(GRU(WORDGRU, name='gru1', return_sequences=True))(wordEmbedding) wordDrop = Dropout(DROPOUTPER, name='drop1')(hij) alpha_its, Si = AttentionLayer(name='att1')(wordDrop) v6 = Dense(1, activation="sigmoid", name="dense")(Si) #model.add(Dense(1, activation="sigmoid", name="documentOut3")) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def fGRU_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_SENTS+1, MAX_WORDS), name="wordInputs", dtype='float32') wordInp = Flatten()(wordInputs) wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInp) hij = Bidirectional(GRU(WORDGRU, return_sequences=True), name='gru1')(wordEmbedding) head = GlobalAveragePooling1D()(hij) v6 = Dense(1, activation="sigmoid", name="dense")(head) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def fGlove_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_SENTS+1, MAX_WORDS), name="wordInputs", dtype='float32') wordInp = Flatten()(wordInputs) wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInp) head = GlobalAveragePooling1D()(wordEmbedding) v6 = Dense(1, activation="sigmoid", name="dense")(head) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def lstm_word_model(self): embed_input = Input(shape=(self.opt['max_sequence_length'], self.opt['embedding_dim'],)) output = Bidirectional(LSTM(self.opt['units_lstm'], activation='tanh', kernel_regularizer=l2(self.opt['regul_coef_lstm']), dropout=self.opt['dropout_rate']))(embed_input) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(self.opt['dense_dim'], activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) output = Activation('relu')(output) output = Dropout(rate=self.opt['dropout_rate'])(output) output = Dense(1, activation=None, kernel_regularizer=l2(self.opt['regul_coef_dense']))(output) output = BatchNormalization()(output) act_output = Activation('sigmoid')(output) model = Model(inputs=embed_input, outputs=act_output) return model
def create_lstm_layer(self, input_dim): """Create a LSTM layer of a model.""" inp = Input(shape=(input_dim, self.embedding_dim,)) inp_dropout = Dropout(self.ldrop_val)(inp) ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) outp = LSTM(self.hidden_dim, input_shape=(input_dim, self.embedding_dim,), kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, recurrent_dropout=self.recdrop_val, dropout=self.inpdrop_val, kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=True)(inp_dropout) outp_dropout = Dropout(self.dropout_val)(outp) model = Model(inputs=inp, outputs=outp_dropout, name="LSTM_encoder") return model
def create_lstm_layer_1(self, input_dim): """Create a LSTM layer of a model.""" inp = Input(shape=(input_dim, self.embedding_dim,)) inp_drop = Dropout(self.ldrop_val)(inp) ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) bioutp = Bidirectional(LSTM(self.hidden_dim, input_shape=(input_dim, self.embedding_dim,), kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, recurrent_dropout=self.recdrop_val, dropout=self.inpdrop_val, kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=True), merge_mode=None)(inp_drop) dropout_forw = Dropout(self.dropout_val)(bioutp[0]) dropout_back = Dropout(self.dropout_val)(bioutp[1]) model = Model(inputs=inp, outputs=[dropout_forw, dropout_back], name="biLSTM_encoder") return model
def create_lstm_layer_2(self, input_dim): """Create a LSTM layer of a model.""" inp = Input(shape=(input_dim, 2*self.perspective_num,)) inp_drop = Dropout(self.ldrop_val)(inp) ker_in = glorot_uniform(seed=self.seed) rec_in = Orthogonal(seed=self.seed) bioutp = Bidirectional(LSTM(self.aggregation_dim, input_shape=(input_dim, 2*self.perspective_num,), kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, recurrent_dropout=self.recdrop_val, dropout=self.inpdrop_val, kernel_initializer=ker_in, recurrent_initializer=rec_in, return_sequences=True), merge_mode=None)(inp_drop) dropout_forw = Dropout(self.dropout_val)(bioutp[0]) dropout_back = Dropout(self.dropout_val)(bioutp[1]) model = Model(inputs=inp, outputs=[dropout_forw, dropout_back], name="biLSTM_enc_persp") return model
def create_attention_layer(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.zeros((self.max_sequence_length-1,1)), np.ones((1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="attention_generator") return model
def create_attention_layer_f(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.zeros((self.max_sequence_length-1,1)), np.ones((1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="att_generator_forw") return model
def create_attention_layer_b(self, input_dim_a, input_dim_b): """Create an attention layer of a model.""" inp_a = Input(shape=(input_dim_a, self.hidden_dim,)) inp_b = Input(shape=(input_dim_b, self.hidden_dim,)) val = np.concatenate((np.ones((1,1)), np.zeros((self.max_sequence_length-1,1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(inp_b) last_state = Lambda(lambda x: K.permute_dimensions(K.dot(x, kcon), (0,2,1)))(inp_b_perm) ker_in = glorot_uniform(seed=self.seed) outp_a = Dense(self.attention_dim, input_shape=(input_dim_a, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(inp_a) outp_last = Dense(self.attention_dim, input_shape=(1, self.hidden_dim), kernel_initializer=ker_in, activation='relu')(last_state) outp_last_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))([outp_last_perm, outp_a]) outp_norm = Activation('softmax')(outp) outp_norm_perm = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)))(outp_norm) model = Model(inputs=[inp_a, inp_b], outputs=outp_norm_perm, name="att_generator_back") return model
def bilstm_woatt_model(self): """Define a model with bi-LSTM layers and without attention.""" input_a = Input(shape=(self.max_sequence_length, self.embedding_dim,)) input_b = Input(shape=(self.max_sequence_length, self.embedding_dim,)) lstm_layer = self.create_lstm_layer_last(self.max_sequence_length) lstm_last_a = lstm_layer(input_a) lstm_last_b = lstm_layer(input_b) dist = Lambda(self.cosine_dist, output_shape=self.cosine_dist_output_shape, name="similarity_network")([lstm_last_a, lstm_last_b]) dense = Dense(1, activation='sigmoid', name='similarity_score', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None)(dist) model = Model([input_a, input_b], dense) return model
def build_mod5(opt=adam()): n = 3 * 1024 in1 = Input((128,), name='x1') x1 = fc_block1(in1, n) x1 = fc_identity(x1, n) in2 = Input((1024,), name='x2') x2 = fc_block1(in2, n) x2 = fc_identity(x2, n) x = merge([x1, x2], mode='concat', concat_axis=1) x = fc_identity(x, n) out = Dense(4716, activation='sigmoid', name='output')(x) model = Model(input=[in1, in2], output=out) model.compile(optimizer=opt, loss='categorical_crossentropy') # model.summary() # plot(model=model, show_shapes=True) return model
def build_mod6(opt=adam()): in1 = Input((128,), name='x1') in2 = Input((1024,), name='x2') inp = merge([in1, in2], mode='concat', concat_axis=1) wide = Dense(4000)(inp) deep = Dense(1000, activation='sigmoid')(inp) deep = Dense(1000, activation='sigmoid')(deep) deep = Dense(4000)(deep) out = merge([wide, deep], mode='sum', concat_axis=1) out = Dense(4716, activation='sigmoid', name='output')(out) model = Model(input=[in1, in2], output=out) model.compile(optimizer=opt, loss='categorical_crossentropy') # model.summary() plot(model=model, show_shapes=True) return model
def build(self, vocabs=None): if self._keras_model: return if vocabs is None and self._word_vector_init is not None: raise ValueError('If word_vector_init is not None, build method ' 'must be called with vocabs that are not None!') image_input, image_embedding = self._build_image_embedding() sentence_input, word_embedding = self._build_word_embedding(vocabs) sequence_input = Concatenate(axis=1)([image_embedding, word_embedding]) sequence_output = self._build_sequence_model(sequence_input) model = Model(inputs=[image_input, sentence_input], outputs=sequence_output) model.compile(optimizer=Adam(lr=self._learning_rate, clipnorm=5.0), loss=categorical_crossentropy_from_logits, metrics=[categorical_accuracy_with_variable_timestep]) self._keras_model = model
def to_configs(states, verbose=True, **kwargs): base = setting['base'] width = states.shape[1] // base height = states.shape[1] // base load(width,height) def build(): P = len(setting['panels']) states = Input(shape=(height*base,width*base)) error = build_error(states, height, width, base) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], height * width, -1]) # a, pos, panel matches = K.permute_dimensions(matches, [0,2,1]) # a, panel, pos config = matches * K.arange(height*width,dtype='float') config = K.sum(config, axis=-1) return Model(states, wrap(states, config)) model = build() return model.predict(states, **kwargs)
def generate_gpu(configs,**kwargs): configs = np.array(configs) import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def build(): P = 2 configs = Input(shape=(size*size,)) _configs = 1 - K.round((configs/2)+0.5) # from -1/1 to 1/0 configs_one_hot = K.one_hot(K.cast(_configs,'int32'), P) configs_one_hot = K.reshape(configs_one_hot, [-1,P]) _panels = K.variable(panels) _panels = K.reshape(_panels, [P, base*base]) states = tf.matmul(configs_one_hot, _panels) states = K.reshape(states, [-1, size, size, base, base]) states = K.permute_dimensions(states, [0, 1, 3, 2, 4]) states = K.reshape(states, [-1, size*base, size*base, 1]) states = K.spatial_2d_padding(states, padding=((pad,pad),(pad,pad))) states = K.squeeze(states, -1) return Model(configs, wrap(configs, states)) return preprocess(batch_swirl(build().predict(configs,**kwargs)))
def generate_gpu2(configs,**kwargs): configs = np.array(configs) import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def build(): P = 2 configs = Input(shape=(size*size,)) _configs = 1 - K.round((configs/2)+0.5) # from -1/1 to 1/0 configs_one_hot = K.one_hot(K.cast(_configs,'int32'), P) configs_one_hot = K.reshape(configs_one_hot, [-1,P]) _panels = K.variable(panels) _panels = K.reshape(_panels, [P, base*base]) states = tf.matmul(configs_one_hot, _panels) states = K.reshape(states, [-1, size, size, base, base]) states = K.permute_dimensions(states, [0, 1, 3, 2, 4]) states = K.reshape(states, [-1, size*base, size*base, 1]) states = K.spatial_2d_padding(states, padding=((pad,pad),(pad,pad))) states = K.squeeze(states, -1) states = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **swirl_args) return Model(configs, wrap(configs, states)) return preprocess(build().predict(configs,**kwargs))
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] dim = states.shape[1] - pad*2 size = dim // base def build(): states = Input(shape=(dim+2*pad,dim+2*pad)) s = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **unswirl_args) error = build_errors(s,base,pad,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) return build().predict(states, **kwargs)
def generate_gpu(configs, **kwargs): import math size = int(math.sqrt(len(configs[0]))) base = panels.shape[1] dim = base*size def build(): P = 2 configs = Input(shape=(size*size,)) _configs = 1 - K.round((configs/2)+0.5) # from -1/1 to 1/0 configs_one_hot = K.one_hot(K.cast(_configs,'int32'), P) configs_one_hot = K.reshape(configs_one_hot, [-1,P]) _panels = K.variable(panels) _panels = K.reshape(_panels, [P, base*base]) states = tf.matmul(configs_one_hot, _panels) states = K.reshape(states, [-1, size, size, base, base]) states = K.permute_dimensions(states, [0, 1, 3, 2, 4]) states = K.reshape(states, [-1, size*base, size*base]) return Model(configs, wrap(configs, states)) return build().predict(np.array(configs),**kwargs)
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] size = states.shape[1]//base dim = states.shape[1] def build(): states = Input(shape=(dim,dim)) error = build_errors(states,base,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) model = build() return model.predict(states, **kwargs)
def _build(self,input_shape): _encoder = self.build_encoder(input_shape) _decoder = self.build_decoder(input_shape) x = Input(shape=input_shape) z = Sequential([flatten, *_encoder])(x) y = Sequential(_decoder)(flatten(z)) z2 = Input(shape=K.int_shape(z)[1:]) y2 = Sequential(_decoder)(flatten(z2)) self.loss = bce self.encoder = Model(x, z) self.decoder = Model(z2, y2) self.net = Model(x, y) self.autoencoder = self.net
def create_actor_network(self, state_size, action_dim): """Create actor network.""" print ("[MESSAGE] Build actor network.""") S = Input(shape=state_size) h_0 = Conv2D(32, (3, 3), padding="same", kernel_regularizer=l2(0.0001), activation="relu")(S) h_1 = Conv2D(32, (3, 3), padding="same", kernel_regularizer=l2(0.0001), activation="relu")(h_0) h_1 = AveragePooling2D(2, 2)(h_1) h_1 = Flatten()(h_1) h_1 = Dense(600, activation="relu")(h_1) A = Dense(action_dim, activation="softmax")(h_1) model = Model(inputs=S, outputs=A) return model, model.trainable_weights, S
def main(): args = get_arguments() model = MoleculeVAE() data, data_test, charset = load_dataset(args.data) if os.path.isfile(args.model): model.load(charset, args.model, latent_rep_size = args.latent_dim) else: raise ValueError("Model file %s doesn't exist" % args.model) x_latent = model.encoder.predict(data) if not args.visualize: if not args.save_h5: np.savetxt(sys.stdout, x_latent, delimiter = '\t') else: h5f = h5py.File(args.save_h5, 'w') h5f.create_dataset('charset', data = charset) h5f.create_dataset('latent_vectors', data = x_latent) h5f.close() else: visualize_latent_rep(args, model, x_latent)
def build(nc, w, h, loss='categorical_crossentropy', optimizer='adam', **kwargs): data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None? inp = Input(shape=(h, w, 3)) enet = encoder.build(inp) enet = decoder.build(enet, nc=nc) name = 'enet_naive_upsampling' enet = Reshape((data_shape, nc))(enet) # TODO: need to remove data_shape for multi-scale training enet = Activation('softmax')(enet) model = Model(inputs=inp, outputs=enet) model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy', 'mean_squared_error']) return model, name
def build(nc, w, h, loss='categorical_crossentropy', # optimizer='adadelta'): optimizer='adam', metrics=None, **kwargs): data_shape = w * h if None not in (w, h) else -1 # TODO: -1 or None? inp = Input(shape=(h, w, 3), name='image') enet = encoder.build(inp) enet = decoder.build(enet, nc=nc) name = 'enet_unpooling' # TODO: need to remove data_shape for multi-scale training enet = Reshape((data_shape, nc))(enet) enet = Activation('softmax', name='output')(enet) model = Model(inputs=inp, outputs=enet) if metrics is None: metrics = ['accuracy'] model.compile(optimizer=optimizer, loss=loss, metrics=metrics) return model, name
def save_model(model): if not os.path.exists('Data/Model/'): os.makedirs('Data/Model/') model_json = model.to_json() with open("Data/Model/model.json", "w") as model_file: model_file.write(model_json) # serialize weights to HDF5 model.save_weights("Data/Model/weights.h5") print('Model and weights saved') return
def model_cnn(net_layers, input_shape): inp = Input(shape=input_shape) model = inp for cl in net_layers['conv_layers']: model = Conv2D(filters=cl[0], kernel_size=cl[1], activation='relu')(model) if cl[4]: model = MaxPooling2D()(model) if cl[2]: model = BatchNormalization()(model) if cl[3]: model = Dropout(0.2)(model) model = Flatten()(model) for dl in net_layers['dense_layers']: model = Dense(dl[0])(model) model = Activation('relu')(model) if dl[1]: model = BatchNormalization()(model) if dl[2]: model = Dropout(0.2)(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) return model # %% # LSTM architecture # conv_layers -> [(filters, kernel_size, BatchNormaliztion, Dropout, MaxPooling)] # dense_layers -> [(num_neurons, BatchNormaliztion, Dropout)]
def model_lstm(input_shape): inp = Input(shape=input_shape) model = inp if input_shape[0] > 2: model = Conv1D(filters=24, kernel_size=(3), activation='relu')(model) # if input_shape[0] > 0: model = TimeDistributed(Conv1D(filters=24, kernel_size=3, activation='relu'))(model) model = LSTM(16)(model) model = Activation('relu')(model) model = Dropout(0.2)(model) model = Dense(16)(model) model = Activation('relu')(model) model = BatchNormalization()(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) return model # %% # Conv-1D architecture. Just one sample as input