Create_training_example Error with initializing previous_segments

The instructions mention:

# Step 2: Initialize segment times as empty list (≈ 1 line)
    previous_segments = ...

Yet, it seems like an empty list is not of a tuple format with channels which is what the grader function is looking for.

AttributeError                            Traceback (most recent call last)
<ipython-input-23-cfc8a2ca224c> in <module>
     16     print("\033[92m All tests passed!")
---> 18 create_training_example_test(create_training_example)

<ipython-input-23-cfc8a2ca224c> in create_training_example_test(target)
      2 def create_training_example_test(target):
      3     np.random.seed(18)
----> 4     x, y = target(backgrounds[0], activates, negatives, 1375)
      6     assert type(x) == np.ndarray, "Wrong type for x"

<ipython-input-22-ea45a7eb768a> in create_training_example(background, activates, negatives, Ty)
     37     for random_activate in random_activates: # @KEEP
     38         # Insert the audio clip on the background
---> 39         background, segment_time = insert_audio_clip(background, random_activates, previous_segments)
     40         # Retrieve segment_start and segment_end from segment_time
     41         segment_start, segment_end = segment_time

<ipython-input-15-6fed76cfa826> in insert_audio_clip(background, audio_clip, previous_segments)
     40     ### END CODE HERE ###
     41         # Step 4: Superpose audio segment and background
---> 42         new_background = background.overlay(audio_clip, position = segment_time[0])
     43     else:
     44         #print("Timeouted")

/opt/conda/lib/python3.7/site-packages/pydub/ in overlay(self, seg, position, loop, times, gain_during_overlay)
   1151         output = StringIO()
-> 1153         seg1, seg2 = AudioSegment._sync(self, seg)
   1154         sample_width = seg1.sample_width
   1155         spawn = seg1._spawn

/opt/conda/lib/python3.7/site-packages/pydub/ in _sync(cls, *segs)
    434     @classmethod
    435     def _sync(cls, *segs):
--> 436         channels = max(seg.channels for seg in segs)
    437         frame_rate = max(seg.frame_rate for seg in segs)
    438         sample_width = max(seg.sample_width for seg in segs)

/opt/conda/lib/python3.7/site-packages/pydub/ in <genexpr>(.0)
    434     @classmethod
    435     def _sync(cls, *segs):
--> 436         channels = max(seg.channels for seg in segs)
    437         frame_rate = max(seg.frame_rate for seg in segs)
    438         sample_width = max(seg.sample_width for seg in segs)

AttributeError: 'list' object has no attribute 'channels'

All previous tests pass successfully.

1 Like

@TMosh adding a mentor here so that you can see this post. Thank you!

The mentors can see all the posts. You don’t need to tag us specifically.

Sorry about that!

I haven’t been able to figure out the solution yet. Can you offer an explanation?

It would help a lot if you mentioned the week number and the assignment number (or name).

Trigger Word Detection, and the exact function name is create_training_example

So you’re asking about Course 5, Week 3, Assignment 2?

The expected code there is:

previous_segments = []

Or you can create an empty list:

previous_segments = list()

That is what I did, I just didnt paste the entire line due to honor code. But with the empty list initialization the error that comes out is AttributeError: 'list' object has no attribute 'channels' as pasted above.

So what else could be the issue?

I’d guess there is an error in your insert_audio_clip() function, because the error trace labeled “39” has a call to that function.

Tests seem to pass. Is there an issue not caught by the unit tests?

Passing a test does not mean your code is perfect. The unit tests don’t check for every possible error.

@sgnv, did u able to find out the issue. thanks in advance

I’m running into the same error. I’m not really doing anything in insert_audio_clip other than


It does seem that the empty list scenario is somehow broken. Any insights would be appreciated.

A little extra detail, it is blowing up in insert_audio_clip but at part of the provided code, specifically; new_background = background.overlay(audio_clip, position = segment_time[0])

Since this thread is a year old, I recommend you start a new one. It will attract more attention from the newer mentors.

I am currently offline and cant easily access the notebooks.