English
Languages
English
Japanese
German
Korean
Portuguese, Brazilian
French
Shortcuts

qiskit.aqua.components.neural_networks.PyTorchDiscriminator

class PyTorchDiscriminator(n_features=1, n_out=1)[source]

Discriminator based on PyTorch

Parameters
  • n_features (int) – Dimension of input data vector.

  • n_out (int) – Dimension of the discriminator’s output vector.

Raises

MissingOptionalLibraryError – Pytorch not installed

__init__(n_features=1, n_out=1)[source]
Parameters
  • n_features (int) – Dimension of input data vector.

  • n_out (int) – Dimension of the discriminator’s output vector.

Raises

MissingOptionalLibraryError – Pytorch not installed

Methods

__init__([n_features, n_out])

type n_features

int

get_label(x[, detach])

Get data sample labels, i.e. true or fake.

gradient_penalty(x[, lambda_, k, c])

Compute gradient penalty for discriminator optimization

load_model(load_dir)

Load discriminator model

loss(x, y[, weights])

Loss function

save_model(snapshot_dir)

Save discriminator model

set_seed(seed)

Set seed.

train(data, weights[, penalty, …])

Perform one training step w.r.t.

Attributes

discriminator_net

Get discriminator

property discriminator_net

Get discriminator

Returns

discriminator object

Return type

object

get_label(x, detach=False)[source]

Get data sample labels, i.e. true or fake.

Parameters
  • x (Union(numpy.ndarray, torch.Tensor)) – Discriminator input, i.e. data sample.

  • detach (bool) – if None detach from torch tensor variable (optional)

Returns

Discriminator output, i.e. data label

Return type

torch.Tensor

gradient_penalty(x, lambda_=5.0, k=0.01, c=1.0)[source]

Compute gradient penalty for discriminator optimization

Parameters
  • x (numpy.ndarray) – Generated data sample.

  • lambda (float) – Gradient penalty coefficient 1.

  • k (float) – Gradient penalty coefficient 2.

  • c (float) – Gradient penalty coefficient 3.

Returns

Gradient penalty.

Return type

torch.Tensor

load_model(load_dir)[source]

Load discriminator model

Parameters

load_dir (str) – file with stored pytorch discriminator model to be loaded

loss(x, y, weights=None)[source]

Loss function

Parameters
  • x (torch.Tensor) – Discriminator output.

  • y (torch.Tensor) – Label of the data point

  • weights (torch.Tensor) – Data weights.

Returns

Loss w.r.t to the generated data points.

Return type

torch.Tensor

save_model(snapshot_dir)[source]

Save discriminator model

Parameters

snapshot_dir (str) – directory path for saving the model

set_seed(seed)[source]

Set seed.

Parameters

seed (int) – seed

train(data, weights, penalty=True, quantum_instance=None, shots=None)[source]

Perform one training step w.r.t. to the discriminator’s parameters

Parameters
  • data (tuple) – real_batch: torch.Tensor, Training data batch. generated_batch: numpy array, Generated data batch.

  • weights (tuple) – real problem, generated problem

  • penalty (bool) – Indicate whether or not penalty function is applied to the loss function.

  • quantum_instance (QuantumInstance) – Quantum Instance (depreciated)

  • shots (int) – Number of shots for hardware or qasm execution. Not used for classical network (only quantum ones)

Returns

with Discriminator loss (torch.Tensor) and updated parameters (array).

Return type

dict