|
|
|
@ -22,15 +22,16 @@ from ....layer.basic import Dense, OneHot
|
|
|
|
|
|
|
|
|
|
class ConditionalVAE(Cell):
|
|
|
|
|
r"""
|
|
|
|
|
Conditional Variational auto-encoder (CVAE).
|
|
|
|
|
Conditional Variational Auto-Encoder (CVAE).
|
|
|
|
|
|
|
|
|
|
The difference with VAE is that CVAE uses labels information.
|
|
|
|
|
see more details in `<http://papers.nips.cc/paper/5775-learning-structured-output-representation-using-deep-
|
|
|
|
|
conditional-generative-models>`.
|
|
|
|
|
see more details in `Learning Structured Output Representation using Deep Conditional Generative Models
|
|
|
|
|
<http://papers.nips.cc/paper/5775-learning-structured-output-representation-using-deep-conditional-
|
|
|
|
|
generative-models>`_.
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
When define the encoder and decoder, the shape of the encoder's output tensor and decoder's input tensor
|
|
|
|
|
should be math:`(N, hidden_size)`.
|
|
|
|
|
should be :math:`(N, hidden_size)`.
|
|
|
|
|
The latent_size should be less than or equal to the hidden_size.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
@ -42,7 +43,7 @@ class ConditionalVAE(Cell):
|
|
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
|
- **input_x** (Tensor) - the same shape as the input of encoder.
|
|
|
|
|
- **input_y** (Tensor) - the tensor of the target data, the shape is math:`(N, 1)`.
|
|
|
|
|
- **input_y** (Tensor) - the tensor of the target data, the shape is :math:`(N, 1)`.
|
|
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
|
- **output** (tuple) - (recon_x(Tensor), x(Tensor), mu(Tensor), std(Tensor)).
|
|
|
|
@ -100,13 +101,13 @@ class ConditionalVAE(Cell):
|
|
|
|
|
Args:
|
|
|
|
|
sample_y (Tensor): Define the label of sample, int tensor.
|
|
|
|
|
generate_nums (int): The number of samples to generate.
|
|
|
|
|
shape(tuple): The shape of sample, it should be math:`(generate_nums, C, H, W)` or math:`(-1, C, H, W)`.
|
|
|
|
|
shape(tuple): The shape of sample, it should be (generate_nums, C, H, W) or (-1, C, H, W).
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Tensor, the generated sample.
|
|
|
|
|
"""
|
|
|
|
|
generate_nums = check_int_positive(generate_nums)
|
|
|
|
|
if not isinstance(shape, tuple) or len(shape) != 4 or shape[0] != generate_nums or shape[0] != -1:
|
|
|
|
|
if not isinstance(shape, tuple) or len(shape) != 4 or (shape[0] != -1 and shape[0] != generate_nums):
|
|
|
|
|
raise ValueError('The shape should be (generate_nums, C, H, W) or (-1, C, H, W).')
|
|
|
|
|
sample_z = self.normal((generate_nums, self.latent_size), self.to_tensor(0.0), self.to_tensor(1.0), seed=0)
|
|
|
|
|
sample_y = self.one_hot(sample_y)
|
|
|
|
|