0

我正在尝试生成新信号,例如我的 29000 个信号和 2000 个样本的数据集。我正在使用 biLSTM-CNN 来训练我的模型。但是在训练之后,损失为 0,准确度为 0。我已经将数据拆分为批处理和混洗测试和训练集。我试图修改模型结构,但结果是一样的。你能帮助我吗?

我的代码如下:

import tensorflow as tf
from tensorflow.keras import layers, regularizers
import matplotlib.pyplot as plt
import numpy as np
import random

from IPython import display
from tqdm import tqdm

from scipy.io import loadmat

def prepare_data(dim):
    data = loadmat('finaliconzeri.mat')
    data = data['car_finale']
    data = np.reshape(data, (data.shape[0], 1, data.shape[1]))
    print('Data shape:', data.shape)

    #shape is [29000 1 2000]
    data = np.array(data, dtype='float64')
    train_size = int(data.shape[0] * 0.9)
    test_size = data.shape[0] - train_size
    print(train_size, test_size)

    train_dataset = tf.data.Dataset.from_tensor_slices(data[:train_size]).shuffle(train_size).batch(BATCH_SIZE)
    test_dataset  = tf.data.Dataset.from_tensor_slices(data[train_size:]).shuffle(test_size).batch(4)

    seed = tf.random.normal(dim)
    
    return seed, train_dataset, test_dataset
def generatore():
    model = tf.keras.Sequential()
    model.add(layers.InputLayer(input_shape=(400,10),dtype=tf.dtypes.float64))
    model.add(layers.Bidirectional(layers.LSTM(100,return_sequences=True),dtype='float64'))
    model.add(layers.Conv1DTranspose(filters=90, kernel_size=5, strides=5, padding='valid',activation='relu'))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(1,activation='tanh'))
    model.add(layers.Permute((2, 1)))
    
    return model


def discriminatore():
    model = tf.keras.Sequential()
   
    model.add(layers.InputLayer(input_shape=(1,2000),dtype=tf.dtypes.float64))
    model.add(layers.Permute((2, 1)))
    
    model.add(layers.Conv1D(filters=10, kernel_size=90, strides=5, padding='valid',activation='relu',dtype='float64'))
    model.add(layers.MaxPool1D(pool_size=36,strides=3,dtype='float64'))
    model.add(layers.Conv1D(filters=5, kernel_size=24, strides=3, padding='valid',activation='relu'))
    model.add(layers.MaxPool1D(pool_size=16,strides=3,dtype='float64'))
    model.add(layers.Flatten(dtype='float64'))
    model.add(layers.Dense(25,dtype='float64'))
    model.add(layers.Softmax(dtype='float64'))
    model.add(layers.Dense(1,activation='sigmoid',dtype='float64'))
    return model

generator     = generatore()
discriminator = discriminatore()

disc_log_dir = 'logs/gradient_tape/disc_loss'
disc_losses = tf.keras.metrics.Mean('disc_loss', dtype=tf.float64)
disc_losses_list = []

fake_disc_accuracy = tf.keras.metrics.BinaryAccuracy('fake_disc_accuracy')
real_disc_accuracy = tf.keras.metrics.BinaryAccuracy('real_disc_accuracy')
fake_disc_accuracy_list, real_disc_accuracy_list = [], []

gen_log_dir = 'logs/gradient_tape/gen_loss'
gen_losses = tf.keras.metrics.Mean('gen_loss', dtype=tf.float64)
gen_losses_list = []


cross_entropy = tf.keras.losses.BinaryCrossentropy()

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

def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output), fake_output)

generator_optimizer = tf.keras.optimizers.Adam()
discriminator_optimizer = tf.keras.optimizers.Adam()

disc_steps = 1

@tf.function
def train_step(real_sig, dim):
    noise = tf.random.normal(dim)

    for i in range(disc_steps):
        with tf.GradientTape() as disc_tape:
            generated_sig = generator(noise, training=True)

            real_output = discriminator(real_sig, training=True)
            fake_output = discriminator(generated_sig, training=True)

            disc_loss = discriminator_loss(real_output, fake_output)
        
        gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
        discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
  
    with tf.GradientTape() as gen_tape:
        generated_sig = generator(noise, training=True)
        fake_output = discriminator(generated_sig, training=True)

        gen_loss = generator_loss(fake_output)
    
    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

def train(dataset, epochs, dim):
    for epoch in tqdm(range(epochs)):
    
        for batch in dataset:
            train_step(batch, dim)
            
        disc_losses_list.append(disc_losses.result().numpy())
        gen_losses_list.append(gen_losses.result().numpy())
        
        fake_disc_accuracy_list.append(fake_disc_accuracy.result().numpy())
        real_disc_accuracy_list.append(real_disc_accuracy.result().numpy())

    
BATCH_SIZE = 8
noise_dim = [BATCH_SIZE,400,10]

seed, train_dataset, test_dataset = prepare_data(noise_dim)
train(train_dataset, 100, noise_dim)

4

0 回答 0