我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.layers.Reshape()。
def tsinalis(input_shape, n_classes): """ Input size should be [batch, 1d, 2d, ch] = (None, 1, 15000, 1) """ model = Sequential(name='Tsinalis') model.add(Conv1D (kernel_size = (200), filters = 20, input_shape=input_shape, activation='relu')) print(model.input_shape) print(model.output_shape) model.add(MaxPooling1D(pool_size = (20), strides=(10))) print(model.output_shape) model.add(keras.layers.core.Reshape([20,-1,1])) print(model.output_shape) model.add(Conv2D (kernel_size = (20,30), filters = 400, activation='relu')) print(model.output_shape) model.add(MaxPooling2D(pool_size = (1,10), strides=(1,2))) print(model.output_shape) model.add(Flatten()) print(model.output_shape) model.add(Dense (500, activation='relu')) model.add(Dense (500, activation='relu')) model.add(Dense(n_classes, activation = 'softmax',activity_regularizer=keras.regularizers.l2() )) model.compile( loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(), metrics=[keras.metrics.categorical_accuracy]) return model
def make_generator(): """Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images of size 28x28x1.""" model = Sequential() model.add(Dense(1024, input_dim=100)) model.add(LeakyReLU()) model.add(Dense(128 * 7 * 7)) model.add(BatchNormalization()) model.add(LeakyReLU()) if K.image_data_format() == 'channels_first': model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7,))) bn_axis = 1 else: model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7,))) bn_axis = -1 model.add(Conv2DTranspose(128, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Convolution2D(64, (5, 5), padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) # Because we normalized training inputs to lie in the range [-1, 1], # the tanh function should be used for the output of the generator to ensure its output # also lies in this range. model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh')) return model
def build_encoder(self,input_shape): return [Reshape((*input_shape,1)), GaussianNoise(0.1), BN(), Convolution2D(self.parameters['clayer'],(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), Dropout(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), Convolution2D(self.parameters['clayer'],(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), Dropout(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), Convolution2D(self.parameters['clayer'],(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), Dropout(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), flatten,]
def generator_model(noise_dim=100, aux_dim=47, model_name="generator"): # Merge noise and auxilary inputs gen_input = Input(shape=(noise_dim,), name="noise_input") aux_input = Input(shape=(aux_dim,), name="auxilary_input") x = concatenate([gen_input, aux_input], axis=-1) # Dense Layer 1 x = Dense(10 * 100)(x) x = BatchNormalization()(x) x = LeakyReLU(0.2)(x) # output shape is 10*100 # Reshape the tensors to support CNNs x = Reshape((100, 10))(x) # shape is 100 x 10 # Conv Layer 1 x = Conv1D(filters=250, kernel_size=13, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(0.2)(x) # output shape is 100 x 250 x = UpSampling1D(size=2)(x) # output shape is 200 x 250 # Conv Layer 2 x = Conv1D(filters=100, kernel_size=13, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU(0.2)(x) # output shape is 200 x 100 x = UpSampling1D(size=2)(x) # output shape is 400 x 100 # Conv Layer 3 x = Conv1D(filters=1, kernel_size=13, padding='same')(x) x = BatchNormalization()(x) x = Activation('tanh')(x) # final output shape is 400 x 1 generator_model = Model( outputs=[x], inputs=[gen_input, aux_input], name=model_name) return generator_model
def model_generator(): model = Sequential() nch = 256 reg = lambda: l1l2(l1=1e-7, l2=1e-7) h = 5 model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg())) model.add(BatchNormalization(mode=0)) model.add(Reshape(dim_ordering_shape((nch, 4, 4)))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg())) model.add(Activation('sigmoid')) return model
def rcnn(input_shape, n_classes): """ Input size should be [batch, 1d, ch] = (XXX, 3000, 1) """ model = Sequential(name='RCNN test') model.add(Conv1D (kernel_size = (200), filters = 20, batch_input_shape=input_shape, activation='elu')) model.add(MaxPooling1D(pool_size = (20), strides=(10))) model.add(Conv1D (kernel_size = (20), filters = 200, activation='elu')) model.add(MaxPooling1D(pool_size = (10), strides=(3))) model.add(Conv1D (kernel_size = (20), filters = 200, activation='elu')) model.add(MaxPooling1D(pool_size = (10), strides=(3))) model.add(Dense (512, activation='elu')) model.add(Dense (512, activation='elu')) model.add(Reshape((1,model.output_shape[1]))) model.add(LSTM(256, stateful=True, return_sequences=False)) model.add(Dropout(0.3)) model.add(Dense(n_classes, activation = 'sigmoid')) model.compile(loss='categorical_crossentropy', optimizer=Adadelta()) return model
def build_encoder(self,input_shape): last_convolution = np.array(input_shape) // 8 self.parameters['clayer'] = 8 self.parameters['N'] = int(np.prod(last_convolution)*self.parameters['clayer'] // self.parameters['M']) return [Reshape((*input_shape,1)), GaussianNoise(0.1), BN(), Convolution2D(16,(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), Dropout(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), Convolution2D(64,(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), SpatialDropout2D(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), Convolution2D(64,(3,3), activation=self.parameters['activation'],padding='same', use_bias=False), SpatialDropout2D(self.parameters['dropout']), BN(), MaxPooling2D((2,2)), Convolution2D(64,(1,1), activation=self.parameters['activation'],padding='same', use_bias=False), SpatialDropout2D(self.parameters['dropout']), BN(), Convolution2D(self.parameters['clayer'],(1,1), padding='same'), flatten, ] # mixin classes ############################################################### # Now effectively 3 subclasses; GumbelSoftmax in the output, Convolution, Gaussian. # there are 4 more results of mixins:
def __call__(self, model): if self.crop_right: model = Lambda(lambda x: x[:, :, :K.int_shape(x)[2]-1, :])(model) if self.v is not None: model = Merge(mode='sum')([model, self.v]) if self.h is not None: hV = Dense(output_dim=2*self.filters)(self.h) hV = Reshape((1, 1, 2*self.filters))(hV) model = Lambda(lambda x: x[0]+x[1])([model,hV]) model_f = Lambda(lambda x: x[:,:,:,:self.filters])(model) model_g = Lambda(lambda x: x[:,:,:,self.filters:])(model) model_f = Lambda(lambda x: K.tanh(x))(model_f) model_g = Lambda(lambda x: K.sigmoid(x))(model_g) res = Merge(mode='mul')([model_f, model_g]) return res
def create_model(numNodes, factors): left_input = Input(shape=(1,)) right_input = Input(shape=(1,)) left_model = Sequential() left_model.add(Embedding(input_dim=numNodes + 1, output_dim=factors, input_length=1, mask_zero=False)) left_model.add(Reshape((factors,))) right_model = Sequential() right_model.add(Embedding(input_dim=numNodes + 1, output_dim=factors, input_length=1, mask_zero=False)) right_model.add(Reshape((factors,))) left_embed = left_model(left_input) right_embed = left_model(right_input) left_right_dot = merge([left_embed, right_embed], mode="dot", dot_axes=1, name="left_right_dot") model = Model(input=[left_input, right_input], output=[left_right_dot]) embed_generator = Model(input=[left_input, right_input], output=[left_embed, right_embed]) return model, embed_generator
def build_generator(self): model = Sequential() model.add(Dense(1024, activation='relu', input_dim=self.latent_dim)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(128 * 7 * 7, activation="relu")) model.add(BatchNormalization(momentum=0.8)) model.add(Reshape((7, 7, 128))) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=4, padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(momentum=0.8)) model.add(UpSampling2D()) model.add(Conv2D(self.channels, kernel_size=4, padding='same')) model.add(Activation("tanh")) model.summary() gen_input = Input(shape=(self.latent_dim,)) img = model(gen_input) return Model(gen_input, img)
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=100)) 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=(100,)) img = model(noise) return Model(noise, img)
def build_generator(self): noise_shape = (100,) model = Sequential() model.add(Dense(256, input_shape=noise_shape)) 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=noise_shape) img = model(noise) return Model(noise, img)
def build_generator(self): model = Sequential() model.add(Dense(512, 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(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() z = Input(shape=(self.latent_dim,)) gen_img = model(z) return Model(z, gen_img)
def deflating_convolution(inputs, n_deflation_layers, n_filters_init=32, noise=None, name_prefix=None): def add_linear_noise(x, eps, ind): flattened_deflated = Reshape((-1,), name=name_prefix + '_conv_flatten_{}'.format(ind))(x) deflated_shape = ker.int_shape(x) deflated_size = deflated_shape[1] * deflated_shape[2] * deflated_shape[3] noise_transformed = Dense(deflated_size, activation=None, name=name_prefix + '_conv_noise_dense_{}'.format(ind))(eps) added_noise = Add(name=name_prefix + '_conv_add_noise_{}'.format(ind))([noise_transformed, flattened_deflated]) x = Reshape((deflated_shape[1], deflated_shape[2], deflated_shape[3]), name=name_prefix + '_conv_backreshape_{}'.format(ind))(added_noise) return x deflated = Conv2D(filters=n_filters_init, kernel_size=(5, 5), strides=(2, 2), padding='same', activation='relu', name=name_prefix + '_conv_0')(inputs) if noise is not None: deflated = add_linear_noise(deflated, noise, 0) for i in range(1, n_deflation_layers): deflated = Conv2D(filters=n_filters_init * (2**i), kernel_size=(5, 5), strides=(2, 2), padding='same', activation='relu', name=name_prefix + '_conv_{}'.format(i))(deflated) # if noise is not None: # deflated = add_linear_noise(deflated, noise, i) return deflated
def test_tiny_mcrnn_music_tagger(self): x_in = Input(shape=(4,6,1)) x = ZeroPadding2D(padding=(0, 1))(x_in) x = BatchNormalization(axis=2, name='bn_0_freq')(x) # Conv block 1 x = Conv2D(2, (3, 3), padding='same', name='conv1')(x) x = BatchNormalization(axis=3, name='bn1')(x) x = Activation('elu')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(x) # Conv block 2 x = Conv2D(4, (3, 3), padding='same', name='conv2')(x) x = BatchNormalization(axis=3, name='bn2')(x) x = Activation('elu')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(x) # Should get you (1,1,2,4) x = Reshape((2, 4))(x) x = GRU(32, return_sequences=True, name='gru1')(x) x = GRU(32, return_sequences=False, name='gru2')(x) # Create model. model = Model(x_in, x) model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()]) self._test_keras_model(model, mode='random_zero_mean', delta=1e-2)
def test_tiny_image_captioning(self): # use a conv layer as a image feature branch img_input_1 = Input(shape=(16,16,3)) x = Conv2D(2,(3,3))(img_input_1) x = Flatten()(x) img_model = Model(inputs=[img_input_1], outputs=[x]) img_input = Input(shape=(16,16,3)) x = img_model(img_input) x = Dense(8, name = 'cap_dense')(x) x = Reshape((1,8), name = 'cap_reshape')(x) sentence_input = Input(shape=(5,)) # max_length = 5 y = Embedding(8, 8, name = 'cap_embedding')(sentence_input) z = concatenate([x,y], axis = 1, name = 'cap_merge') z = LSTM(4, return_sequences = True, name = 'cap_lstm')(z) z = TimeDistributed(Dense(8), name = 'cap_timedistributed')(z) combined_model = Model(inputs=[img_input, sentence_input], outputs=[z]) self._test_keras_model(combined_model, one_dim_seq_flags=[False, True])
def get_model(): inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS)) base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES) #base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES) #base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES) #base = models.get_unet(inputs, NUMBER_OF_CLASSES) #base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES) # softmax reshape= Reshape((-1,NUMBER_OF_CLASSES))(base) act = Activation('softmax')(reshape) model = Model(inputs=inputs, outputs=act) model.compile(optimizer=Adadelta(), loss='categorical_crossentropy') #print(model.summary()) #sys.exit() return model
def get_model(): inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS)) base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES) #base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES) #base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES) #base = models.get_unet(inputs, NUMBER_OF_CLASSES) #base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES) # sigmoid reshape= Reshape((-1,NUMBER_OF_CLASSES))(base) act = Activation('sigmoid')(reshape) model = Model(inputs=inputs, outputs=act) model.compile(optimizer=Adadelta(), loss='binary_crossentropy') #print(model.summary()) #sys.exit() return model
def global_handle(self, emb_layer, flag): fw_lstm_out = self.forward_lstm(emb_layer) bw_lstm_out = self.backward_lstm(emb_layer) conv_out = self.conv_dropout(self.conv(emb_layer)) fw_lstm_out = TimeDistributed(Dense(self.params['attention_dim']), name='fw_tb_'+flag)(fw_lstm_out) fw_lstm_att = Attention()(fw_lstm_out) # fw_lstm_att = Reshape((self.params['lstm_output_dim'], 1))(fw_lstm_att) conv_out = TimeDistributed(Dense(self.params['attention_dim']), name='conv_tb_'+flag)(conv_out) conv_att = Attention()(conv_out) # conv_att = Reshape((self.params['filters'], 1))(conv_att) bw_lstm_out = TimeDistributed(Dense(self.params['attention_dim']), name='bw_tb_'+flag)(bw_lstm_out) bw_lstm_att = Attention()(bw_lstm_out) # bw_lstm_att = Reshape((self.params['lstm_output_dim'], 1))(bw_lstm_att) return concatenate([fw_lstm_att, conv_att, bw_lstm_att], axis=2)
def __init__(self, g_size=(3, 128, 64), g_nb_filters=128, g_nb_coding=200, g_init=None, **kwargs): super(MLP, self).__init__(**kwargs) self.g_size = g_size self.g_nb_filters = g_nb_filters self.g_nb_coding = g_nb_coding self.g_init = g_init if g_init is not None else InitNormal() c, h, w = g_size # h and w should be multiply of 16 nf = g_nb_filters self.add(Dense(g_nb_filters, input_shape=(g_nb_coding,)) ) self.add(Activation('relu')) self.add(Dense(g_nb_filters)) self.add(Activation('relu')) self.add(Dense(g_nb_filters, )) self.add(Activation('relu')) self.add(Dense(np.prod(g_size), )) self.add(Reshape(g_size)) g_init(self)
def fit_cnn1(self, X33_train, Y_train, X33_unif_train, Y_unif_train): # Create temp cnn with input shape=(4,33,33,) input33 = Input(shape=(4, 33, 33)) output_cnn = self.one_block_model(input33) output_cnn = Reshape((5,))(output_cnn) # Cnn compiling temp_cnn = Model(inputs=input33, outputs=output_cnn) sgd = SGD(lr=self.learning_rate, momentum=self.momentum_rate, decay=self.decay_rate, nesterov=False) temp_cnn.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) # Stop the training if the monitor function doesn't change after patience epochs earlystopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1, mode='auto') # Save model after each epoch to check/bm_epoch#-val_loss checkpointer = ModelCheckpoint(filepath="/home/ixb3/Scrivania/check/bm_{epoch:02d}-{val_loss:.2f}.hdf5", verbose=1) # First-phase training with uniformly distribuited training set temp_cnn.fit(x=X33_train, y=Y_train, batch_size=self.batch_size, epochs=self.nb_epoch, callbacks=[earlystopping, checkpointer], validation_split=0.3, verbose=1) # fix all the layers of the temporary cnn except the output layer for the second-phase temp_cnn = self.freeze_model(temp_cnn, freeze_output=False) # Second-phase training of the output layer with training set with real distribution probabily temp_cnn.fit(x=X33_unif_train, y=Y_unif_train, batch_size=self.batch_size, epochs=self.nb_epoch, callbacks=[earlystopping, checkpointer], validation_split=0.3, verbose=1) # set the weights of the first cnn to the trained weights of the temporary cnn self.cnn1.set_weights(temp_cnn.get_weights())
def discriminator_model(): model = Sequential() model.add(Convolution1D( 12, 5, border_mode='same', input_shape=(INPUT_LN, 1))) model.add(Activation('relu')) model.add(MaxPooling1D(pool_length=N_GEN_l[0])) model.add(Convolution1D(12, 5, border_mode='same')) model.add(Activation('relu')) model.add(MaxPooling1D(pool_length=N_GEN_l[1])) #model.add(Reshape((128*7,))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dense(1)) model.add(Activation('sigmoid')) return model
def image_caption_model(vocab_size=2500, embedding_matrix=None, lang_dim=100, max_caplen=28, img_dim=2048, clipnorm=1): print('generating vocab_history model v5') # text: current word lang_input = Input(shape=(1,)) img_input = Input(shape=(img_dim,)) seq_input = Input(shape=(max_caplen,)) vhist_input = Input(shape=(vocab_size,)) if embedding_matrix is not None: x = Embedding(output_dim=lang_dim, input_dim=vocab_size, init='glorot_uniform', input_length=1, weights=[embedding_matrix])(lang_input) else: x = Embedding(output_dim=lang_dim, input_dim=vocab_size, init='glorot_uniform', input_length=1)(lang_input) lang_embed = Reshape((lang_dim,))(x) lang_embed = merge([lang_embed, seq_input], mode='concat', concat_axis=-1) lang_embed = Dense(lang_dim)(lang_embed) lang_embed = Dropout(0.25)(lang_embed) merge_layer = merge([img_input, lang_embed, vhist_input], mode='concat', concat_axis=-1) merge_layer = Reshape((1, lang_dim+img_dim+vocab_size))(merge_layer) gru_1 = GRU(img_dim)(merge_layer) gru_1 = Dropout(0.25)(gru_1) gru_1 = Dense(img_dim)(gru_1) gru_1 = BatchNormalization()(gru_1) gru_1 = Activation('softmax')(gru_1) attention_1 = merge([img_input, gru_1], mode='mul', concat_axis=-1) attention_1 = merge([attention_1, lang_embed, vhist_input], mode='concat', concat_axis=-1) attention_1 = Reshape((1, lang_dim + img_dim + vocab_size))(attention_1) gru_2 = GRU(1024)(attention_1) gru_2 = Dropout(0.25)(gru_2) gru_2 = Dense(vocab_size)(gru_2) gru_2 = BatchNormalization()(gru_2) out = Activation('softmax')(gru_2) model = Model(input=[img_input, lang_input, seq_input, vhist_input], output=out) model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.0001, clipnorm=1.)) return model
def generator_model(): model = Sequential() model.add(Dense(input_dim=100, output_dim=1024)) model.add(Activation('tanh')) model.add(Dense(128*7*7)) model.add(BatchNormalization()) model.add(Activation('tanh')) model.add(Reshape((128,7,7), input_shape=(128*7*7,))) model.add(UpSampling2D(size=(2,2), dim_ordering="th")) model.add(Convolution2D(64,5,5, border_mode='same', dim_ordering="th")) model.add(Activation('tanh')) model.add(UpSampling2D(size=(2,2), dim_ordering="th")) model.add(Convolution2D(1,5,5, border_mode='same', dim_ordering="th")) model.add(Activation('tanh')) return model
def generator_model(): model = Sequential() model.add(Dense(input_dim=100, output_dim=1024)) model.add(Activation('tanh')) model.add(Dense(128*7*7)) model.add(BatchNormalization()) model.add(Activation('tanh')) model.add(Reshape((128, 7, 7), input_shape=(128*7*7,))) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(64, 5, 5, border_mode='same')) model.add(Activation('tanh')) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(1, 5, 5, border_mode='same')) model.add(Activation('tanh')) return model
def build_generator(latent_size): cnn = Sequential() cnn.add(Dense(1024, input_dim=latent_size, activation='relu')) cnn.add(Dense(128 * 7 * 7, activation='relu')) cnn.add(Reshape((128, 7, 7))) # upsample to (..., 14, 14) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Conv2D(256, 5, padding='same', activation='relu', kernel_initializer='glorot_normal')) # upsample to (..., 28, 28) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Conv2D(128, 5, padding='same', activation='relu', kernel_initializer='glorot_normal')) # take a channel axis reduction cnn.add(Conv2D(1, 2, padding='same', activation='tanh', kernel_initializer='glorot_normal')) # this is the z space commonly refered to in GAN papers latent = Input(shape=(latent_size,)) fake_image = cnn(latent) return Model(inputs=latent, outputs=fake_image)
def build_generator(latent_size): model = Sequential() model.add(Dense(1024, input_dim=latent_size, activation='relu')) model.add(Dense(28 * 28, activation='tanh')) model.add(Reshape((1, 28, 28))) return model
def build_generator(latent_size): ''' Any model with input shape (?, latent_size) and output shape (?, 1, 28, 28) fits here. ''' model = Sequential() model.add(Dense(1024, input_dim=latent_size, activation='relu')) model.add(Dense(28 * 28, activation='tanh')) model.add(Reshape((1, 28, 28))) return model
def GeneratorDeconv(image_size = 64): L = int(image_size) inputs = Input(shape = (100, )) x = Dense(512*int(L/16)**2)(inputs) #shape(512*(L/16)**2,) x = BatchNormalization()(x) x = Activation('relu')(x) x = Reshape((int(L/16), int(L/16), 512))(x) # shape(L/16, L/16, 512) x = Conv2DTranspose(256, (4, 4), strides = (2, 2), kernel_initializer = init, padding = 'same')(x) # shape(L/8, L/8, 256) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(128, (4, 4), strides = (2, 2), kernel_initializer = init, padding = 'same')(x) # shape(L/4, L/4, 128) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(64, (4, 4), strides = (2, 2), kernel_initializer = init, padding = 'same')(x) # shape(L/2, L/2, 64) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(3, (4, 4), strides= (2, 2), kernel_initializer = init, padding = 'same')(x) # shape(L, L, 3) images = Activation('tanh')(x) model = Model(inputs = inputs, outputs = images) model.summary() return model
def build(self): query = Input(name='query', shape=(self.config['text1_maxlen'],)) show_layer_info('Input', query) doc = Input(name='doc', shape=(self.config['text2_maxlen'],)) show_layer_info('Input', doc) embedding = Embedding(self.config['vocab_size'], self.config['embed_size'], weights=[self.config['embed']], trainable = self.embed_trainable) q_embed = embedding(query) show_layer_info('Embedding', q_embed) d_embed = embedding(doc) show_layer_info('Embedding', d_embed) q_rep = Bidirectional(LSTM(self.config['hidden_size'], return_sequences=True, dropout=self.config['dropout_rate']))(q_embed) show_layer_info('Bidirectional-LSTM', q_rep) d_rep = Bidirectional(LSTM(self.config['hidden_size'], return_sequences=True, dropout=self.config['dropout_rate']))(d_embed) show_layer_info('Bidirectional-LSTM', d_rep) cross = Match(match_type='dot')([q_rep, d_rep]) #cross = Dot(axes=[2, 2])([q_embed, d_embed]) show_layer_info('Match-dot', cross) cross_reshape = Reshape((-1, ))(cross) show_layer_info('Reshape', cross_reshape) mm_k = Lambda(lambda x: K.tf.nn.top_k(x, k=self.config['topk'], sorted=True)[0])(cross_reshape) show_layer_info('Lambda-topk', mm_k) pool1_flat_drop = Dropout(rate=self.config['dropout_rate'])(mm_k) show_layer_info('Dropout', pool1_flat_drop) if self.config['target_mode'] == 'classification': out_ = Dense(2, activation='softmax')(pool1_flat_drop) elif self.config['target_mode'] in ['regression', 'ranking']: out_ = Dense(1)(pool1_flat_drop) show_layer_info('Dense', out_) #model = Model(inputs=[query, doc, dpool_index], outputs=out_) model = Model(inputs=[query, doc], outputs=out_) return model
def build(self): query = Input(name='query', shape=(self.config['text1_maxlen'],)) show_layer_info('Input', query) doc = Input(name='doc', shape=(self.config['text2_maxlen'],)) show_layer_info('Input', doc) dpool_index = Input(name='dpool_index', shape=[self.config['text1_maxlen'], self.config['text2_maxlen'], 3], dtype='int32') show_layer_info('Input', dpool_index) embedding = Embedding(self.config['vocab_size'], self.config['embed_size'], weights=[self.config['embed']], trainable = self.embed_trainable) q_embed = embedding(query) show_layer_info('Embedding', q_embed) d_embed = embedding(doc) show_layer_info('Embedding', d_embed) cross = Dot(axes=[2, 2], normalize=False)([q_embed, d_embed]) show_layer_info('Dot', cross) cross_reshape = Reshape((self.config['text1_maxlen'], self.config['text2_maxlen'], 1))(cross) show_layer_info('Reshape', cross_reshape) conv2d = Conv2D(self.config['kernel_count'], self.config['kernel_size'], padding='same', activation='relu') dpool = DynamicMaxPooling(self.config['dpool_size'][0], self.config['dpool_size'][1]) conv1 = conv2d(cross_reshape) show_layer_info('Conv2D', conv1) pool1 = dpool([conv1, dpool_index]) show_layer_info('DynamicMaxPooling', pool1) pool1_flat = Flatten()(pool1) show_layer_info('Flatten', pool1_flat) pool1_flat_drop = Dropout(rate=self.config['dropout_rate'])(pool1_flat) show_layer_info('Dropout', pool1_flat_drop) if self.config['target_mode'] == 'classification': out_ = Dense(2, activation='softmax')(pool1_flat_drop) elif self.config['target_mode'] in ['regression', 'ranking']: out_ = Dense(1)(pool1_flat_drop) show_layer_info('Dense', out_) model = Model(inputs=[query, doc, dpool_index], outputs=out_) return model
def build_errors(states,base,pad,dim,size): # address the numerical viscosity in swirling s = K.round(states+viscosity_adjustment) s = Reshape((dim+2*pad,dim+2*pad,1))(s) s = Cropping2D(((pad,pad),(pad,pad)))(s) s = K.reshape(s,[-1,size,base,size,base]) s = K.permute_dimensions(s, [0,1,3,2,4]) s = K.reshape(s,[-1,size,size,1,base,base]) s = K.tile (s,[1, 1, 1, 2, 1, 1,]) # number of panels : 2 allpanels = K.variable(panels) allpanels = K.reshape(allpanels, [1,1,1,2,base,base]) allpanels = K.tile(allpanels, [K.shape(s)[0], size,size, 1, 1, 1]) def hash(x): ## 2x2 average hashing x = K.reshape(x, [-1,size,size,2, base//3, 3, base//3, 3]) x = K.mean(x, axis=(5,7)) return K.round(x) ## diff hashing (horizontal diff) # x1 = x[:,:,:,:,:,:-1] # x2 = x[:,:,:,:,:,1:] # d = x1 - x2 # return K.round(d) ## just rounding # return K.round(x) ## do nothing # return x # s = hash(s) # allpanels = hash(allpanels) # error = K.binary_crossentropy(s, allpanels) error = K.abs(s - allpanels) error = hash(error) error = K.mean(error, axis=(4,5)) return error
def __init__(self,N,M,min,max,anneal_rate): self.N = N self.M = M self.layers = Sequential([ # Dense(N * M), Reshape((N,M))]) self.min = min self.max = max self.anneal_rate = anneal_rate self.tau = K.variable(max, name="temperature")
def build_decoder(self,input_shape): data_dim = np.prod(input_shape) return [ *([Dropout(self.parameters['dropout'])] if self.parameters['dropout_z'] else []), Dense(self.parameters['layer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']), Dense(self.parameters['layer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']), Dense(data_dim, activation='sigmoid'), Reshape(input_shape),]
def _build(self,input_shape): data_dim = np.prod(input_shape) self.gs = self.build_gs() self.gs2 = self.build_gs(N=data_dim) self.gs3 = self.build_gs(N=data_dim) _encoder = self.build_encoder(input_shape) _decoder = self.build_decoder(input_shape) x = Input(shape=input_shape) z = Sequential([flatten, *_encoder, self.gs])(x) y = Sequential([flatten, *_decoder, self.gs2, Lambda(take_true), Reshape(input_shape)])(z) z2 = Input(shape=(self.parameters['N'], self.parameters['M'])) y2 = Sequential([flatten, *_decoder, self.gs3, Lambda(take_true), Reshape(input_shape)])(z2) def rec(x, y): return bce(K.reshape(x,(K.shape(x)[0],data_dim,)), K.reshape(y,(K.shape(x)[0],data_dim,))) def loss(x, y): return rec(x,y) + self.gs.loss() + self.gs2.loss() self.callbacks.append(LambdaCallback(on_epoch_end=self.gs.cool)) self.callbacks.append(LambdaCallback(on_epoch_end=self.gs2.cool)) self.callbacks.append(LambdaCallback(on_epoch_end=self.gs3.cool)) self.custom_log_functions['tau'] = lambda: K.get_value(self.gs.tau) self.loss = loss self.metrics.append(rec) self.encoder = Model(x, z) self.decoder = Model(z2, y2) self.net = Model(x, y) self.autoencoder = self.net
def build_decoder(self,input_shape): "this function did not converge well. sigh" data_dim = np.prod(input_shape) last_convolution = 1 + np.array(input_shape) // 4 first_convolution = last_convolution * 4 diff = tuple(first_convolution - input_shape) crop = [[0,0],[0,0]] for i in range(2): if diff[i] % 2 == 0: for j in range(2): crop[i][j] = diff[i] // 2 else: crop[i][0] = diff[i] // 2 crop[i][1] = diff[i] // 2 + 1 crop = ((crop[0][0],crop[0][1]),(crop[1][0],crop[1][1])) print(last_convolution,first_convolution,diff,crop) return [*([Dropout(self.parameters['dropout'])] if self.parameters['dropout_z'] else []), *[Dense(self.parameters['layer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']),], *[Dense(np.prod(last_convolution) * self.parameters['clayer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']),], Reshape((*last_convolution, self.parameters['clayer'])), *[UpSampling2D((2,2)), Deconvolution2D(self.parameters['clayer'],(3,3), activation='relu',padding='same', use_bias=False), BN(), Dropout(self.parameters['dropout']),], *[UpSampling2D((2,2)), Deconvolution2D(1,(3,3), activation='sigmoid',padding='same'),], Cropping2D(crop), Reshape(input_shape),]
def build_decoder(self,input_shape): data_dim = np.prod(input_shape) return [ Dense(self.parameters['layer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']), Dense(self.parameters['layer'], activation='relu', use_bias=False), BN(), Dropout(self.parameters['dropout']), Dense(data_dim, activation='sigmoid'), Reshape(input_shape),]
def combined_discriminate2(data,sae,discriminator,**kwargs): _data = Input(shape=data.shape[1:]) _data2 = Reshape((*data.shape[1:],1))(_data) _categorical = wrap(_data,K.concatenate([_data2, 1-_data2],-1),name="categorical") _images = sae.decoder(_categorical) _features = sae.features(_images) _results = discriminator.net(_features) m = Model(_data, _results) return m.predict(data,**kwargs)
def __init__(self,sae,discriminator): _data = Input(shape=(sae.parameters['N'],)) _data2 = Reshape((sae.parameters['N'],1))(_data) _categorical = wrap(_data,K.concatenate([_data2, 1-_data2],-1),name="categorical") _images = sae.decoder(_categorical) _features = sae.features(_images) _results = discriminator.net(_features) m = Model(_data, _results) self.model = m # action autoencoder ################################################################
def build_decoder(self,input_shape): data_dim = np.prod(input_shape) return [ *[ Sequential([ Dense(self.parameters['layer'], activation=self.parameters['decoder_activation'], use_bias=False), BN(), Dropout(self.parameters['dropout']),]) for i in range(self.parameters['decoder_layers']) ], Sequential([ Dense(data_dim, activation='sigmoid'), Reshape(input_shape),]),]
def build_generator(latent_size): # we will map a pair of (z, L), where z is a latent vector and L is a # label drawn from P_c, to image space (..., 1, 28, 28) cnn = Sequential() cnn.add(Dense(1024, input_dim=latent_size, activation='relu')) cnn.add(Dense(128 * 7 * 7, activation='relu')) cnn.add(Reshape((128, 7, 7))) # upsample to (..., 14, 14) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Convolution2D(256, 5, 5, border_mode='same', activation='relu', init='glorot_normal')) # upsample to (..., 28, 28) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Convolution2D(128, 5, 5, border_mode='same', activation='relu', init='glorot_normal')) # take a channel axis reduction cnn.add(Convolution2D(1, 2, 2, border_mode='same', activation='tanh', init='glorot_normal')) # this is the z space commonly refered to in GAN papers latent = Input(shape=(latent_size, )) # this will be our label image_class = Input(shape=(1,), dtype='int32') # 10 classes in MNIST cls = Flatten()(Embedding(10, latent_size, init='glorot_normal')(image_class)) # hadamard product between z-space and a class conditional embedding h = merge([latent, cls], mode='mul') fake_image = cnn(h) return Model(input=[latent, image_class], output=fake_image)
def build_model(): from keras.models import Model from keras.layers import Input, Dense, Dropout, Activation, Flatten, Reshape from keras.layers import Convolution2D, MaxPooling2D nb_classes = 10 nb_filters = 32 pool_size = (2,2) kernel_size = (3,3) v = Input(shape=(28,28)) h = Reshape([1,28,28])(v) h = Convolution2D(nb_filters, kernel_size[0], kernel_size[1], border_mode='valid')(h) h = Activation('relu')(h) h = Convolution2D(nb_filters, kernel_size[0], kernel_size[1])(h) h = Activation('relu')(h) h = MaxPooling2D(pool_size=pool_size)(h) h = Dropout(0.25)(h) h = Flatten()(h) h = Dense(128)(h) h = Activation('relu')(h) h = Dropout(0.5)(h) h = Dense(nb_classes)(h) o = Activation('softmax')(h) model = Model(input=v, output=o) return model
def build_model(): from keras.models import Model from keras.layers import Input, Dense, Dropout, Activation, Flatten, Reshape from keras.layers import Convolution2D, MaxPooling2D nb_classes = 10 nb_filters = {{nb_filters,choice,[64,32,16,8]}} pool_size = (2,2) kernel_size = (3,3) v = Input(shape=(28,28)) h = Reshape([1,28,28])(v) h = Convolution2D(nb_filters, kernel_size[0], kernel_size[1], border_mode='valid')(h) h = Activation('relu')(h) {{cnn_layer2,maybe, h = Convolution2D(nb_filters, kernel_size[0], kernel_size[1])(h) h = Activation('relu')(h) }} h = MaxPooling2D(pool_size=pool_size)(h) h = Dropout(0.25)(h) h = Flatten()(h) h = Dense(128)(h) h = Activation('relu')(h) h = Dropout(0.5)(h) h = Dense(nb_classes)(h) o = Activation('softmax')(h) model = Model(input=v, output=o) return model
def emit_Reshape(self, IR_node): shape_str = self.shapeToStr(IR_node.IR_layer.attr["shape"].list.i) self.add_body(1, "{:<15} = layers.Reshape(name = '{}', target_shape = ({},))({})".format( IR_node.variable_name, IR_node.name, shape_str, self.parent_variable_name(IR_node)))
def _build_model(self): model = Sequential() model.add(Reshape((1, 80, 80), input_shape=(self.state_size,))) model.add(Convolution2D(32, 6, 6, subsample=(3, 3), border_mode='same', activation='relu', init='he_uniform')) model.add(Flatten()) model.add(Dense(64, activation='relu', init='he_uniform')) model.add(Dense(32, activation='relu', init='he_uniform')) model.add(Dense(self.action_size, activation='softmax')) opt = Adam(lr=self.learning_rate) model.compile(loss='categorical_crossentropy', optimizer=opt) return model
def cnn(height_a, height_q, count): question_input = Input(shape=(height_q, 1), name='question_input') embedding_q = Embedding(input_dim=count, output_dim=128, input_length=height_q)(question_input) re_q = Reshape((height_q, 128, 1), input_shape=(height_q,))(embedding_q) conv1_Q = Conv2D(128, (2, 128), activation='sigmoid', padding='valid', kernel_regularizer=regularizers.l2(0.02), kernel_initializer=initializers.random_normal(mean=0.0, stddev=0.05))(re_q) Max1_Q = MaxPooling2D((29, 1), strides=(1, 1), padding='valid')(conv1_Q) F1_Q = Flatten()(Max1_Q) Drop1_Q = Dropout(0.5)(F1_Q) predictQ = Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.02), kernel_initializer=initializers.random_normal(mean=0.0, stddev=0.05))(Drop1_Q) # kernel_initializer=initializers.random_normal(mean=0.0, stddev=0.01) answer_input = Input(shape=(height_a, 1), name='answer_input') embedding_a = Embedding(input_dim=count, output_dim=128, input_length=height_a)(answer_input) re_a = Reshape((height_a, 128, 1), input_shape=(height_a,))(embedding_a) conv1_A = Conv2D(128, (2, 128), activation='sigmoid', padding='valid', kernel_regularizer=regularizers.l2(0.02), kernel_initializer=initializers.random_normal(mean=0.0, stddev=0.05))(re_a) Max1_A = MaxPooling2D((399, 1), strides=(1, 1), padding='valid')(conv1_A) F1_A = Flatten()(Max1_A) Drop1_A = Dropout(0.5)(F1_A) predictA = Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.02), kernel_initializer=initializers.random_normal(mean=0.0, stddev=0.05))(Drop1_A) predictions = merge([predictA, predictQ], mode='dot') model = Model(inputs=[question_input, answer_input], outputs=predictions) model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)) # model.compile(loss='mean_squared_error', # optimizer='nadam') return model