def __init__(self, params): super(CVAE, self).__init__() self.model_str = 'CVAE' self.is_cuda = False self.latent_dim = latent_dim = params.get('latent_dim', 2) self.hdim = hdim = params.get('hdim', 100) self.batchnorm = params.get('batchnorm', True) # encoder self.fc1 = fc(784 + 10, hdim) if self.batchnorm: self.bn_1 = BN(hdim, momentum=.9) self.fc_mu = fc(hdim, latent_dim) # output the mean of z if self.batchnorm: self.bn_mu = BN(latent_dim, momentum=.9) self.fc_logvar = fc(hdim, latent_dim) # output the log of the variance of z if self.batchnorm: self.bn_logvar = BN(latent_dim, momentum=.9) # decoder self.fc2 = fc(latent_dim + 10, hdim) if self.batchnorm: self.bn_2 = BN(hdim, momentum=.9) self.fc3 = fc(hdim, 784) if self.batchnorm: self.bn_3 = BN(784, momentum=.9)
def __init__( self, n_actions, hidden_dim, featurizer, game_info, player_id, #neural_network_history, #neural_network_loss, learning_rate, optimizer, tensorboard=None, grad_clip=None, shared_network=None, q_network=None, cuda=False): super(PiNetworkBN, self).__init__() self.is_cuda = cuda self.n_actions = n_actions self.featurizer = featurizer self.hidden_dim = hidden_dim hdim = self.hidden_dim # SHARE WEIGHTS assert not (shared_network is not None and q_network is not None ), "you should provide either q_network or shared_network" if q_network is not None: self.shared_network = q_network.shared_network else: if shared_network is not None: self.shared_network = shared_network else: self.shared_network = SharedNetworkBN(n_actions, hidden_dim) for i in range(19, 27): setattr(self, 'fc' + str(i), getattr(self.shared_network, 'fc' + str(i))) setattr(self, 'bn' + str(i), getattr(self.shared_network, 'bn' + str(i))) # LAST PERSONAL LAYERS self.fc27 = fc(hdim, hdim) self.bn27 = BN(hdim, momentum=.99) self.fc28 = fc(hdim, n_actions) self.bn28 = BN(n_actions, momentum=.99) self.optim = optim.Adam(self.parameters(), lr=learning_rate) if cuda: self.cuda() # for saving neural network history data self.game_info = game_info self.player_id = player_id # know the owner of the network #self.neural_network_history = neural_network_history #self.neural_network_loss = neural_network_loss self.tensorboard = tensorboard
def __init__(self, params): super(Discriminator, self).__init__() self.model_str = 'WDisc' self.h_dim = params.get('hidden_dim', 100) self.batchnorm = False # self.batchnorm = params.get('batchnorm', True) self.fc1 = fc(784, self.h_dim) self.fc2 = fc(self.h_dim, 1) if self.batchnorm: self.bn_1 = BN(self.h_dim, eps=1e-5, momentum=.9) self.bn_2 = BN(1, eps=1e-5, momentum=.9)
def __init__(self, params): super(Generator, self).__init__() self.model_str = 'WGen' self.latent_dim = params.get('latent_dim', 2) self.h_dim = params.get('hidden_dim', 100) self.batchnorm = params.get('batchnorm', True) self.fc1 = fc(self.latent_dim, self.h_dim) self.fc2 = fc(self.h_dim, 784) if self.batchnorm: self.bn_1 = BN(self.h_dim, eps=1e-5, momentum=.9) self.bn_2 = BN(784, eps=1e-5, momentum=.9)
def __init__(self, n_actions, hidden_dim, cuda=False): super(SharedNetworkBN, self).__init__() self.n_actions = n_actions self.hidden_dim = hidden_dim hdim = hidden_dim self.fc19 = fc(5 * 6 * 2, hdim) self.bn19 = BN(hdim, momentum=.99) self.fc20 = fc(5 * 6 * 2 + hdim, hdim) self.bn20 = BN(hdim, momentum=.99) self.fc21 = fc(5 * 6 * 2 + hdim, hdim) self.bn21 = BN(hdim, momentum=.99) self.fc22 = fc(5 * 6 * 2 + hdim, hdim) self.bn22 = BN(hdim, momentum=.99) self.fc23 = fc(hdim, hdim) self.bn23 = BN(hdim, momentum=.99) self.fc24 = fc(6, hdim) self.bn24 = BN(hdim, momentum=.99) self.fc25 = fc(3 * hdim, hdim) self.bn25 = BN(hdim, momentum=.99) self.fc26 = fc(hdim, hdim) self.bn26 = BN(hdim, momentum=.99) if cuda: self.cuda()
def __init__(self, params): super(PixelCNN, self).__init__() # get params self.n_layers = params.get('n_layers', 2) self.batchnorm = params.get('batchnorm', True) self.latent_dim = params.get('latent_dim', 2) self.filters = params.get('n_filters', 10) self.kernel_size = params.get('kernel_size', 3) self.padding = params.get('padding', 1) for k in range(1, self.n_layers): setattr( self, 'conv%da' % k, MaskedConv2d('A' if k == 1 else 'B', in_channels=1 if k == 1 else self.filters, out_channels=self.filters, kernel_size=self.kernel_size, stride=1, padding=self.padding, dilation=1)) setattr( self, 'conv%db' % k, MaskedConv2d('A' if k == 1 else 'B', in_channels=1 if k == 1 else self.filters, out_channels=self.filters, kernel_size=self.kernel_size, stride=1, padding=self.padding, dilation=1)) setattr(self, 'fc%da' % k, fc(self.latent_dim, 784)) setattr(self, 'fc%db' % k, fc(self.latent_dim, 784)) if self.batchnorm: setattr(self, 'bn_conv%da' % k, BN2d(self.filters, eps=1e-5, momentum=.9)) setattr(self, 'bn_conv%db' % k, BN2d(self.filters, eps=1e-5, momentum=.9)) setattr(self, 'bn_fc%da' % k, BN1d(784, eps=1e-5, momentum=.9)) setattr(self, 'bn_fc%db' % k, BN1d(784, eps=1e-5, momentum=.9)) self.final_conv = MaskedConv2d('B', in_channels=self.filters, out_channels=1, kernel_size=self.kernel_size, padding=self.padding, dilation=1) if self.batchnorm: self.bn_final_conv = BN2d(1, eps=1e-5, momentum=.9)
def __init__(self, params): super(Discriminator, self).__init__() self.model_str = 'WDisc' self.latent_dim = params.get('latent_dim', 32) self.batchnorm = params.get('batchnorm', False) self.n_filters = params.get('n_filters', 32) self.kernel_size = params.get('kernel_size', 5) self.padding = params.get('padding', 2) self.conv1 = conv(1, self.n_filters, self.kernel_size, padding=self.padding, stride=1) self.maxpool1 = maxpool(2, 2) self.conv2 = conv(self.n_filters, self.n_filters * 2, self.kernel_size, padding=self.padding, stride=1) self.maxpool2 = maxpool(2, 2) self.conv3 = conv(self.n_filters * 2, self.n_filters * 4, self.kernel_size, padding=self.padding, stride=1) self.maxpool3 = maxpool(2, 2) self.fc1 = fc(self.n_filters * 4 * 3 * 3, 1) if self.batchnorm: self.bn_conv1 = BN2d(self.n_filters, eps=1e-5, momentum=.9) self.bn_conv2 = BN2d(2 * self.n_filters, eps=1e-5, momentum=.9) self.bn_conv3 = BN2d(4 * self.n_filters, eps=1e-5, momentum=.9) self.bn_fc1 = BN1d(1, eps=1e-5, momentum=.9)
def __init__(self, params): super(PixelVAE, self).__init__() self.model_str = 'PixelVAE' self.is_cuda = False self.latent_dim = latent_dim = params.get('latent_dim', 2) self.hdim = hdim = params.get('hdim', 400) self.batchnorm = params.get('batchnorm', True) # encoder self.fc1 = fc(784, hdim) if self.batchnorm: self.bn_1 = BN(hdim, momentum=.9) self.fc_mu = fc(hdim, latent_dim) # output the mean of z if self.batchnorm: self.bn_mu = BN(latent_dim, momentum=.9) self.fc_logvar = fc(hdim, latent_dim) # output the log of the variance of z if self.batchnorm: self.bn_logvar = BN(latent_dim, momentum=.9) # decoder self.pixelcnn = PixelCNN(params)
def __init__(self, n_actions, hidden_dim, cuda=False): super(SharedNetwork, self).__init__() self.n_actions = n_actions self.hidden_dim = hidden_dim hdim = hidden_dim self.fc19 = fc(5 * 6 * 2, hdim) self.fc20 = fc(5 * 6 * 2 + hdim, hdim) self.fc21 = fc(5 * 6 * 2 + hdim, hdim) self.fc22 = fc(5 * 6 * 2 + hdim, hdim) self.fc23 = fc(hdim, hdim) self.fc24 = fc(6, hdim) self.fc25 = fc(3 * hdim, hdim) self.fc26 = fc(hdim, hdim) for i in range(19, 26): fcc = getattr(self, 'fc' + str(i)) shape = fcc.weight.data.cpu().numpy().shape fcc.weight.data = t.from_numpy( np.random.normal(0, 1 / np.sqrt(shape[0]), shape)).float() if cuda: self.cuda()
def __init__(self, params): super(Generator, self).__init__() self.model_str = 'WGen' self.latent_dim = params.get('latent_dim', 32) self.batchnorm = params.get('batchnorm', False) self.n_filters = params.get('n_filters', 32) self.kernel_size = params.get('kernel_size', 5) self.padding = params.get('padding', 2) self.fc1 = fc(self.latent_dim, self.n_filters * 4 * 7 * 7) self.deconv1 = deconv(4 * self.n_filters, 2 * self.n_filters, self.kernel_size, stride=2, padding=self.padding, output_padding=1) self.deconv2 = deconv(2 * self.n_filters, self.n_filters, self.kernel_size, stride=2, padding=self.padding, output_padding=1) self.deconv3 = deconv(self.n_filters, 1, self.kernel_size, stride=1, padding=self.padding, output_padding=0) if self.batchnorm: self.bn_fc1 = BN1d(self.n_filters * 4 * 7 * 7, eps=1e-5, momentum=.9) self.bn_deconv1 = BN2d(2 * self.n_filters, eps=1e-5, momentum=.9) self.bn_deconv2 = BN2d(self.n_filters, eps=1e-5, momentum=.9) self.bn_deconv3 = BN2d(1, eps=1e-5, momentum=.9)
def __init__(self): super(Convnet, self).__init__() self.conv1 = conv(300, 50, 3, padding=1) # self.conv2 = conv(50, 50, 3, padding=1) self.fc2 = fc(50, 2)
def __init__(self, params): super(ConVAE, self).__init__() self.model_str = 'ConVAE' self.is_cuda = False self.latent_dim = latent_dim = params.get('latent_dim', 2) self.hdim = hdim = params.get('hdim', 400) self.batchnorm = params.get('batchnorm', True) self.kernel_size = params.get('kernel_size', 3) self.padding = params.get('padding', 1) self.n_filters = params.get('n_filters', 64) # encoder self.conv1 = conv(1, self.n_filters, self.kernel_size, padding=self.padding) self.conv2 = conv(self.n_filters, self.n_filters * 2, self.kernel_size, padding=self.padding, stride=2) self.conv3 = conv(self.n_filters * 2, self.n_filters * 4, self.kernel_size, padding=self.padding, stride=2) self.fc1 = fc(7 * 7 * self.n_filters * 4, hdim) self.fc_mu = fc(hdim, latent_dim) # output the mean of z self.fc_logvar = fc(hdim, latent_dim) # output the log of the variance of z if self.batchnorm: self.bn_conv1 = BN2d(self.n_filters, momentum=.1) self.bn_conv2 = BN2d(self.n_filters * 2, momentum=.1) self.bn_conv3 = BN2d(self.n_filters * 4, momentum=.1) self.bn_fc1 = BN1d(self.hdim, momentum=.1) self.bn_mu = BN1d(latent_dim, momentum=.1) self.bn_logvar = BN1d(latent_dim, momentum=.1) # decoder self.fc2 = fc(latent_dim, 7 * 7 * self.n_filters * 4) self.deconv1 = deconv(self.n_filters * 4, self.n_filters * 2, self.kernel_size, stride=2, padding=self.padding, output_padding=1) self.deconv2 = deconv(self.n_filters * 2, self.n_filters, self.kernel_size, stride=2, padding=self.padding, output_padding=1) self.deconv3 = deconv(self.n_filters, 1, self.kernel_size, stride=1, padding=self.padding, output_padding=0) if self.batchnorm: self.bn_fc2 = BN1d(7 * 7 * self.n_filters * 4, momentum=.1) self.bn_deconv1 = BN2d(self.n_filters * 2, momentum=.1) self.bn_deconv2 = BN2d(self.n_filters, momentum=.1) self.bn_deconv3 = BN2d(1, momentum=.1)
def __init__( self, n_actions, hidden_dim, featurizer, game_info, player_id, #neural_network_history, #neural_network_loss, learning_rate, optimizer, grad_clip=None, tensorboard=None, shared_network=None, q_network=None, cuda=False, verbose=False): super(PiNetwork, self).__init__() # cuda is a reserved property for t.nn.Module self.is_cuda = cuda self.verbose = verbose self.n_actions = n_actions self.featurizer = featurizer self.hidden_dim = hidden_dim hdim = self.hidden_dim # SHARE WEIGHTS assert not (shared_network is not None and q_network is not None ), "you should provide either q_network or shared_network" if q_network is not None: self.shared_network = q_network.shared_network else: if shared_network is not None: self.shared_network = shared_network else: self.shared_network = SharedNetwork(n_actions, hidden_dim) for i in range(19, 27): setattr(self, 'fc' + str(i), getattr(self.shared_network, 'fc' + str(i))) # LAST PERSONAL LAYERS self.fc27 = fc(hdim, hdim) self.fc28 = fc(hdim, n_actions) # INIT WEIGHTS SELU for i in range(27, 29): fcc = getattr(self, 'fc' + str(i)) shape = fcc.weight.data.cpu().numpy().shape fcc.weight.data = t.from_numpy( np.random.normal(0, 1 / np.sqrt(shape[0]), shape)).float() self.grad_clip = grad_clip params = filter(lambda p: p.requires_grad, self.parameters()) if optimizer == 'adam': self.optim = optim.Adam(params, lr=learning_rate) elif optimizer == 'sgd': # 2016 paper self.optim = optim.SGD(params, lr=learning_rate) else: raise Exception( 'unsupported optimizer: use adam or sgd (lower cased)') if cuda: self.cuda() # for saving neural network history data self.game_info = game_info self.player_id = player_id # know the owner of the network #self.neural_network_history = neural_network_history #self.neural_network_loss = neural_network_loss self.tensorboard = tensorboard
def __init__(self, hdim, n_filters, cuda=False): super(CardFeaturizer1, self).__init__() self.hdim = hdim self.conv1 = conv(2, n_filters, 1) self.conv2 = conv(n_filters, n_filters, 5, padding=2) self.conv3 = conv(n_filters, n_filters, 3, padding=1) self.conv4 = conv(n_filters, n_filters, 3, dilation=2, padding=2) self.conv5 = conv(2, n_filters, 1) self.fc1 = fc(13 * n_filters * 3, hdim) self.fc2 = fc(13 * 2, hdim) self.fc3 = fc(hdim, hdim) self.fc4 = fc(4 * n_filters, hdim) self.fc5 = fc(52, hdim) self.fc6 = fc(hdim, hdim) self.fc7 = fc(52, hdim) self.fc8 = fc(hdim, hdim) self.fc9 = fc(52, hdim) self.fc10 = fc(hdim, hdim) self.fc11 = fc(3 * hdim, hdim) self.fc12 = fc(52, hdim) self.fc13 = fc(hdim, hdim) self.fc14 = fc(5 * hdim, hdim) self.fc15 = fc(hdim, hdim) # self.fc17 = fc(hdim, 9) self.fc18 = fc(hdim, 1) for i in range(1, 19): if i == 16 or i == 17: continue fcc = getattr(self, 'fc' + str(i)) shape = fcc.weight.data.cpu().numpy().shape fcc.weight.data = t.from_numpy( np.random.normal(0, 1 / np.sqrt(shape[0]), shape)).float() for i in range(1, 6): convv = getattr(self, 'conv' + str(i)) shape = convv.weight.data.cpu().numpy().shape convv.weight.data = t.from_numpy( np.random.normal(0, 1 / np.sqrt(shape[-1] * shape[-2]), shape)).float() if cuda: # configure the model params on gpu self.cuda()
def __init__(self, n_actions, hidden_dim, featurizer, game_info, player_id, learning_rate, optimizer, use_entropy_loss, grad_clip, tensorboard=None, is_target_Q=False, shared_network=None, pi_network=None, cuda=False, verbose=False): super(QNetwork, self).__init__() self.is_cuda = cuda self.verbose = verbose self.n_actions = n_actions self.featurizer = featurizer self.hidden_dim = hidden_dim hdim = self.hidden_dim assert not (shared_network is not None and pi_network is not None ), "you should provide either pi_network or shared_network" if pi_network is not None: self.shared_network = pi_network.shared_network else: if shared_network is not None: self.shared_network = shared_network else: self.shared_network = SharedNetwork(n_actions, hidden_dim) # SHARE WEIGHTS for i in range(19, 27): setattr(self, 'fc' + str(i), getattr(self.shared_network, 'fc' + str(i))) # LAST PERSONAL LAYERS self.fc27 = fc(hdim, hdim) self.fc28 = fc(hdim, n_actions) # INIT WEIGHTS SELU for i in range(27, 29): fcc = getattr(self, 'fc' + str(i)) shape = fcc.weight.data.cpu().numpy().shape fcc.weight.data = t.from_numpy( np.random.normal(0, 1 / np.sqrt(shape[0]), shape)).float() # optimizer self.grad_clip = grad_clip # exclude frozen weights (requires_grad=False) for featurizer params = filter(lambda p: p.requires_grad, self.parameters()) if optimizer == 'adam': self.optim = optim.Adam(params, lr=learning_rate) elif optimizer == 'sgd': # 2016 paper self.optim = optim.SGD(params, lr=learning_rate) else: raise Exception( 'unsupported optimizer: use adam or sgd (lower cased)') # to initialize network on gpu if cuda: self.cuda() # for saving neural network history data self.game_info = game_info self.player_id = player_id self.tensorboard = tensorboard self.use_entropy_loss = use_entropy_loss # hyperparams for loss self.beta = 0.1