我正在嘗試基于此示例(鏈接)構建 SIMON 模型。下面是我如何構建 2 個模型:(1)句子編碼器(sentEncoder),它輸入(2)檔案編碼器(docModel)。
當我嘗試適應時,出現以下錯誤。
輸入張量必須為 3、4 或 5 級,但為 2。
我的輸入是形狀 (3003, 30, 28),即(樣本,發送的最大長度,單熱編碼字符)。
maxLength = 30
max_cells = 3003
charMap = {'a': 1,'b': 2, 'c': 3,'d': 4,'e': 5,'f': 6,
'g': 7,'h': 8, 'i': 9, 'j': 10, 'k': 11,
'l': 12,'m': 13, 'n': 14, 'o': 15, 'p': 16,
'q': 17,'r': 18, 's': 19, 't': 20, 'u': 21,
'v': 22,'w': 23, 'x': 24, 'y': 25, 'z': 26,
' ': 27}
maxChars = len(charMap) 1
x_train = np.zeros((max_cells, maxLength, maxChars), dtype='int32')
y_train = np.zeros((max_cells, 3), dtype='int32')
def buildSentModel(sentModelInput):
layer = Conv1D(10,
5,
padding='valid',
activation='relu',
strides=1)(sentModelInput)
layer = Bidirectional(LSTM(units=12, return_sequences=False))(layer)
return Model(input=sentModelInput, output=layer)
def buildDocModel(sentModel, docInput):
layer = TimeDistributed(sentModel)(docInput)
#layer = Flatten()(layer)
layer = Dense(3, activation='sigmoid')(layer)
return Model(input=docInput, output=layer)
sentModelInput = Input(shape=(30,28), dtype='float32')
sentModel = buildSentModel(sentModelInput);
docModel = buildDocModel(sentModel, sentModelInput);
docModel.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['categorical_accuracy'])
docModel.fit(x_train, y_train, steps_per_epoch=20,epochs=100, shuffle=True)
這是整個錯誤:
檔案“C:\temp\Simon\TempSimonNames.py”,第 107 行,模型 = buildDocModel(sentModel, sentModelInput);
檔案“C:\temp\Simon\TempSimonNames.py”,第 94 行,在 buildDocModel 層 = TimeDistributed(sentModel)(docInput)
檔案“C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py”,第 75 行,symbolic_fn_wrapper return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 489, in call output = self.call(inputs, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\wrappers.py", line 250, in call y = self.layer.call(inputs, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 583, in call output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 740, in run_internal_graph layer.call(computed_tensor, **kwargs))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\convolutional.py", line 163, in call dilation_rate=self.dilation_rate[0])
檔案“C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py”,第 3671 行,在 conv1d **kwargs)
檔案“C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\ops\nn_ops.py”,第917行,在convolution_v2名稱=名稱中)
檔案“C:\格式(n 2))
ValueError:輸入張量必須為 3、4 或 5 級,但為 2。
將不勝感激任何幫助。謝謝!
uj5u.com熱心網友回復:
您似乎混淆了一些變數和形狀。
根據您的代碼, sent_model 是一個模型,接收最多包含 30 個字符的句子,因此輸入形狀為 (30, 28) (盡管我想知道為什么您的句子最多只有 30 個字符,但這似乎不是那么多)。
您的 doc_model 現在應該將 sent_model 應用于檔案的每個句子。所以你的檔案的輸入形狀應該是 (num_sentences_per_doc, 30, 28)。
現在問題來了:您將相同的輸入層傳遞給兩個模型。這沒有任何意義,因為兩個模型需要具有不同的輸入層。sentModelInput(您定義的唯一輸入層)的形狀與您的 sent_model 的輸入形狀匹配,但您的 doc_model 的維度太少,因為該模型需要 (num_sentences_per_doc, 30, 28) 的形狀,如前所述。因此,解決所有這些問題的方法如下:
- 對兩個模型使用兩個不同的輸入層。
- 確保輸入層實際上具有正確的形狀。
這個修改過的代碼應該讓你知道你需要做什么。如果要使用目前注釋掉的flatten方式,你的檔案需要有固定數量的句子,所以我把這個叫做num_sents_per_doc。如果你想構建一個可以處理可變長度檔案的模型,那么你需要將 docInput 的輸入形狀設定為 (None, 30, 28) 并使用一個可以處理這個可變長度輸入的神經網路結構。
def buildSentModel():
sentModelInput = Input(shape=(30,28), dtype='float32')
layer = Conv1D(10,
5,
padding='valid',
activation='relu',
strides=1)(sentModelInput)
layer = Bidirectional(LSTM(units=12, return_sequences=False))(layer)
return Model(input=sentModelInput, output=layer)
def buildDocModel():
docInput = Input(shape=(num_sents_per_doc, 30,28), dtype='float32')
layer = TimeDistributed(sentModel)(docInput)
#layer = Flatten()(layer)
layer = Dense(3, activation='sigmoid')(layer)
return Model(input=docInput, output=layer)
sentModel = buildSentModel();
docModel = buildDocModel(sentModel);
docModel.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['categorical_accuracy'])
docModel.fit(x_train, y_train, steps_per_epoch=20,epochs=100, shuffle=True)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323917.html
