You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras模型抛出未知错误(IndexError:列表索引越界):GAN音频判别器问题

Troubleshooting IndexError: List Index Out of Range in GAN Audio Discriminator

Hey there, sorry you're stuck with this vague IndexError while building your audio validation CNN discriminator for GANs. The "no line number" part makes it tricky, but let's walk through the most likely culprits based on the code snippet you shared and common GAN pitfalls:

1. Mismatched Input Tensor Shapes

First up, double-check that your generator's output tensor matches the audio_shape you defined for the discriminator.

  • Your discriminator expects inputs shaped like audio_shape (probably something like (sample_length, num_channels)), but generators often output tensors with a batch dimension upfront (e.g., (batch_size, sample_length, num_channels)). Wait, no—actually, Keras models automatically handle batch dimensions, so the real issue might be that the non-batch dimensions don't line up.
  • Add quick print statements right before feeding the generator's output to the discriminator to confirm:
    gen_output = generator(input_data)
    print(f"Generator output shape: {gen_output.shape}")
    print(f"Discriminator expected input shape: {audio_shape}")
    
    Make sure the last N dimensions (excluding batch) are an exact match.

2. Truncated Model Definition

Your code cuts off at mode...—if you didn't finish defining the discriminator's layers, this could easily cause tensor propagation errors that manifest as an IndexError. For example, if you stop after a pooling layer without flattening and adding dense layers to produce a final output (either for binary real/fake classification or multi-class audio validation), the model can't properly process the input.

Here's a quick example of how to wrap up the discriminator:

def build_audio_discriminator(audio_shape, num_classes):
    model = Sequential()
    model.add(Conv1D(32, kernel_size=(2), padding="same", input_shape=audio_shape))
    model.add(MaxPooling1D(pool_size=(2)))
    # Add remaining layers
    model.add(Conv1D(64, kernel_size=(2), padding="same", activation='relu'))
    model.add(MaxPooling1D(pool_size=(2)))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    # For multi-class validation:
    model.add(Dense(num_classes, activation='softmax'))
    # For binary real/fake check:
    # model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model

Ensure every layer connects properly and you have a final output layer that matches your task.

3. Accidental Dimension Collapse

Watch out for cases where pooling layers shrink your tensor's length to zero. For example, if your input audio length is an odd number, using a pool_size=(2) repeatedly can eventually lead to a tensor with a dimension of 0. This breaks downstream layers that try to index into the tensor.

Add debug prints to track tensor shapes after each layer:

model.add(Conv1D(32, kernel_size=(2), padding="same", input_shape=audio_shape))
model.add(Lambda(lambda x: print(f"After Conv1D: {x.shape}")))
model.add(MaxPooling1D(pool_size=(2)))
model.add(Lambda(lambda x: print(f"After MaxPool1D: {x.shape}")))

If you see any dimension hit 0, adjust your kernel/pool sizes or pad your input audio to avoid this.

4. GAN Training Pipeline Issues

If you're combining the generator and discriminator into a combined GAN model, make sure you're handling layer freezing correctly. Sometimes, when you freeze the discriminator during generator training, incorrect tensor handling can trigger indexing errors. Also, confirm you're not accidentally passing a list of tensors instead of a single tensor to the discriminator.

If you can share the full code (including how you're calling the discriminator and training loop) and the full error stack trace, we can narrow this down even further. But start with these checks—they cover 90% of similar cases I've seen.

内容的提问来源于stack exchange,提问作者user8587747

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:09:54