[英]Python Librosa Keras Neural Network Error: Too Many Indices For Array
[英]Too Many Indices error, using recurrent neural network for natural language generation
我正在根據本文中的代碼開發一個項目: https://www.analyticsvidhya.com/blog/2020/08/build-a-natural-language-generation-nlg-system-using-pytorch/
嘗試訓練 model 時出現錯誤,如圖所示:
# train the model
train(net, batch_size = 32, epochs=1, print_every=256)
IndexError Traceback (most recent call last)
<ipython-input-68-7c9ee6264cba> in <module>
1 # train the model
----> 2 train(net, batch_size = 32, epochs=1, print_every=256)
<ipython-input-66-6d3226f72734> in train(net, epochs, batch_size, lr, clip, print_every)
17
18 # x_int, y_int, list of numbers that each represent a vocab word
---> 19 for x, y in get_batches(x_int, y_int, batch_size):
20 counter+= 1
21
<ipython-input-63-4e36dace452e> in get_batches(arr_x, arr_y, batch_size)
7 for n in range(batch_size, arr_x.shape[0], batch_size):
8 #taking row from prv to n and all columns
----> 9 x = arr_x[prv:n,:]
10 y = arr_y[prv:n,:]
11 prv = n
IndexError: too many indices for array
然后又指出了在這部分代碼的第 9 行 (x = arr_x[prv:n,:]) 上設置 arrays 的問題:
def get_batches(arr_x, arr_y, batch_size):
# iterate through the arrays
prv = 0
#Range returns a sequence of numbers
#yield returns a batch each loop (using yield keeps local variables)
for n in range(batch_size, arr_x.shape[0], batch_size):
#taking row from prv to n and all columns
x = arr_x[prv:n,:]
y = arr_y[prv:n,:]
prv = n
yield x, y
我不確定我需要更改什么/如何設置陣列以使其工作。
看起來 arr_x 是一維數組。 但是您正試圖像二維一樣解決它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.