[英]Broadcasting 4D numpy array
I am facing an issue with broadcasting the processed 4D NumPy array into a 4D array.我面临将处理后的 4D NumPy 数组广播到 4D 数组的问题。 I have compared the dimensions to check something wrong with the dimensions.
我比较了尺寸以检查尺寸是否有问题。 I am not understanding what's wrong.
我不明白出了什么问题。
train_path = files
train_file_names = os.listdir(train_path)
train_file_names.sort(key=lambda x: int(x.partition('.')[0]))
seg_num = 60
seg_len = 2
sample_num = len(df)
data = np.zeros((seg_num*100, 496, 64, 1))
label = np.zeros((seg_num * sample_num,))
for i, file_name in enumerate(train_file_names):
sr, sound_file = wavfile.read(train_path + file_name)
# print(train_path+file_name)
length = sr * seg_len # 5s segment
range_high = len(sound_file) - length
print(range_high, length)
random_start = np.random.randint(range_high, size=seg_num)
print("i", i)
for j in range(seg_num):
cur_wav = sound_file[random_start[j]:random_start[j] + length]
cur_wav = cur_wav / 32768.0
cur_spectro = preprocess_sound(cur_wav, sr)
#print(cur_spectro.shape)
cur_spectro = np.copy(np.expand_dims(cur_spectro, 3))
print("cur_spectro",cur_spectro.shape)
print("data", data.shape)
print(data[i * seg_num + j, :, :, :].shape)
data[i * seg_num + j, :, :, :] = cur_spectro
label[i * seg_num + j] = df['class'][i]
Output输出
88200 88200
i 0
cur_spectro (0, 496, 64, 1)
data (6000, 496, 64, 1)
(496, 64, 1)
<ipython-input-226-30eefc542ce4> in loading_data(files, df)
29 print("data", data.shape)
30 print(data[i * seg_num + j, :, :, :].shape)
---> 31 data[i * seg_num + j, :, :, :] = cur_spectro
32 label[i * seg_num + j] = df['class'][i]
33 ValueError: could not broadcast input array from shape (0,496,64,1) into shape (496,64,1)
One of your printouts is cur_spectro (0, 496, 64, 1)
.您的打印输出之一是
cur_spectro (0, 496, 64, 1)
。
See What does a numpy shape starting with zero mean It contains a description how to understand the case when one of array dimensions is zero .请参阅以零开头的 numpy 形状是什么意思它包含如何理解数组维度之一为零的情况的描述。 It means actually that the array in question is empty .
这实际上意味着有问题的数组是空的。
So it looks like you attempted to copy an empty array into a non-empty array.所以看起来你试图将一个空数组复制到一个非空数组中。 As in this situation these arrays can not be aligned (broadcast), the exception is raised.
由于在这种情况下这些数组无法对齐(广播),因此会引发异常。
Just as you can read in one of comments, analyze why preprocess_sound function returns as empty array.正如您可以在其中一条评论中阅读一样,分析为什么preprocess_sound函数返回为空数组。
Maybe you should add to your code a check whether the first dimension of cur_spectro has size zero and in such case you should either skip the offending instruction or copy to the target array some "surrogate" content.也许您应该在代码中添加一个检查cur_spectro的第一个维度的大小是否为零的检查,在这种情况下,您应该跳过有问题的指令或将一些“代理”内容复制到目标数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.