Let's use TensorFlow and Keras to build a basic GAN. The MNIST dataset contains handwritten numbers, and this GAN will use them to create pictures.
1. Import Libraries
import tensorflow as tf from tensorflow.keras import layers, models import numpy as np import matplotlib.pyplot as plt |
TensorFlow and Keras are needed to build and train our neural networks, so we start by importing them. We also bring in NumPy to work with data arrays and Matplotlib to see the pictures that were made.
2. Load and Preprocess the Data
(x_train, _), (_, _) = tf.keras.datasets.mnist.load_data() x_train = x_train / 255.0 x_train = np.expand_dims(x_train, axis=-1) |
We load the MNIST dataset, which is made up of numbers that were written by hand. To make training easier, the values of the pixels are set to be in the range [0, 1]. The input is then changed so that it has a channel dimension, which makes it work with our model's convolutional layers.
3. Define the Generator
def build_generator(): model = models.Sequential() model.add(layers.Dense(128, activation='relu', input_dim=100)) model.add(layers.BatchNormalization()) model.add(layers.Dense(256, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(512, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(28 * 28 * 1, activation='tanh')) model.add(layers.Reshape((28, 28, 1))) return model |
The generator is a model that works in a certain order and is given a random noise vector. It has several thick layers with batch normalization and ReLU activations that help keep training stable. The last layer uses a tanh activation to make output numbers in the [-1, 1] range, which can be used to make pictures. The result is changed so that it fits the MNIST images' size (28x28x1).
4. Define the Discriminator
def build_discriminator(): model = models.Sequential() model.add(layers.Flatten(input_shape=(28, 28, 1))) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) return model |
It is also said that the discriminator is a sequential model. The pictures that are fed to it are flattened and then sent through several thick layers with ReLU activations. A sigmoid activation is used in the last layer to make a chance score that shows whether the picture input is real or fake.
5. Compile the Models
discriminator = build_discriminator() discriminator.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) generator = build_generator() gan_input = layers.Input(shape=(100,)) generated_image = generator(gan_input) discriminator.trainable = False gan_output = discriminator(generated_image) gan = models.Model(gan_input, gan_output) gan.compile(optimizer='adam', loss='binary_crossentropy') |
To make the discriminator, we use the binary cross-entropy loss function and the Adam algorithm to build it independently. Then, we put the generator and discriminator together to make the GAN model. During the GAN training, the discriminator's weights are set to "non-trainable" so that only the generator is changed. The binary cross-entropy loss is added to the GAN model to find the difference between real and fake classes.
6. Training the GAN
def train_gan(gan, generator, discriminator, epochs=10000, batch_size=128): (x_train, _), (_, _) = tf.keras.datasets.mnist.load_data() x_train = x_train / 255.0 x_train = np.expand_dims(x_train, axis=-1) real = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) for epoch in range(epochs): idx = np.random.randint(0, x_train.shape[0], batch_size) real_images = x_train[idx] noise = np.random.normal(0, 1, (batch_size, 100)) generated_images = generator.predict(noise) d_loss_real = discriminator.train_on_batch(real_images, real) d_loss_fake = discriminator.train_on_batch(generated_images, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) noise = np.random.normal(0, 1, (batch_size, 100)) g_loss = gan.train_on_batch(noise, real) if epoch % 1000 == 0: print(f"Epoch: {epoch}, D Loss: {d_loss}, G Loss: {g_loss}") save_generated_images(epoch, generator) def save_generated_images(epoch, generator, examples=10, dim=(1, 10), figsize=(10, 1)): noise = np.random.normal(0, 1, (examples, 100)) generated_images = generator.predict(noise) generated_images = 0.5 * generated_images + 0.5 plt.figure(figsize=figsize) for i in range(examples): plt.subplot(dim[0], dim[1], i + 1) plt.imshow(generated_images[i, :, :, 0], cmap='gray') plt.axis('off') plt.tight_layout() plt.savefig(f"gan_generated_image_epoch_{epoch}.png") plt.close() train_gan(gan, generator, discriminator) |
To explain:
Setting up the training: The training function train_gan creates the real and fake labels and sets up the training loop a certain number of times. For the discriminator, the real and fake arrays are used as their names.
Batch Training: A random group of real pictures is chosen from the training material for each epoch. A group of noise vectors are made and fed through the generator to make fake pictures.
Train the Discriminator: Both real and fake pictures are used to train the discriminator. The total discriminator loss is found by adding up the losses from the two training steps.
Train the Generator: The GAN model is used to train the generator. The GAN is fed noise vectors, and the loss is found by comparing them to the true labels. This tells the creator to make pictures that look more real.
Keeping track of progress and saving images: The function prints out the current loss every 1000 epochs and uses the save_generated_images function to save the generated images. This function takes noise vectors and turns them into a set of pictures that can be looked at.
You can make a basic GAN that makes pictures like the ones in the MNIST collection by following these steps and reading the code. This example shows how adversarial training works and how the generator and discriminator work together to make the created data better.
Let's use TensorFlow and Keras to build a basic GAN. The MNIST dataset contains handwritten numbers, and this GAN will use them to create pictures.
1. Import Libraries
import tensorflow as tf from tensorflow.keras import layers, models import numpy as np import matplotlib.pyplot as plt |
TensorFlow and Keras are needed to build and train our neural networks, so we start by importing them. We also bring in NumPy to work with data arrays and Matplotlib to see the pictures that were made.
2. Load and Preprocess the Data
(x_train, _), (_, _) = tf.keras.datasets.mnist.load_data() x_train = x_train / 255.0 x_train = np.expand_dims(x_train, axis=-1) |
We load the MNIST dataset, which is made up of numbers that were written by hand. To make training easier, the values of the pixels are set to be in the range [0, 1]. The input is then changed so that it has a channel dimension, which makes it work with our model's convolutional layers.
3. Define the Generator
def build_generator(): model = models.Sequential() model.add(layers.Dense(128, activation='relu', input_dim=100)) model.add(layers.BatchNormalization()) model.add(layers.Dense(256, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(512, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(28 * 28 * 1, activation='tanh')) model.add(layers.Reshape((28, 28, 1))) return model |
The generator is a model that works in a certain order and is given a random noise vector. It has several thick layers with batch normalization and ReLU activations that help keep training stable. The last layer uses a tanh activation to make output numbers in the [-1, 1] range, which can be used to make pictures. The result is changed so that it fits the MNIST images' size (28x28x1).
4. Define the Discriminator
def build_discriminator(): model = models.Sequential() model.add(layers.Flatten(input_shape=(28, 28, 1))) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) return model |
It is also said that the discriminator is a sequential model. The pictures that are fed to it are flattened and then sent through several thick layers with ReLU activations. A sigmoid activation is used in the last layer to make a chance score that shows whether the picture input is real or fake.
5. Compile the Models
discriminator = build_discriminator() discriminator.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) generator = build_generator() gan_input = layers.Input(shape=(100,)) generated_image = generator(gan_input) discriminator.trainable = False gan_output = discriminator(generated_image) gan = models.Model(gan_input, gan_output) gan.compile(optimizer='adam', loss='binary_crossentropy') |
To make the discriminator, we use the binary cross-entropy loss function and the Adam algorithm to build it independently. Then, we put the generator and discriminator together to make the GAN model. During the GAN training, the discriminator's weights are set to "non-trainable" so that only the generator is changed. The binary cross-entropy loss is added to the GAN model to find the difference between real and fake classes.
6. Training the GAN
def train_gan(gan, generator, discriminator, epochs=10000, batch_size=128): (x_train, _), (_, _) = tf.keras.datasets.mnist.load_data() x_train = x_train / 255.0 x_train = np.expand_dims(x_train, axis=-1) real = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) for epoch in range(epochs): idx = np.random.randint(0, x_train.shape[0], batch_size) real_images = x_train[idx] noise = np.random.normal(0, 1, (batch_size, 100)) generated_images = generator.predict(noise) d_loss_real = discriminator.train_on_batch(real_images, real) d_loss_fake = discriminator.train_on_batch(generated_images, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) noise = np.random.normal(0, 1, (batch_size, 100)) g_loss = gan.train_on_batch(noise, real) if epoch % 1000 == 0: print(f"Epoch: {epoch}, D Loss: {d_loss}, G Loss: {g_loss}") save_generated_images(epoch, generator) def save_generated_images(epoch, generator, examples=10, dim=(1, 10), figsize=(10, 1)): noise = np.random.normal(0, 1, (examples, 100)) generated_images = generator.predict(noise) generated_images = 0.5 * generated_images + 0.5 plt.figure(figsize=figsize) for i in range(examples): plt.subplot(dim[0], dim[1], i + 1) plt.imshow(generated_images[i, :, :, 0], cmap='gray') plt.axis('off') plt.tight_layout() plt.savefig(f"gan_generated_image_epoch_{epoch}.png") plt.close() train_gan(gan, generator, discriminator) |
To explain:
Setting up the training: The training function train_gan creates the real and fake labels and sets up the training loop a certain number of times. For the discriminator, the real and fake arrays are used as their names.
Batch Training: A random group of real pictures is chosen from the training material for each epoch. A group of noise vectors are made and fed through the generator to make fake pictures.
Train the Discriminator: Both real and fake pictures are used to train the discriminator. The total discriminator loss is found by adding up the losses from the two training steps.
Train the Generator: The GAN model is used to train the generator. The GAN is fed noise vectors, and the loss is found by comparing them to the true labels. This tells the creator to make pictures that look more real.
Keeping track of progress and saving images: The function prints out the current loss every 1000 epochs and uses the save_generated_images function to save the generated images. This function takes noise vectors and turns them into a set of pictures that can be looked at.
You can make a basic GAN that makes pictures like the ones in the MNIST collection by following these steps and reading the code. This example shows how adversarial training works and how the generator and discriminator work together to make the created data better.
Sequence of prompts stored as linked records or documents.
It helps with filtering, categorization, and evaluating generated outputs.
As text fields, often with associated metadata and response outputs.
Combines keyword and vector-based search for improved result relevance.
Yes, for storing structured prompt-response pairs or evaluation data.
Combines database search with generation to improve accuracy and grounding.
Using encryption, anonymization, and role-based access control.
Using tools like DVC or MLflow with database or cloud storage.
Databases optimized to store and search high-dimensional embeddings efficiently.
They enable semantic search and similarity-based retrieval for better context.
They provide organized and labeled datasets for supervised trainining.
Track usage patterns, feedback, and model behavior over time.
Enhancing model responses by referencing external, trustworthy data sources.
They store training data and generated outputs for model development and evaluation.
Removing repeated data to reduce bias and improve model generalization.
Yes, using BLOB fields or linking to external model repositories.
With user IDs, timestamps, and quality scores in relational or NoSQL databases.
Using distributed databases, replication, and sharding.
NoSQL or vector databases like Pinecone, Weaviate, or Elasticsearch.
Pinecone, FAISS, Milvus, and Weaviate.
With indexing, metadata tagging, and structured formats for efficient access.
Text, images, audio, and structured data from diverse databases.
Yes, for representing relationships between entities in generated content.
Yes, using structured or document databases with timestamps and session data.
They store synthetic data alongside real data with clear metadata separation.
Copyrights © 2024 letsupdateskills All rights reserved