簡體   English   中英

警告:tensorflow:Model 是用輸入 Tensor() 的形狀構建的。 但它是在形狀不兼容的輸入上調用的

[英]WARNING:tensorflow:Model was constructed with shape for input Tensor(). but it was called on an input with incompatible shape

我正在使用生成器訓練 model,我從 Tensorflow 收到此警告,盡管我可以毫無錯誤地訓練 model,但我想修復此問題或至少了解它發生的原因。

我來自生成器的數據具有以下形狀:

for x, y in model_generator(): # x[0] and x[1] are the inputs, y is the output
    print(x[0].shape, x[1].shape, y.shape)

# (20,)(20,)(20,17772) 
# 17772 --> Number of unique words in my datatset
# 20 --> Number of words per example (per sentence)

這是我的 model:

Model: "functional_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 20)]         0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 20)]         0                                            
__________________________________________________________________________________________________
embedding (Embedding)           (None, 20, 50)       890850      input_1[0][0]                    
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 20, 50)       890850      input_2[0][0]                    
__________________________________________________________________________________________________
lstm (LSTM)                     [(None, 64), (None,  29440       embedding[0][0]                  
__________________________________________________________________________________________________
lstm_1 (LSTM)                   (None, 20, 64)       29440       embedding_1[0][0]                
                                                                 lstm[0][1]                       
                                                                 lstm[0][2]                       
__________________________________________________________________________________________________
time_distributed (TimeDistribut (None, 20, 17772)    1155180     lstm_1[0][0]                     
==================================================================================================
Total params: 2,995,760
Trainable params: 1,214,060
Non-trainable params: 1,781,700
__________________________________________________________________________________________________
None

這是我在運行 model 時收到的警告:

WARNING:tensorflow:Model was constructed with shape (None, 20) for input Tensor("input_1:0", shape=(None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 1).
WARNING:tensorflow:Model was constructed with shape (None, 20) for input Tensor("input_2:0", shape=(None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 1).
WARNING:tensorflow:Model was constructed with shape (None, 20) for input Tensor("input_1:0", shape=(None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 1).
WARNING:tensorflow:Model was constructed with shape (None, 20) for input Tensor("input_2:0", shape=(None, 20), dtype=float32), but it was called on an input with incompatible shape (None, 1).

我不明白為什么會這樣,輸入的形狀是 (20,) 所以應該是正確的,有什么建議嗎?

編輯

發電機:

def model_generator():
    for index, output in enumerate(training_decoder_output):
        for i in range(size):
            yield ([training_encoder_input[size*index+i], training_decoder_input[size*index+i]], output[i])

# Generator, returns inputs and ouput one by one when calling 
# (I saved the outputs in chunks on disk so that's why I iterate over it in that way)

調用model.fit()

model.fit(model_generator(), epochs=5)

training_encoder_input樣本:

print(training_encoder_input[:5])

[[   3 1516   10 3355 2798    1 9105    1 9106    4  162    1  411    1
  9107 3356  612    1 9108    1]
 [   0    0    0    0    0    0    0    0    0    0    0    2 9109 2799
  5632   29 1187    2  157  275]
 [   0   54 5633 5634    1  412 4199   12 9110 5633 5634   27  443  134
  1516    7    6 4200 1280    1]
 [  23 9112  816   11 9113   33  184 9114  816    1 9115   42    3    2
    57    5 2120    3  185    1]
 [   0    0    0    0    0    0   15  301 9116    3 3357    1 9117    1
    67 5635    4  110 5635    1]]

輸入的形狀應該是這樣的:

x[0].shape => (1,20,) # where 1 is batch size. 

在 model 中, None是批量大小,所以這個特定的維度也應該出現在你的x數據中。 因此,您需要將生成更新為:

def model_generator():
for index, output in enumerate(training_decoder_output):
    for i in range(size):
        yield ([np.expand_dims(training_encoder_input[size*index+i], axis=0), np.expand_dims(training_decoder_input[size*index+i]], axis=0), np.expand_dims(output[i], axis=0))

如果您有多個批量大小,則創建一個元素列表/數組為(bs,20,) ,其中bs是批量大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM