【问题标题】:Too Many Indices error, using recurrent neural network for natural language generationToo Many Indices 错误,使用循环神经网络进行自然语言生成
【发布时间】: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


    【解决方案1】:

    看起来 arr_x 是一维数组。但你正试图像二维一样处理它

    【讨论】:

    • 感谢您的回复!这也是我的想法,但我不确定如何/需要更改哪些内容才能使其成为二维数组。我对 NLG 和 RNN 很陌生,但一直在寻找如何解决这个问题。
    • 有很多方法(tf.reshape()tf.stack() 等)取决于你想做什么
    • 感谢您的回复。由于它是一个 numpy 数组,因此尝试执行 np.reshape() 并且也无法使其正常工作,尽管我可能没有在括号内使用正确的值。
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 2019-10-02
    • 2017-02-19
    • 2017-07-08
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多