CGAN - 条件GAN
原始GAN的缺点
代码实现
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import glob
gpu = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_memory_growth(gpu[0], True)
import tensorflow.keras.datasets.mnist as mnist
(train_image, train_label), (_, _) = mnist.load_data()
train_image = train_image / 127.5 - 1
train_image = np.expand_dims(train_image, -1)
train_image.shape
dataset = tf.data.Dataset.from_tensor_slices((train_image, train_label))
AUTOTUNE = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 256
image_count = train_image.shape[0]
noise_dim = 50
dataset = dataset.shuffle(image_count).batch(BATCH_SIZE)
def generator_model():
seed = layers.Input(shape=((noise_dim,))) # 输入 形状长度为50的向量
label = layers.Input(shape=(()))# 形状为空
# 输入维度: 因0-9一共10个字符所以长度为10 映射成长度为50 输入序列的长度为1
x = layers.Embedding(10, 50, input_length=1)(label)#嵌入层将正整数(下标)转换为具有固定大小的向量
x = layers.Flatten()(x)
x = layers.concatenate([seed, x