jacbz / Lofi

ML-supported lo-fi music generator
https://lofi.jacobzhang.de/
Apache License 2.0
356 stars 99 forks source link

Unable to train #14

Closed f00d4tehg0dz closed 1 year ago

f00d4tehg0dz commented 1 year ago

Hey There!

I'm running into this error "Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead." when trying with CPU tensor. Below is the error output I get. I have also tried with CUDA and get other various errors. So I'll stick with CPU for now.

python3.8 lofi2lofi_train.py Using cpu device Starting training: lofi2lofi == Epoch 0 == Scheduled sampling rate: C 0, M 0 Batch 0: Loss 5.507 (C: 2.230 + KL: 0.021 + M: 2.710 + T: 0.059 + K: 0.081 + Mo: 0.193 + V: 0.090 + E: 0.123) Validation Batch 0: Loss 5.470 (C: 2.192 + KL: 0.020 + M: 2.705 + T: 0.059 + K: 0.082 + Mo: 0.188 + V: 0.052 + E: 0.172) Epoch chord loss: 2.230, melody loss: 2.710, KL: 0.021, chord accuracy: 1.765, melody accuracy: 0.624 VALIDATION: epoch chord loss: 2.192, melody loss: 2.705, KL: 0.020, chord accuracy: 14.286, melody accuracy: 0.311 Traceback (most recent call last): File "lofi2lofi_train.py", line 14, in <module> train(dataset, model, "lofi2lofi") File "/mnt/Storage//Lofi/model/model/train.py", line 190, in train axs[0, 0].plot(epochs, train_losses_chords, label='Train', color='royalblue') File "/home/ubuntu/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 1688, in plot lines = [*self._get_lines(*args, data=data, **kwargs)] File "/home/ubuntu/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 311, in __call__ yield from self._plot_args( File "/home/ubuntu/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 494, in _plot_args y = _check_1d(xy[1]) File "/home/ubuntu/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 1348, in _check_1d return np.atleast_1d(x) File "<__array_function__ internals>", line 200, in atleast_1d File "/home/ubuntu/.local/lib/python3.8/site-packages/numpy/core/shape_base.py", line 65, in atleast_1d ary = asanyarray(ary) File "/home/ubuntu/.local/lib/python3.8/site-packages/torch/_tensor.py", line 643, in __array__ return self.numpy() RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Command I run is python3.8 lofi2lofi_train.py

f00d4tehg0dz commented 1 year ago

Was able to correct this. Change lines 190 and 207 in train.py

axs[0, 0].plot(epochs, train_losses_chords, label='Train', color='royalblue')
axs[0, 1].plot(epochs, train_losses_melodies, label='Train', color='royalblue')

to

axs[0, 0].plot(epochs, torch.tensor(train_losses_chords).detach().numpy(), label='Train', color='royalblue')
axs[0, 1].plot(epochs, torch.tensor(train_losses_chords).detach().numpy(), label='Train', color='royalblue')