def mnist(wgain=1.0, wdecay=0, bn_noise_std=0.0, n_units=1024): img_shape = (28, 28) n_in = np.prod(img_shape) n_encoder = n_units n_decoder = n_units n_discriminator = n_units def block(n_out): return [ affine(n_encoder, wgain, wdecay=wdecay), ex.nnet.BatchNormalization(noise_std=bn_noise_std), ex.nnet.ReLU(), ] encoder = ex.Sequential(block(n_encoder) + block(n_encoder)) decoder = ex.Sequential( block(n_decoder) + block(n_decoder) + [ affine(n_in, wgain), ex.nnet.Sigmoid(), ]) discriminator = ex.Sequential( block(n_discriminator) + block(n_discriminator) + [ affine(1, wgain), ex.nnet.Sigmoid(), ]) return encoder, decoder, discriminator
def aae_latent_encoder(n_hidden, n_discriminator=1024, recon_weight=0.025): wgain = 1.0 discriminator = ex.Sequential([ affine(n_discriminator, wgain, bias=None), ex.nnet.BatchNormalization(), ex.nnet.ReLU(), affine(n_discriminator, wgain, bias=None), ex.nnet.BatchNormalization(), ex.nnet.ReLU(), affine(1, wgain), ex.nnet.Sigmoid(), ]) latent_encoder = model.ae.AdversarialEncoder( n_hidden, discriminator, dp.AutoFiller(), recon_weight=recon_weight, ) return latent_encoder
# Setup network def affine(n_out, gain): return expr.nnet.Affine(n_out=n_out, weights=dp.AutoFiller(gain)) gain = 1.0 n_in = x_train.shape[1] n_encoder = 512 n_decoder = 512 n_hidden = 32 sigma = 0.01 encoder = expr.Sequential([ affine(n_encoder, gain), expr.nnet.ReLU(), affine(n_encoder, gain), expr.nnet.ReLU(), ]) decoder = expr.Sequential([ affine(n_decoder, gain), expr.nnet.ReLU(), affine(n_decoder, gain), expr.nnet.ReLU(), affine(n_in, gain), expr.nnet.Sigmoid(), ]) net = dp.model.VariationalAutoencoder( encoder=encoder, decoder=decoder, n_hidden=n_hidden, )
def img64x64(wgain=1.0, wdecay=1e-5, bn_mom=0.9, bn_eps=1e-6, bn_noise_std=0.0): n_channels = 3 n_encoder = 1024 n_discriminator = 512 decode_from_shape = (256, 8, 8) n_decoder = np.prod(decode_from_shape) def conv_block(n_filters, backward=False): block = [] if backward: block.append( backconv(n_filters, 5, stride=2, gain=wgain, wdecay=wdecay, bias=None)) else: block.append( conv(n_filters, 5, stride=2, gain=wgain, wdecay=wdecay, bias=None)) block.append( ex.nnet.SpatialBatchNormalization(momentum=bn_mom, eps=bn_eps, noise_std=bn_noise_std)) block.append(ex.nnet.ReLU()) return block encoder = ex.Sequential( conv_block(64) + conv_block(128) + conv_block(256) + [ ex.Flatten(), affine(n_encoder, gain=wgain, wdecay=wdecay, bias=None), ex.nnet.BatchNormalization(noise_std=bn_noise_std), ex.nnet.ReLU(), ]) decoder = ex.Sequential([ affine(n_decoder, gain=wgain, wdecay=wdecay, bias=None), ex.nnet.BatchNormalization(noise_std=bn_noise_std), ex.nnet.ReLU(), ex.Reshape((-1, ) + decode_from_shape), ] + conv_block(256, backward=True) + conv_block(128, backward=True) + conv_block(32, backward=True) + [ conv(n_channels, 5, wdecay=wdecay, gain=wgain), ex.Tanh(), ]) discriminator = ex.Sequential([ conv(32, 5, wdecay=wdecay, gain=wgain), ex.nnet.ReLU(), ] + conv_block(128) + conv_block(256) + conv_block(256) + [ ex.Flatten(), affine(n_discriminator, gain=wgain, wdecay=wdecay, bias=None), ex.nnet.BatchNormalization(noise_std=bn_noise_std), ex.nnet.ReLU(), affine(1, gain=wgain, wdecay=wdecay), ex.nnet.Sigmoid(), ]) return encoder, decoder, discriminator
x_train = np.reshape(x_train, (x_train.shape[0], -1)) x_test = np.reshape(x_test, (x_test.shape[0], -1)) # Setup network def affine(n_out): return expr.nnet.Affine(n_out=n_out, weights=dp.AutoFiller(gain=1.25)) n_in = x_train.shape[1] n_discriminator = 1024 n_hidden = 64 n_generator = 1024 generator = expr.Sequential([ affine(n_generator), expr.nnet.BatchNormalization(), expr.nnet.ReLU(), affine(n_generator), expr.nnet.BatchNormalization(), expr.nnet.ReLU(), affine(n_in), expr.nnet.Sigmoid(), ]) discriminator = expr.Sequential([ expr.nnet.Dropout(0.5), affine(n_discriminator), expr.nnet.ReLU(), expr.nnet.Dropout(0.5), affine(n_discriminator), expr.nnet.ReLU(), affine(1), expr.nnet.Sigmoid(), ])