[英]Broadcasting 4D numpy array
我面臨將處理后的 4D NumPy 數組廣播到 4D 數組的問題。 我比較了尺寸以檢查尺寸是否有問題。 我不明白出了什么問題。
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]
輸出
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)
您的打印輸出之一是cur_spectro (0, 496, 64, 1)
。
請參閱以零開頭的 numpy 形狀是什么意思它包含如何理解數組維度之一為零的情況的描述。 這實際上意味着有問題的數組是空的。
所以看起來你試圖將一個空數組復制到一個非空數組中。 由於在這種情況下這些數組無法對齊(廣播),因此會引發異常。
正如您可以在其中一條評論中閱讀一樣,分析為什么preprocess_sound函數返回為空數組。
也許您應該在代碼中添加一個檢查cur_spectro的第一個維度的大小是否為零的檢查,在這種情況下,您應該跳過有問題的指令或將一些“代理”內容復制到目標數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.