<jupyter_start><jupyter_text>generator<jupyter_code># the input is picture x
def make_generator_model():
    model = tf.keras.Sequential()
    model.add(layers.Conv2D(64, (5,5), strides=(2,2), padding='same', use_bias=False,
                            input_shape=[28,28,1]))
    assert model.output_shape == (None,14,14,64) # Note:None is the size of batch
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())
    
    model.add(layers.Conv2D(128, (5,5), strides=(2,2), padding='same', use_bias=False,
                            input_shape=[28,28,1]))
    assert model.output_shape == (None, 7 ,7 ,128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())    
    
    model.add(layers.Conv2D(256, (5,5), strides=(1,1), padding='same', use_bias=False,
                            input_shape=[28,28,1]))
    assert model.output_shape == (None, 7 ,7 ,256)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())    

    model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
    assert model.output_shape == (None, 7, 7, 128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    assert model.output_shape == (None, 14, 14, 64)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
    assert model.output_shape == (None, 28, 28, 1)

    return model<jupyter_output><empty_output><jupyter_text>generator = make_generator_model()x = train_images[1]generated_image = generator(x.reshape(1,28,28,1), training=False)generated_imageplt.imshow(generated_image[0, :, :, 0], cmap='gray')<jupyter_code># discriminator<jupyter_output><empty_output><jupyter_text>def make_discriminator_model():    model = tf.keras.Sequential()    model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same',                                     input_shape=[28, 28, 1]))    model.add(layers.LeakyReLU())    model.add(layers.Dropout(0.3))    model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))    model.add(layers.LeakyReLU())    model.add(layers.Dropout(0.3))    model.add(layers.Flatten())    model.add(layers.Dense(1))    return model<jupyter_code>discriminator = make_discriminator_model()
decision = discriminator(generated_image)
print (decision)<jupyter_output><empty_output><jupyter_text>classifier<jupyter_code>classifier_path = "./models/CNN_mnist.h5"
classifier = keras.models.load_model(classifier_path)
classifier.evaluate(test_images,test_labels,verbose=1)<jupyter_output>10000/10000 [==============================] - 1s 81us/sample - loss: 0.0674 - accuracy: 0.9801<jupyter_text>loss and optimizers<jupyter_code># This method returns a helper function to compute cross entropy loss
# TODO: 这个from_logits=True用处是什么？
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)<jupyter_output><empty_output><jupyter_text>dis loss<jupyter_code>def discriminator_loss(real_output, fake_output):
    real_loss = cross_entropy(tf.ones_like(real_output), real_output)
    fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
    total_loss = real_loss + fake_loss
    return total_loss<jupyter_output><empty_output><jupyter_text>gen loss<jupyter_code>def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output), fake_output)<jupyter_output><empty_output><jupyter_text>classifier loss<jupyter_code>def classifier_loss(preds,target,is_targeted=True):
    # if it is targeted attack
    # 有一种做法是把target(integer)转化成onehot
    if is_targeted:
        return cross_entropy(preds,target)
    return -cross_entropy(preds,target)<jupyter_output><empty_output><jupyter_text>perturb loss<jupyter_code>def perturb_loss(perturbation, thresh=0.3):
    zeros = tf.zeros((tf.shape(perturbation)[0]))
    return tf.reduce_mean(
        tf.maximum(zeros, tf.norm(tf.reshape(perturbation, (tf.shape(perturbation)[0], -1)), axis=1) - thresh))<jupyter_output><empty_output><jupyter_text>total loss<jupyter_code>def total_loss(f_loss,gan_loss,perturb_loss,alpha,beta):
    """
    x:the input image
    gx:generator(x),the same as perturbation,so the fake
       images is x+gx
    real_output:dis(x)
    fake_output:dis(x+gx)
    preds:classifier(x+gx)
    target:the target of targeted attack
    is_targeted:whether it is targeted attack or not
    """
    alpha = 1
    beta = 5
    total_loss = f_loss + alpha*gan_loss + beta*perturb_loss
    
    return total_loss<jupyter_output><empty_output><jupyter_text>optimizer<jupyter_code>generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)<jupyter_output><empty_output><jupyter_text>saving<jupyter_code>checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,
                                 discriminator_optimizer=discriminator_optimizer,
                                 generator=generator,
                                 discriminator=discriminator)
EPOCHS = 50
num_examples_to_generate = 16

# We will reuse this seed overtime (so it's easier)
# to visualize progress in the animated GIF)<jupyter_output><empty_output><jupyter_text>images = []labels = []for image_batch,label_batch in zip(train_images_dataset,train_labels_dataset):    images = image_batch    labels = label_batchimages.shape,labels.shape<jupyter_code># Notice the use of `tf.function`
# This annotation causes the function to be "compiled".
@tf.function
def train_step(images,labels):

    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        # pdb.set_trace()
        perturbation = generator(images, training=True)
        generated_images = images + perturbation

        real_output = discriminator(images, training=True)
        fake_output = discriminator(generated_images, training=True)
        preds = classifier(generated_images)
        
        
        class_loss = classifier_loss(preds,labels,is_targeted=False)
        gen_loss = generator_loss(fake_output)
        pert_loss = perturb_loss(perturbation,thresh=0.3)
        
        all_loss = total_loss(class_loss,gen_loss,pert_loss,alpha=1,beta=2)
        disc_loss = discriminator_loss(real_output, fake_output)

    gradients_of_generator = gen_tape.gradient(all_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
def train(dataset,labels, epochs):
  for epoch in range(epochs):
    start = time.time()
    
    wxz_i = 1
    for image_batch,label_batch in zip(dataset,labels):
      print(wxz_i)
      train_step(image_batch,label_batch)
      wxz_i = wxz_i + 1

    # Produce images for the GIF as we go
    display.clear_output(wait=True)
    generate_and_save_images(generator,
                             epoch + 1,
                             seed)

    # Save the model every 15 epochs
    if (epoch + 1) % 15 == 0:
      checkpoint.save(file_prefix = checkpoint_prefix)

    print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))

  # Generate after the final epoch
  display.clear_output(wait=True)
  generate_and_save_images(generator,
                           epochs,
                           seed)
def generate_and_save_images(model, epoch, test_input):
  # Notice `training` is set to False.
  # This is so all layers run in inference mode (batchnorm).
  predictions = model(test_input, training=False)

  fig = plt.figure(figsize=(4,4))

  for i in range(predictions.shape[0]):
      plt.subplot(4, 4, i+1)
      plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
      plt.axis('off')

  plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
  plt.show()
train(train_images_dataset,train_labels_dataset,EPOCHS)<jupyter_output>1
{'fraction_non_alphanumeric': 0.12267568220236658, 'fraction_numerical': 0.023907268775658054, 'mean_word_length': 4.343870967741935, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '622792', 'license': 'MIT', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2775, 'n_words': 455, 'path': 'advgan/advgan.ipynb', 'repo_name': 'johnsonkee/graduate_design', 'size': '8282'}