Я пытаюсь выполнить следующий код, разработанный в тензорном потоке для GAN, но всякий раз, когда я выполняю его, я получаю ошибку индекса «индекс списка вне диапазона»
import pandas as pd
import numpy as np
import tensorflow as tf
import time
dataset = pd.read_csv('kagglecreditcard.csv')
is_Class0 = dataset['Class'] == 0
norm_set = dataset[is_Class0]
is_Class1 = dataset['Class'] == 1
test_set = dataset[is_Class1]
#test_labels = test_set.pop('Class')
DATA_SIZE = 5
# BATCH_SIZE = 50
# TEST_SIZE = 400
# LAYER_DENSITY = [500, 400, 300, 100, 31]
EPOCHS = 5
# noise_dim = 50
# Y_test = test_set.sample(TEST_SIZE)
# Y = tf.data.Dataset.from_tensor_slices(Y_test).batch(BATCH_SIZE)
X_train = norm_set.sample(DATA_SIZE)
norm_targets = X_train.pop('Class')
X = tf.data.Dataset.from_tensor_slices((X_train.values)).batch(1)
target = tf.data.Dataset.from_tensor_slices((norm_targets.values))
Y_train = test_set.sample(DATA_SIZE)
Y = tf.data.Dataset.from_tensor_slices((Y_train.values))
two = []
one = []
def make_generator():
inputs = tf.keras.Input(shape=(30,),name='myInput')
x = tf.keras.layers.Dense(50, activation=tf.nn.softmax)(inputs)
outputs = tf.keras.layers.Dense(30)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
def make_discriminator():
inputs = tf.keras.Input(shape=(30,))
x = tf.keras.layers.Dense(40, activation=tf.nn.softmax)(inputs)
outputs = tf.keras.layers.Dense(1, activation=tf.nn.leaky_relu)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
# This method returns a helper function to compute cross entropy loss
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)
def discriminator_loss(real_output, fake_output):
return tf.losses.binary_crossentropy(real_output, fake_output)
def generator_loss(ith_sample,fake_output):
return tf.keras.losses.kullback_leibler_divergence(ith_sample,fake_output)
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
noise = tf.random.normal([1,30])
generator = make_generator()
discriminator = make_discriminator()
gen_out = generator(noise)
disc_out = discriminator(gen_out)
gen_loss = generator_loss(two[0],gen_out)
disc_loss = discriminator_loss(one[0],disc_out)
re_out = []
fk_out = []
gn_loss = []
dc_loss = []
gen_gradients = []
disc_gradients = []
def train_step(records):
noise = tf.random.normal([1, 30])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_records = generator(noise, training=True)
real_output = discriminator(records, training=True)
fake_output = discriminator(generated_records, training=True)
re_out.append(real_output)
fk_out.append(fake_output)
gen_loss = generator_loss(records,fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
gn_loss.append(gen_loss)
dc_loss.append(disc_loss)
gradients_of_generator = gen_tape.gradient(tuple(gen_loss), generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(tuple(disc_loss), discriminator.trainable_variables)
gen_gradients.append(gradients_of_generator)
disc_gradients.append(gradients_of_discriminator)
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, epochs):
for epoch in range(epochs):
start = time.time()
for image_batch in dataset:
train_step(image_batch)
print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))
train(X, 4)
Но когда я выполняю код, я получаю ошибка:
ошибка при генерации значения убытка; следовательно, с ним не будет никакого градиента