def __init__(self, n_verts=0, n_dims=3, latent_dim=2, n_layers=2, n_units=128, relu=False, add_random_offsets=False, dropout=False): if not n_verts: raise Exception('Please provide the number of vertices `n_verts`') self.n_verts = n_verts # input vert count self.n_dims = n_dims # input dimensions self.relu = relu # whether to add relu layers in encoder/decoder self.dropout = dropout # whether to add dropout layers in encoder/decoder self.latent_dim = latent_dim self.n_layers = n_layers self.n_units = n_units self.encoder = self.build_encoder() self.decoder = self.build_decoder() # attach the encoder and decoder i = Input((self.n_verts, self.n_dims)) if add_random_offsets: random_offsets = K.cast(K.learning_phase(),'float')*K.random_uniform((K.shape(i)[0],1,3))*K.constant([[[1,1,0]]]) offset_layer = Lambda(lambda x: x + random_offsets) offset_layer.uses_learning_phase = True i_offset = offset_layer(i) else: i_offset = i z = self.encoder(i_offset) # push observations into latent space o = self.decoder(z) # project from latent space to feature space if add_random_offsets: o = Lambda(lambda x: x - random_offsets)(o) self.model = Model(inputs=[i], outputs=[o]) self.model.compile(loss='mse', optimizer=optimizers.Adam(lr=1e-4))
def PadSymmetricInTestPhase(): pad = Lambda(lambda x: K.in_train_phase( x, tf.pad(x, tf.constant([[0, 0], [2, 2], [2, 2], [0, 0]]), 'SYMMETRIC' ))) pad.uses_learning_phase = True return pad