The bug is not in the lower level function: it is in your L_model_forward logic. A perfectly correct function can still throw errors if you pass it incorrect or (as in this case) mismatching arguments. That means your logic for handling the various different layers is not correct. The way to debug this is to start with the “dimensional analysis”, which tells you what shapes you should be getting at each layer. Here’s a thread which shows how to do that for this particular test case.