You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using BigQuery to preprocess structured data, clean missing values, and normalize
defpreprocess_data():
client=bigquery.Client()
query=""" SELECT product_id, sales, IFNULL(social_trends_score, 0) as trend_score, (sales - MIN(sales) OVER()) / (MAX(sales) OVER() - MIN(sales) OVER()) as normalized_sales FROM `project.dataset.sales_data` WHERE sales IS NOT NULL """job=client.query(query)
result=job.result() # Waits for query to finishreturnresultprocessed_data=preprocess_data()
Step 3: GAN Training with TensorFlow
importtensorflowastffromtensorflow.kerasimportlayers# GAN Generator Modeldefbuild_generator():
model=tf.keras.Sequential()
model.add(layers.Dense(128, input_dim=100)) #please fill in the empty valuemodel.add(layers.LeakyReLU(0.2))
model.add(layers.BatchNormalization())
model.add(layers.Dense(256)) #please fill in the empty valuemodel.add(layers.LeakyReLU(0.2)) #please fill in the empty valuemodel.add(layers.BatchNormalization())
model.add(layers.Dense(512)) #please fill in the empty valuemodel.add(layers.LeakyReLU(0.2)#please fill in the empty valuemodel.add(layers.BatchNormalization())
model.add(layers.Dense(784, activation='tanh')) # Fashion design as 28x28 imagemodel.add(layers.Reshape((28, 28, 1))) # Reshape to 28x28x1 (grayscale image)returnmodel# GAN Discriminator Modeldefbuild_discriminator():
model=tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28, 1))) # Flatten the input imagemodel.add(layers.Dense(512)) #please fill in the empty valuemodel.add(layers.LeakyReLU(0.2))
model.add(layers.Dense(256))
model.add(layers.LeakyReLU(0.2))
model.add(layers.Dense(1, activation='sigmoid'))
returnmodel# Compile GANdefcompile_gan(generator, discriminator):
discriminator.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
discriminator.trainable=Falsegan_input=tf.keras.layers.Input(shape=(100,)) #please fill in the empty valuegenerated_image=generator(gan_input)
gan_output=discriminator(generated_image)
gan=tf.keras.Model(gan_input, gan_output)
gan.compile(loss='binary_crossentropy', optimizer='adam')
returngan# Train GANdeftrain_gan(generator, discriminator, gan, epochs=1000, batch_size=128):
forepochinrange(epochs):
noise=tf.random.normal([batch_size, 100])
generated_images=generator.predict(noise)
real_images=processed_data.sample(batch_size)
labels_real=tf.ones((batch_size, 1)) #please fill in the empty valuelabels_fake=tf.zeros((batch_size, 1)) #please fill in the empty valued_loss_real=discriminator.train_on_batch(real_images, labels_real) #please fill in the empty valued_loss_fake=discriminator.train_on_batch(generated_images, labels_fake) #please fill in the empty valuenoise=tf.random.normal([batch_size, 100])
g_loss=gan.train_on_batch(noise, tf.ones((batch_size, 1)))
ifepoch%100==0:
print(f"Epoch {epoch}, D Loss Real: {d_loss_real}, D Loss Fake: {d_loss_fake}, G Loss: {g_loss}")
Build and train the GAN
generator=build_generator()
discriminator=build_discriminator()
gan=compile_gan(generator, discriminator) #please fill in the empty valuetrain_gan(generator, discriminator, gan)