【发布时间】:2020-12-07 05:22:21
【问题描述】:
我正在根据本文中的代码开发一个项目:https://www.analyticsvidhya.com/blog/2020/08/build-a-natural-language-generation-nlg-system-using-pytorch/
尝试训练模型时出现错误,如图所示:
# 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,:]) 设置数组的问题:
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
我不确定我需要更改什么/如何设置阵列以使其工作。
【问题讨论】:
标签: python tensorflow machine-learning recurrent-neural-network