1. 程式人生 > >Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (37, 30)

Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (37, 30)

吳恩達深度學習作業Neural+machine+translation+with+attention+-+yangdaxia執行報錯:

原因如下:模型要求輸入維度為(m, 30, 37)

實際輸入維度為(37, 30)

所以經過兩部處理:

1,轉置transpose()

2.增加一維

EXAMPLES = ['3 May 1979', '5 April 09', '21th of August 2016', 'Tue 10 Jul 2007', 'Saturday May 9 2018', 'March 3 2001', 'March 3rd 2001', '1 March 2001']
for example in EXAMPLES:
    
    source = string_to_int(example, Tx, human_vocab)
    source = np.array(list(map(lambda x: to_categorical(x, num_classes=len(human_vocab)), source))).swapaxes(0,1)
    source = source.transpose()#交換兩個軸
    source = np.expand_dims(source, axis=0)#增加一維軸


    prediction = model.predict([source, s0, c0])
    prediction = np.argmax(prediction, axis = -1)
    output = [inv_machine_vocab[int(i)] for i in prediction]
    
    print("source:", example)
    print("output:", ''.join(output))