def set_approx_model(self): """Create a model with the keras functional api """ get_custom_objects().update( {'custom_activation': Activation(self.custom_activation)}) image_input = Input(shape=(28, 28), name="image_input") flat_image = Flatten(name="flat_image") x1 = flat_image(image_input) dense_image = Dense(100, activation='linear', use_bias=False, name="dense_image") x1 = dense_image(x1) #bias bias_input = Input(shape=(400, 1), name="bias_input") flat_bias = Flatten(name="flat_bias") x2 = flat_bias(bias_input) dense_bias = Dense(100, activation=self.custom_activation, name="dense_bias") if self.use_bias == False: dense_bias.use_bias = False x2 = dense_bias(x2) merge = concatenate([x1, x2]) add_up_layer = Dense(100, activation='relu', use_bias=False, name='add_up_layer') add_up_layer.trainable = False x = add_up_layer(merge) sum_pooling_layer = Dense( 1, activation='linear', kernel_initializer=tf.keras.initializers.Ones(), name="sum_pooling", use_bias=False) sum_pooling_layer.trainable = False output = sum_pooling_layer(x) model = Model(inputs=[image_input, bias_input], outputs=output) self.model = model w_matrix = np.row_stack([np.identity(100), np.identity(100)]) for i, layer in enumerate(model.layers): if layer.name == 'add_up_layer': model.layers[i].set_weights([w_matrix]) model.compile(optimizer=SGD(learning_rate=0.0001), loss='mse', metrics=['mse'])
def cnn_2layer_fc_model(n_classes, n1=128, n2=256, dropout_rate=0.2, input_shape=(28, 28)): model_A, x = None, None x = Input(input_shape) if len(input_shape) == 2: y = Reshape((input_shape[0], input_shape[1], 1))(x) else: y = Reshape((input_shape))(x) y = Conv2D(filters=n1, kernel_size=(3, 3), strides=1, padding="same", activation=None)(y) y = BatchNormalization()(y) y = Activation("relu")(y) y = Dropout(dropout_rate)(y) y = AveragePooling2D(pool_size=(2, 2), strides=1, padding="same")(y) y = Conv2D(filters=n2, kernel_size=(3, 3), strides=2, padding="valid", activation=None)(y) y = BatchNormalization()(y) y = Activation("relu")(y) y = Dropout(dropout_rate)(y) #y = AveragePooling2D(pool_size = (2,2), strides = 2, padding = "valid")(y) y = Flatten()(y) logits = Dense(units=n_classes, activation=None, use_bias=False, kernel_regularizer=tf.keras.regularizers.l2(1e-3))(y) # valid = Dense(1, activation="sigmoid")(y) # y = Activation("softmax")(logits) valid_logit = Dense(units=1, activation=None, use_bias=False, kernel_regularizer=tf.keras.regularizers.l2(1e-3), name='dense-valid-1')(y) # 对于原始的完整模型而言,这些层不会被更新 valid_logit.trainable = False y = Activation("softmax")(logits) valid = Activation("sigmoid")(valid_logit) # valid.trainable = False model_A = Model(inputs=x, outputs=[y, valid]) model_A.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss="sparse_categorical_crossentropy", metrics=["accuracy"]) return model_A
def set_model(self): """ We create the model which is described at the top of the file """ if (self.load_model and os.path.isdir(self.storage_path)): print('Load model') self.model = tf.keras.models.load_model( pathjoin(self.storage_path, 'model.h5')) return elif (self.load_model and not os.path.isdir(self.storage_path)): print("No model file to load. Model will be fitted!") #Eingebaute Funktion, die die Uebergangsmatrix mit 1en initialisiert. ones_initializer = tf.keras.initializers.Ones() model = Sequential() model.add(Flatten(input_shape=self.train_images[0].shape)) model.add(Dense(400, activation='relu', use_bias=False)) #Kernel initializer sorgt dafuer, dass die Gewichtsmatrix die geforderte Pooling Operation realisiert custom_pooling = Dense(100, activation='relu', use_bias=False, kernel_initializer=ones_initializer) #Gewichte sollen nicht veraendert werden custom_pooling.trainable = False model.add(custom_pooling) model.add(Dense(400, activation='relu', use_bias=False)) #Gleiches wie oben, kernel wird mit 1en initialisiert und nicht trainierbar -> sum-pooling sum_pooling = Dense(1, activation='sigmoid', use_bias=False, kernel_initializer=ones_initializer) sum_pooling.trainable = False model.add(sum_pooling) #print("list of weights [0] shape: {}, [1] shape {}".format(list_of_weights[0].shape, list_of_weights[1].shape)) model.layers[2].set_weights( [np.transpose(self.getSumPoolingWeights(400, 100))]) self.model = model self.model.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['acc']) self.model.summary()
def sparse_fc_mapping(x, input_idxs): num_units = len(input_idxs) d = Dense(num_units, use_bias=False) d.trainable = False x = d(x) w = d.get_weights() w[0].fill(0) for i in range(num_units): w[0][input_idxs[i], i] = 1. d.set_weights(w) return x
from tensorflow.keras.layers import Dense, Dropout, Flatten from tensorflow.keras import Input import matplotlib.pyplot as mp from tensorflow.keras.backend import set_session import numpy as np #关闭上次未完全关闭的会话 if 'session' in locals() and tensorflow.session is not None: print('Close interactive session') tensorflow.session.close() config = tensorflow.ConfigProto() config.gpu_options.allow_growth = True #允许显存增长 set_session(tensorflow.Session(config=config)) print('GPU memory is allowed to growth.') x = Input(shape=(32, )) layer = Dense(32) layer.trainable = True y = layer(x) frozen_model = Model(x, y) # in the model below, the weights of `layer` will not be updated during training frozen_model.compile(optimizer='rmsprop', loss='mse') layer.trainable = False trainable_model = Model(x, y) # with this model the weights of the layer will be updated during training # (which will also affect the above model since it uses the same layer instance) trainable_model.compile(optimizer='rmsprop', loss='mse') data = np.random.normal(0, 1, (100, 32)) labels = np.random.normal(0, 1, (100, 32)) frozen_model.fit(data, labels,
def compile_model(self): """ Compiles the Model. Args: None Returns: None """ # the Sampling function for the VAE def sampling(args): z_mean, z_log_sigma = args epsilon = K.random_normal( shape=(K.shape(z_mean)[0], self.params.encodedSize)) return z_mean + K.exp(z_log_sigma) * epsilon # Loss function for the VAE # Loss function comprised of two parts, Cross_entropy, and # divergence def vae_loss(inputs, finalLayer): reconstruction_loss = K.sum(K.square(finalLayer - inputs)) kl_loss = - 0.5 * K.sum(1 + z_log_sigmaFull - K.square( z_meanFull) - K.square(K.exp(z_log_sigmaFull)), axis=-1) total_loss = K.mean(reconstruction_loss + kl_loss) return total_loss # Loss function for the left VAE # Loss function comprised of two parts, Cross_entropy, and # divergence def left_vae_loss(inputs, finalLayer): reconstruction_loss = K.sum(K.square(finalLayer - inputs)) kl_loss = - 0.5 * K.sum(1 + z_log_sigmaLeft - K.square( z_meanLeft) - K.square(K.exp(z_log_sigmaLeft)), axis=-1) total_loss = K.mean(reconstruction_loss + kl_loss) return total_loss # Loss function for the right VAE # Loss function comprised of two parts, Cross_entropy, and # divergence def right_vae_loss(inputs, finalLayer): reconstruction_loss = K.sum(K.square(finalLayer - inputs)) kl_loss = - 0.5 * K.sum(1 + z_log_sigmaRight - K.square( z_meanRight) - K.square(K.exp(z_log_sigmaRight)), axis=-1) total_loss = K.mean(reconstruction_loss + kl_loss) return total_loss # Define the Encoder with the Left and Right branches leftEncoderInput = Input(shape=(self.params.inputSizeLeft,)) leftEncoderFirstLayer = Dense( self.params.firstLayerSizeLeft, activation='relu')(leftEncoderInput) leftEncoderSecondLayer = Dense( self.params.secondLayerSize, activation='relu')(leftEncoderFirstLayer) rightEncoderInput = Input(shape=(self.params.inputSizeRight,)) rightEncoderFirstLayer = Dense( self.params.firstLayerSizeRight, activation='relu')(rightEncoderInput) rightEncoderSecondLayer = Dense( self.params.secondLayerSize, activation='relu')(rightEncoderFirstLayer) encoderMergeLayer = Dense( self.params.thirdLayerSize, activation='relu') leftMerge = encoderMergeLayer(leftEncoderSecondLayer) rightMerge = encoderMergeLayer(rightEncoderSecondLayer) # These different merge branches are used in different models mergedLayer = keras.layers.average([leftMerge, rightMerge]) leftMergedLayer = keras.layers.average([leftMerge, leftMerge]) rightMergedLayer = keras.layers.average( [rightMerge, rightMerge]) z_mean = Dense(self.params.encodedSize) z_log_sigma = Dense(self.params.encodedSize) # These three sets are used in differen models z_meanLeft = z_mean(leftMergedLayer) z_log_sigmaLeft = z_log_sigma(leftMergedLayer) z_meanRight = z_mean(rightMergedLayer) z_log_sigmaRight = z_log_sigma(rightMergedLayer) z_meanFull = z_mean(mergedLayer) z_log_sigmaFull = z_log_sigma(mergedLayer) zLeft = Lambda(sampling)([z_meanLeft, z_log_sigmaLeft]) zRight = Lambda(sampling)([z_meanRight, z_log_sigmaRight]) zFull = Lambda(sampling)([z_meanFull, z_log_sigmaFull]) # These are the three different models leftEncoder = Model(leftEncoderInput, zLeft) rightEncoder = Model(rightEncoderInput, zRight) self.fullEncoder = Model( [leftEncoderInput, rightEncoderInput], zFull) # Defining the Decoder with Left and Right Outputs decoderInputs = Input(shape=(self.params.encodedSize,)) decoderFirstLayer = Dense( self.params.thirdLayerSize, activation='relu')(decoderInputs) leftDecoderSecondLayer = Dense( self.params.secondLayerSize, activation='relu')(decoderFirstLayer) leftDecoderThirdLayer = Dense( self.params.firstLayerSizeLeft, activation='relu')(leftDecoderSecondLayer) leftDecoderOutput = Dense( self.params.inputSizeLeft, activation='sigmoid')(leftDecoderThirdLayer) rightDecoderSecondLayer = Dense( self.params.secondLayerSize, activation='relu')(decoderFirstLayer) rightDecoderThirdLayer = Dense( self.params.firstLayerSizeRight, activation='relu')(rightDecoderSecondLayer) rightDecoderOutput = Dense( self.params.inputSizeRight, activation='sigmoid')(rightDecoderThirdLayer) # Three different Decoders self.fullDecoder = Model( decoderInputs, [leftDecoderOutput, rightDecoderOutput]) leftDeocder = Model(decoderInputs, leftDecoderOutput) rightDecoder = Model(decoderInputs, rightDecoderOutput) # decoder.summary() # Left to Right transition outputs = self.fullDecoder(leftEncoder(leftEncoderInput)) self.leftToRightModel = Model(leftEncoderInput, outputs) # leftToRightModel.summary() # Right to Left transition outputs = self.fullDecoder(rightEncoder(rightEncoderInput)) self.rightToLeftModel = Model(rightEncoderInput, outputs) # rightToLeftModel.summary() # Full Model outputs = self.fullDecoder(self.fullEncoder( [leftEncoderInput, rightEncoderInput])) # Create the full model self.vae_model = Model( [leftEncoderInput, rightEncoderInput], outputs) lowLearnAdam = keras.optimizers.Adam( lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) self.vae_model.compile(optimizer=lowLearnAdam, loss=vae_loss) # Compile # vae_model.summary() # Freeze all shared layers leftMerge.trainable = False rightMerge.trainable = False mergedLayer.trainable = False leftMergedLayer.trainable = False rightMergedLayer.trainable = False z_meanLeft.trainable = False z_log_sigmaLeft.trainable = False z_meanRight.trainable = False z_log_sigmaRight.trainable = False z_meanFull.trainable = False z_log_sigmaFull.trainable = False zLeft.trainable = False zRight.trainable = False zFull.trainable = False decoderFirstLayer.trainable = False # Left VAE model which can't train middle outputs = leftDeocder(leftEncoder(leftEncoderInput)) self.leftModel = Model(leftEncoderInput, outputs) self.leftModel.compile( optimizer=lowLearnAdam, loss=left_vae_loss) # Right VAE model which can't train middle outputs = rightDecoder(rightEncoder(rightEncoderInput)) self.rightModel = Model(rightEncoderInput, outputs) self.rightModel.compile( optimizer=lowLearnAdam, loss=right_vae_loss) # Make shared layers trainable leftMerge.trainable = True rightMerge.trainable = True mergedLayer.trainable = True leftMergedLayer.trainable = True rightMergedLayer.trainable = True z_meanLeft.trainable = True z_log_sigmaLeft.trainable = True z_meanRight.trainable = True z_log_sigmaRight.trainable = True z_meanFull.trainable = True z_log_sigmaFull.trainable = True zLeft.trainable = True zRight.trainable = True zFull.trainable = True decoderFirstLayer.trainable = True # Make separate layers frozen leftEncoderFirstLayer.trainable = False leftEncoderSecondLayer.trainable = False rightEncoderFirstLayer.trainable = False rightEncoderSecondLayer.trainable = False leftDecoderSecondLayer.trainable = False leftDecoderThirdLayer.trainable = False leftDecoderOutput.trainable = False rightDecoderSecondLayer.trainable = False rightDecoderThirdLayer.trainable = False rightDecoderOutput.trainable = False # Define center model outputs = self.fullDecoder(self.fullEncoder( [leftEncoderInput, rightEncoderInput])) # Create the center model self.centerModel = Model( [leftEncoderInput, rightEncoderInput], outputs) self.centerModel.compile( optimizer=lowLearnAdam, loss=vae_loss) # Compile plot_model(self.fullEncoder, to_file=os.path.join('Output', str(self.params.dataSetInfo.name), 'sharedVaeFullEncoder{}_{}_{}_\ {}_{}_{}_{}.png' .format(str(self.params.numEpochs), str(self.params. firstLayerSizeLeft), str(self.params.inputSizeLeft), str(self.params. secondLayerSize), str(self.params.encodedSize), str(self.params. firstLayerSizeRight), str(self.params. inputSizeRight))), show_shapes=True)