Tensor to list - AttributeError: 'list' object has no attribute 'size'

username: brudal
cooked: <p>I’m trying to prepare some audio data for a Dataloader but I’m struggling with a few points. At the moment my data is organised into two lists <code>inputs</code> and <code>target</code>, which are both length <code>32</code>, but have different dimensions for their elements; <code>inputs[0].shape = (8, 3690288)</code> (8 mono audio tracks) and <code>target[0].shape = (2, 3690288)</code> (a single stereo mix).</p>
<p>I’ve converted each array to a tensor by:</p>
<pre><code class="lang-auto">tensor_inputs = torch.Tensor(inputs)
tensor_target = torch.Tensor(target)
</code></pre>
<p>which seems to work: <code>tensor_inputs.shape = torch.Size([32, 8, 3690288])</code>. I’ve then tried to convert each of these to a melspectrogram:</p>
<pre><code class="lang-auto">melspectrogram = torchaudio.transforms.melspectrogram(
        sr=44100,
        n_fft=1024,
        hop_length=512,
        n_mels=64)

tensor_input_specs = []

for i in range(len(tensor_inputs)):
    spec = mel_spectrogram(tensor_inputs[i])
    tensor_input_specs.append(spec)
    
tensor_target_specs = []

for i in range(len(tensor_target)):
    spec = mel_spectrogram(tensor_target[i])
    tensor_target_specs.append(spec)
</code></pre>
<p>and then move these into a Dataloader by doing:</p>
<pre><code class="lang-auto">dataset = TensorDataset(tensor_input_specs,tensor_target_specs)
iter = DataLoader(dataset)
</code></pre>
<p>However I get the following error: <code>AttributeError: 'list' object has no attribute 'size'</code>, which I imagine is due to the fact that I’m appending the spectrograms to a list, but I’m not sure how else to achieve this.</p>
<p>Any help is appreciated.</p>

username: nivek
cooked: <p>It will be very helpful if you can provide the full stack traceback of your exception, including which line of code is triggering the exception.</p>

username: AlphaBetaGamma96
cooked: <p>Following from <a class="mention" href="/u/nivek">@nivek</a> has said, please do share a full script for your code so the problem can be identified. In addition, share the entire error message (full stacktrace) that you see when trying to run your code.</p>
<p>Your problem emerges from calling <code>.size</code> on an object which is a <code>list</code> and not <code>torch.Tensor</code> object which I suspect is somewhere within your <code>mel_spectrogram</code> object.</p>

username: brudal
cooked: <p>I solved the issue, I think the <code>.size</code> call was in building the <code>TensorDataset</code>, so i just used <code>torch.stack</code> to convert this list to a tensor:</p>
<pre><code class="lang-auto">tensor_inputs = torch.Tensor(inputs)
tensor_target = torch.Tensor(target)

tensor_input_specs = []

for i in range(len(tensor_inputs)):
    spec = mel_spectrogram(tensor_inputs[i])
    tensor_input_specs.append(spec)
    
tensor_target_specs = []

for i in range(len(tensor_target)):
    spec = mel_spectrogram(tensor_target[i])
    tensor_target_specs.append(spec)
    
tensor_input_specs= torch.stack(tensor_input_specs)
tensor_target_specs= torch.stack(tensor_target_specs)
    
dataset = TensorDataset(tensor_input_specs,tensor_target_specs)
iter = DataLoader(dataset)
</code></pre>