Python keras.layers.Dense() Examples
The following are 30 code examples for showing how to use keras.layers.Dense(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
keras.layers
, or try the search function
.
Example 1
Project: keras-anomaly-detection Author: chen0040 File: recurrent.py License: MIT License | 10 votes |
def create_model(time_window_size, metric): model = Sequential() model.add(Conv1D(filters=256, kernel_size=5, padding='same', activation='relu', input_shape=(time_window_size, 1))) model.add(MaxPooling1D(pool_size=4)) model.add(LSTM(64)) model.add(Dense(units=time_window_size, activation='linear')) model.compile(optimizer='adam', loss='mean_squared_error', metrics=[metric]) # model.compile(optimizer='adam', loss='mean_squared_error', metrics=[metric]) # model.compile(optimizer="sgd", loss="mse", metrics=[metric]) print(model.summary()) return model
Example 2
Project: Image-Caption-Generator Author: dabasajay File: model.py License: MIT License | 8 votes |
def RNNModel(vocab_size, max_len, rnnConfig, model_type): embedding_size = rnnConfig['embedding_size'] if model_type == 'inceptionv3': # InceptionV3 outputs a 2048 dimensional vector for each image, which we'll feed to RNN Model image_input = Input(shape=(2048,)) elif model_type == 'vgg16': # VGG16 outputs a 4096 dimensional vector for each image, which we'll feed to RNN Model image_input = Input(shape=(4096,)) image_model_1 = Dropout(rnnConfig['dropout'])(image_input) image_model = Dense(embedding_size, activation='relu')(image_model_1) caption_input = Input(shape=(max_len,)) # mask_zero: We zero pad inputs to the same length, the zero mask ignores those inputs. E.g. it is an efficiency. caption_model_1 = Embedding(vocab_size, embedding_size, mask_zero=True)(caption_input) caption_model_2 = Dropout(rnnConfig['dropout'])(caption_model_1) caption_model = LSTM(rnnConfig['LSTM_units'])(caption_model_2) # Merging the models and creating a softmax classifier final_model_1 = concatenate([image_model, caption_model]) final_model_2 = Dense(rnnConfig['dense_units'], activation='relu')(final_model_1) final_model = Dense(vocab_size, activation='softmax')(final_model_2) model = Model(inputs=[image_input, caption_input], outputs=final_model) model.compile(loss='categorical_crossentropy', optimizer='adam') return model
Example 3
Project: vergeml Author: mme File: imagenet.py License: MIT License | 7 votes |
def _makenet(x, num_layers, dropout, random_seed): from keras.layers import Dense, Dropout dropout_seeder = random.Random(random_seed) for i in range(num_layers - 1): # add intermediate layers if dropout: x = Dropout(dropout, seed=dropout_seeder.randint(0, 10000))(x) x = Dense(1024, activation="relu", name='dense_layer_{}'.format(i))(x) if dropout: # add the final dropout layer x = Dropout(dropout, seed=dropout_seeder.randint(0, 10000))(x) return x
Example 4
Project: keras-anomaly-detection Author: chen0040 File: feedforward.py License: MIT License | 6 votes |
def create_model(self, input_dim): encoding_dim = 14 input_layer = Input(shape=(input_dim,)) encoder = Dense(encoding_dim, activation="tanh", activity_regularizer=regularizers.l1(10e-5))(input_layer) encoder = Dense(encoding_dim // 2, activation="relu")(encoder) decoder = Dense(encoding_dim // 2, activation='tanh')(encoder) decoder = Dense(input_dim, activation='relu')(decoder) model = Model(inputs=input_layer, outputs=decoder) model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) return model
Example 5
Project: Deep_Learning_Weather_Forecasting Author: BruceBinBoxing File: weather_model.py License: Apache License 2.0 | 6 votes |
def weather_l2(hidden_nums=100,l2=0.01): input_img = Input(shape=(37,)) hn = Dense(hidden_nums, activation='relu')(input_img) hn = Dense(hidden_nums, activation='relu', kernel_regularizer=regularizers.l2(l2))(hn) out_u = Dense(37, activation='sigmoid', name='ae_part')(hn) out_sig = Dense(37, activation='linear', name='pred_part')(hn) out_both = concatenate([out_u, out_sig], axis=1, name = 'concatenate') #weather_model = Model(input_img, outputs=[out_ae, out_pred]) mve_model = Model(input_img, outputs=[out_both]) mve_model.compile(optimizer='adam', loss=mve_loss, loss_weights=[1.]) return mve_model
Example 6
Project: Deep_Learning_Weather_Forecasting Author: BruceBinBoxing File: weather_model.py License: Apache License 2.0 | 6 votes |
def CausalCNN(n_filters, lr, decay, loss, seq_len, input_features, strides_len, kernel_size, dilation_rates): inputs = Input(shape=(seq_len, input_features), name='input_layer') x=inputs for dilation_rate in dilation_rates: x = Conv1D(filters=n_filters, kernel_size=kernel_size, padding='causal', dilation_rate=dilation_rate, activation='linear')(x) x = BatchNormalization()(x) x = Activation('relu')(x) #x = Dense(7, activation='relu', name='dense_layer')(x) outputs = Dense(3, activation='sigmoid', name='output_layer')(x) causalcnn = Model(inputs, outputs=[outputs]) return causalcnn
Example 7
Project: Deep_Learning_Weather_Forecasting Author: BruceBinBoxing File: weather_model.py License: Apache License 2.0 | 6 votes |
def weather_ae(layers, lr, decay, loss, input_len, input_features): inputs = Input(shape=(input_len, input_features), name='input_layer') for i, hidden_nums in enumerate(layers): if i==0: hn = Dense(hidden_nums, activation='relu')(inputs) else: hn = Dense(hidden_nums, activation='relu')(hn) outputs = Dense(3, activation='sigmoid', name='output_layer')(hn) weather_model = Model(inputs, outputs=[outputs]) return weather_model
Example 8
Project: cnn-levelset Author: wiseodd File: localizer.py License: MIT License | 6 votes |
def __init__(self, model_path=None): if model_path is not None: self.model = self.load_model(model_path) else: # VGG16 last conv features inputs = Input(shape=(7, 7, 512)) x = Convolution2D(128, 1, 1)(inputs) x = Flatten()(x) # Cls head h_cls = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x) h_cls = Dropout(p=0.5)(h_cls) cls_head = Dense(20, activation='softmax', name='cls')(h_cls) # Reg head h_reg = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x) h_reg = Dropout(p=0.5)(h_reg) reg_head = Dense(4, activation='linear', name='reg')(h_reg) # Joint model self.model = Model(input=inputs, output=[cls_head, reg_head])
Example 9
Project: Keras-GAN Author: eriklindernoren File: sgan.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim)) model.add(Reshape((7, 7, 128))) model.add(BatchNormalization(momentum=0.8)) model.add(UpSampling2D()) model.add(Conv2D(128, kernel_size=3, padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(momentum=0.8)) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=3, padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(1, kernel_size=3, padding="same")) model.add(Activation("tanh")) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 10
Project: Keras-GAN Author: eriklindernoren File: context_encoder.py License: MIT License | 6 votes |
def build_discriminator(self): model = Sequential() model.add(Conv2D(64, kernel_size=3, strides=2, input_shape=self.missing_shape, padding="same")) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(128, kernel_size=3, strides=2, padding="same")) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(256, kernel_size=3, padding="same")) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.missing_shape) validity = model(img) return Model(img, validity)
Example 11
Project: Keras-GAN Author: eriklindernoren File: ccgan.py License: MIT License | 6 votes |
def build_discriminator(self): img = Input(shape=self.img_shape) model = Sequential() model.add(Conv2D(64, kernel_size=4, strides=2, padding='same', input_shape=self.img_shape)) model.add(LeakyReLU(alpha=0.8)) model.add(Conv2D(128, kernel_size=4, strides=2, padding='same')) model.add(LeakyReLU(alpha=0.2)) model.add(InstanceNormalization()) model.add(Conv2D(256, kernel_size=4, strides=2, padding='same')) model.add(LeakyReLU(alpha=0.2)) model.add(InstanceNormalization()) model.summary() img = Input(shape=self.img_shape) features = model(img) validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(features) label = Flatten()(features) label = Dense(self.num_classes+1, activation="softmax")(label) return Model(img, [validity, label])
Example 12
Project: Keras-GAN Author: eriklindernoren File: bigan.py License: MIT License | 6 votes |
def build_encoder(self): model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(self.latent_dim)) model.summary() img = Input(shape=self.img_shape) z = model(img) return Model(img, z)
Example 13
Project: Keras-GAN Author: eriklindernoren File: bigan.py License: MIT License | 6 votes |
def build_discriminator(self): z = Input(shape=(self.latent_dim, )) img = Input(shape=self.img_shape) d_in = concatenate([z, Flatten()(img)]) model = Dense(1024)(d_in) model = LeakyReLU(alpha=0.2)(model) model = Dropout(0.5)(model) model = Dense(1024)(model) model = LeakyReLU(alpha=0.2)(model) model = Dropout(0.5)(model) model = Dense(1024)(model) model = LeakyReLU(alpha=0.2)(model) model = Dropout(0.5)(model) validity = Dense(1, activation="sigmoid")(model) return Model([z, img], validity)
Example 14
Project: Keras-GAN Author: eriklindernoren File: pixelda.py License: MIT License | 6 votes |
def build_classifier(self): def clf_layer(layer_input, filters, f_size=4, normalization=True): """Classifier layer""" d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input) d = LeakyReLU(alpha=0.2)(d) if normalization: d = InstanceNormalization()(d) return d img = Input(shape=self.img_shape) c1 = clf_layer(img, self.cf, normalization=False) c2 = clf_layer(c1, self.cf*2) c3 = clf_layer(c2, self.cf*4) c4 = clf_layer(c3, self.cf*8) c5 = clf_layer(c4, self.cf*8) class_pred = Dense(self.num_classes, activation='softmax')(Flatten()(c5)) return Model(img, class_pred)
Example 15
Project: Keras-GAN Author: eriklindernoren File: wgan.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim)) model.add(Reshape((7, 7, 128))) model.add(UpSampling2D()) model.add(Conv2D(128, kernel_size=4, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=4, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(Conv2D(self.channels, kernel_size=4, padding="same")) model.add(Activation("tanh")) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 16
Project: Keras-GAN Author: eriklindernoren File: wgan_gp.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim)) model.add(Reshape((7, 7, 128))) model.add(UpSampling2D()) model.add(Conv2D(128, kernel_size=4, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=4, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(Conv2D(self.channels, kernel_size=4, padding="same")) model.add(Activation("tanh")) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 17
Project: Keras-GAN Author: eriklindernoren File: lsgan.py License: MIT License | 6 votes |
def build_discriminator(self): model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(256)) model.add(LeakyReLU(alpha=0.2)) # (!!!) No softmax model.add(Dense(1)) model.summary() img = Input(shape=self.img_shape) validity = model(img) return Model(img, validity)
Example 18
Project: Keras-GAN Author: eriklindernoren File: cogan.py License: MIT License | 6 votes |
def build_discriminators(self): img1 = Input(shape=self.img_shape) img2 = Input(shape=self.img_shape) # Shared discriminator layers model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(256)) model.add(LeakyReLU(alpha=0.2)) img1_embedding = model(img1) img2_embedding = model(img2) # Discriminator 1 validity1 = Dense(1, activation='sigmoid')(img1_embedding) # Discriminator 2 validity2 = Dense(1, activation='sigmoid')(img2_embedding) return Model(img1, validity1), Model(img2, validity2)
Example 19
Project: Keras-GAN Author: eriklindernoren File: dcgan.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim)) model.add(Reshape((7, 7, 128))) model.add(UpSampling2D()) model.add(Conv2D(128, kernel_size=3, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=3, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(Activation("relu")) model.add(Conv2D(self.channels, kernel_size=3, padding="same")) model.add(Activation("tanh")) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 20
Project: Keras-GAN Author: eriklindernoren File: gan.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(256, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(1024)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 21
Project: Keras-GAN Author: eriklindernoren File: gan.py License: MIT License | 6 votes |
def build_discriminator(self): model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(256)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.img_shape) validity = model(img) return Model(img, validity)
Example 22
Project: Keras-GAN Author: eriklindernoren File: aae.py License: MIT License | 6 votes |
def build_decoder(self): model = Sequential() model.add(Dense(512, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() z = Input(shape=(self.latent_dim,)) img = model(z) return Model(z, img)
Example 23
Project: Keras-GAN Author: eriklindernoren File: bgan.py License: MIT License | 6 votes |
def build_generator(self): model = Sequential() model.add(Dense(256, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(1024)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() noise = Input(shape=(self.latent_dim,)) img = model(noise) return Model(noise, img)
Example 24
Project: Keras-GAN Author: eriklindernoren File: bgan.py License: MIT License | 6 votes |
def build_discriminator(self): model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(256)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.img_shape) validity = model(img) return Model(img, validity)
Example 25
Project: tartarus Author: sergiooramas File: models.py License: MIT License | 6 votes |
def get_model_41(params): embedding_weights = pickle.load(open("../data/datasets/train_data/embedding_weights_w2v-google_MSD-AG.pk","rb")) # main sequential model model = Sequential() model.add(Embedding(len(embedding_weights[0]), params['embedding_dim'], input_length=params['sequence_length'], weights=embedding_weights)) #model.add(Dropout(params['dropout_prob'][0], input_shape=(params['sequence_length'], params['embedding_dim']))) model.add(LSTM(2048)) #model.add(Dropout(params['dropout_prob'][1])) model.add(Dense(output_dim=params["n_out"], init="uniform")) model.add(Activation(params['final_activation'])) logging.debug("Output CNN: %s" % str(model.output_shape)) if params['final_activation'] == 'linear': model.add(Lambda(lambda x :K.l2_normalize(x, axis=1))) return model # CRNN Arch for audio
Example 26
Project: Jtyoui Author: jtyoui File: NER.py License: MIT License | 6 votes |
def train_model(): if cxl_model: embedding_matrix = load_embedding() else: embedding_matrix = {} train, label = vocab_train_label(train_path, vocab=vocab, tags=tag, max_chunk_length=length) n = np.array(label, dtype=np.float) labels = n.reshape((n.shape[0], n.shape[1], 1)) model = Sequential([ Embedding(input_dim=len(vocab), output_dim=300, mask_zero=True, input_length=length, weights=[embedding_matrix], trainable=False), SpatialDropout1D(0.2), Bidirectional(layer=LSTM(units=150, return_sequences=True, dropout=0.2, recurrent_dropout=0.2)), TimeDistributed(Dense(len(tag), activation=relu)), ]) crf_ = CRF(units=len(tag), sparse_target=True) model.add(crf_) model.compile(optimizer=Adam(), loss=crf_.loss_function, metrics=[crf_.accuracy]) model.fit(x=np.array(train), y=labels, batch_size=16, epochs=4, callbacks=[RemoteMonitor()]) model.save(model_path)
Example 27
Project: Jtyoui Author: jtyoui File: cnn_rnn_crf.py License: MIT License | 6 votes |
def create_model(): inputs = Input(shape=(length,), dtype='int32', name='inputs') embedding_1 = Embedding(len(vocab), EMBED_DIM, input_length=length, mask_zero=True)(inputs) bilstm = Bidirectional(LSTM(EMBED_DIM // 2, return_sequences=True))(embedding_1) bilstm_dropout = Dropout(DROPOUT_RATE)(bilstm) embedding_2 = Embedding(len(vocab), EMBED_DIM, input_length=length)(inputs) con = Conv1D(filters=FILTERS, kernel_size=2 * HALF_WIN_SIZE + 1, padding='same')(embedding_2) con_d = Dropout(DROPOUT_RATE)(con) dense_con = TimeDistributed(Dense(DENSE_DIM))(con_d) rnn_cnn = concatenate([bilstm_dropout, dense_con], axis=2) dense = TimeDistributed(Dense(len(chunk_tags)))(rnn_cnn) crf = CRF(len(chunk_tags), sparse_target=True) crf_output = crf(dense) model = Model(input=[inputs], output=[crf_output]) model.compile(loss=crf.loss_function, optimizer=Adam(), metrics=[crf.accuracy]) return model
Example 28
Project: armchair-expert Author: csvance File: reaction.py License: MIT License | 6 votes |
def __init__(self, path: str = None, use_gpu=False): import tensorflow as tf from keras.models import Sequential from keras.layers import Dense from keras.backend import set_session self.model = Sequential() self.model.add(Dense(AOLReactionFeatureAnalyzer.NUM_FEATURES, activation='relu', input_dim=AOLReactionFeatureAnalyzer.NUM_FEATURES)) self.model.add(Dense(AOLReactionFeatureAnalyzer.NUM_FEATURES - 2, activation='relu')) self.model.add(Dense(1, activation='sigmoid')) self.model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) if use_gpu: config = tf.ConfigProto() config.gpu_options.allow_growth = True set_session(tf.Session(config=config))
Example 29
Project: armchair-expert Author: csvance File: structure.py License: MIT License | 6 votes |
def __init__(self, use_gpu: bool = False): import tensorflow as tf from keras.models import Sequential from keras.layers import Dense, Embedding from keras.layers import LSTM from keras.backend import set_session latent_dim = StructureModel.SEQUENCE_LENGTH * 8 model = Sequential() model.add( Embedding(StructureFeatureAnalyzer.NUM_FEATURES, StructureFeatureAnalyzer.NUM_FEATURES, input_length=StructureModel.SEQUENCE_LENGTH)) model.add(LSTM(latent_dim, dropout=0.2, return_sequences=False)) model.add(Dense(StructureFeatureAnalyzer.NUM_FEATURES, activation='softmax')) model.summary() model.compile(loss='sparse_categorical_crossentropy', optimizer='adam') self.model = model if use_gpu: config = tf.ConfigProto() config.gpu_options.allow_growth = True set_session(tf.Session(config=config))
Example 30
Project: Keras-BiGAN Author: manicman1999 File: bigan.py License: MIT License | 6 votes |
def encoder(self): if self.E: return self.E inp = Input(shape = [im_size, im_size, 3]) x = d_block(inp, 1 * cha) #64 x = d_block(x, 2 * cha) #32 x = d_block(x, 3 * cha) #16 x = d_block(x, 4 * cha) #8 x = d_block(x, 8 * cha) #4 x = d_block(x, 16 * cha, p = False) #4 x = Flatten()(x) x = Dense(16 * cha, kernel_initializer = 'he_normal')(x) x = LeakyReLU(0.2)(x) x = Dense(latent_size, kernel_initializer = 'he_normal', bias_initializer = 'zeros')(x) self.E = Model(inputs = inp, outputs = x) return self.E