值错误:无法从shap广播输入数组

2024-09-22 16:30:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用seq2seq模型进行印地语到英语文本的翻译。我不太熟悉keras或深度学习。在探索seq2seq模型时,我遇到了这个例子。在

https://github.com/karimkhanp/Seq2Seq/blob/master/seq2seq/seq2seq.py

在运行这个程序时,我出错了

ValueError: could not broadcast input array from shape (6) into shape (1,10)

在线

^{pr2}$

错误日志-

[[4000, 4000, 4000, 4000, 4000, 4000]]

Traceback (most recent call last):
  File "seq2seq.py", line 92, in <module>
    Seq2seq.encode()
  File "seq2seq.py", line 58, in encode
    temp[0:len(seq)] = seq
ValueError: could not broadcast input array from shape (6) into shape (1,10)

代码:

def encode(self):
    #Encodes input sentence into fixed length vector
    #print("Enter sentence in hindi")
    inp = raw_input().decode("utf-8")
    tokens = inp.split()
    seq = []
    for token in tokens:
        if token in self.proproces.vocab_tar:
            seq.append(self.proproces.vocab_tar[token])
        else:
            token = "UNK"
            seq.append(self.proproces.vocab_tar[token])
    #seq = map(lambda x:self.proproces.vocab_hind[x], tokens)
    # Normalize seq to maxlen
    X = []
    X.append(seq)
    print(X)
    temp = pad_sequences(X, maxlen=self.maxlen)
    temp[0:len(seq)] = seq
    print(len(temp))
    temp = np.asarray(temp).reshape(128,)
    print(temp.shape)
    prob = model.predict_on_batch(temp)#, batch_size=1, verbose=0)
    translated = self.decode(prob)
    print("Tranlated is", translated)

哪里是尺寸不匹配。在

原始代码有temp = sequence.pad_sequences(X, maxlen=self.maxlen),我将其转换为temp = pad_sequences(X, maxlen=self.maxlen)


Tags: inpyselftokeninputtempseqencode