Converting my model from Keras to PyTorch doesn't work for some reason; produces unchanging outputs

samsepiol: <p>Hey everyone!</p>
<p>I’ve been trying to convert a set of 12 concurrent signals of length 512 each into a singular signal of length 100. I initially wanted to start with a simple Keras implementation as follows:</p>
<pre><code class="lang-python">model = Sequential()
model.add(Conv1D(filters=16, kernel_size=5, strides=2, input_shape=(512, 12)))
model.add(LeakyReLU(alpha=0.05))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Conv1D(filters=32, kernel_size=5, strides=2))
model.add(LeakyReLU(alpha=0.05))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Conv1D(filters=64, kernel_size=5, strides=2))
model.add(LeakyReLU(alpha=0.05))
model.add(Flatten())
model.add(Dense(128))
model.add(LeakyReLU(alpha=0.05))
model.add(Dropout(0.5))
model.add(Dense(100, activation='tanh'))

model.compile(loss='mean_squared_error', optimizer=Adam(lr=1e-6))
history = model.fit(x, y, epochs=100, batch_size=32, verbose=1, validation_data=(x_val, y_val))
</code></pre>
<p>Now this model performs quite poorly*, so I wanted to attempt to use a pix2pix-style approach in PyTorch:<br>
<a href="https://paste.rs/5Ff.py" rel="noopener nofollow ugc">https://paste.rs/5Ff.py</a> for model definitions<br>
<a href="https://paste.rs/FXQ.py" rel="noopener nofollow ugc">https://paste.rs/FXQ.py</a> for training routine</p>
<p>For some reason, my PyTorch generator keeps producing extremely similar outputs:</p>
<pre><code class="lang-python">netG.eval()
y_ = netG(x_val)
</code></pre>
<p>Obviously, the PyTorch pix2pix implementation is different from the simple Keras implementation, but even when I switch the <code>criterion_reconstruction</code> to MSE, disable the loss obtained from the GAN (and simply use the raw reconstruction L1 or L2 loss), use the same optimizers, the same batch size, and same dataset, PyTorch has the issue while Keras does not.</p>
<p>Have I made some glaring error somewhere?</p>
<p>Any help would be greatly appreciated.</p>
<p>*It still generates varying outputs that at least slightly resemble the expected output moire often than not. This differs from my flawed PyTorch implementation, which produces a singular constant output with less semblance to the expected output than anything the Keras model yields.</p>

J_Johnson: <p>Two things I notice:</p>
<ol>
<li>Why are you adding in a <code>reconstruction_lambda</code> scalar to the loss? Try removing that.</li>
<li>Your Discriminator uses a Sigmoid layer and you’re using BCELoss. Try removing the Sigmoid and using nn.BCEWithLogitsLoss. This takes advantage of the log-sum-exp trick for better numerical stability.</li>
</ol>

samsepiol: <p>Thanks for taking the time to reply!</p>
<p>The “reconstruction lambda” is taken from the <a href="https://arxiv.org/abs/1611.07004" rel="noopener nofollow ugc">pix2pix paper</a> (see bottom-left of p. 6), which I’m adapting to my signal generation problem. They use a lambda of 100, I found that 200 seemed to work better (but it’s all mostly garbage anyway).</p>
<p>When I attempt to directly replicate the Keras model, I’ve essentially replaced that line with just the reconstruction loss, so like the Keras model the reconstruction lambda and discriminator-based loss aren’t used.</p>
<p>As for the second suggestion, thanks! I am familiar with that but for some reason neglected to use that here.</p>
<p>In any case though, I’m still stumped as to why simply training the generator  on the L1 or MSE loss without any discriminator involvement is unable to perform similarly to the Keras model. For context, I implement a replication of the Keras model by replacing <code>lossG = loss_adversarial + reconstruction_lambda * loss_reconstruction</code> with <code>lossG = loss_reconstruction</code>, setting <code>loss_reconstruction</code> to MSE, and appropriately setting hyperparameters like batch size, number of epochs, and learning rate.</p>
<p>I feel like something is majorly wrong somewhere and I just can’t sniff it out.</p>

samsepiol: <p>For some reason, choosing a reconstruction lambda of ~50 (lower seems to perform worse, higher also is quite poor but somehow somewhat okay at ~200 as well), increasing the learning rate to a much higher value, and making the batch sizes large  (like in the hundreds) seems to break the issue. I also suspect some mode collapse is involved as the model isn’t producing very diverse outputs, and I’ll get that fixed shortly to see if that’ll help alleviate the problem of the generator failing to produce remotely diverse outputs.</p>
<p>In any case though, I still find it very weird that the PyTorch implementation (even removing the GAN/discriminator component) operates so differently from Keras.</p>