我使用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)
在第
行temp[0:len(seq)] = seq
错误日志 -
[[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)