简体   繁体   English

警告:tensorflow:Model 是用输入 Tensor() 的形状构建的。 但它是在形状不兼容的输入上调用的

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

I'm training a model with a generator and I'm getting this Warning from Tensorflow, although I can train the model without errors, I want to fix this or at least understand why it happens.我正在使用生成器训练 model,我从 Tensorflow 收到此警告,尽管我可以毫无错误地训练 model,但我想修复此问题或至少了解它发生的原因。

My data from the generator have this shapes:我来自生成器的数据具有以下形状:

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)

This is my model:这是我的 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

And this are the warnings I'm getting when running the model:这是我在运行 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).

I don't understand why I get this, the shape of the input is (20,) so should be correct, any suggestions?我不明白为什么会这样,输入的形状是 (20,) 所以应该是正确的,有什么建议吗?

EDIT编辑

Generator:发电机:

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)

Call to model.fit() :调用model.fit()

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

Sample of training_encoder_input : 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]]

The shape of your input should be like:输入的形状应该是这样的:

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

In model None is batch size so this particular dimension should also appear in your x data.在 model 中, None是批量大小,所以这个特定的维度也应该出现在你的x数据中。 So, you need to update your generate as:因此,您需要将生成更新为:

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))

If you have more than one batch size, you create a list/array of elements as (bs,20,) where bs is batch size.如果您有多个批量大小,则创建一个元素列表/数组为(bs,20,) ,其中bs是批量大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow 模型是用输入 Tensor 的形状构建的,但它在形状不兼容的输入(神经网络)上被调用 - Tensorflow model was constructed with shape for input Tensor but it was called on an input with incompatible shape (Neural Network) WARNING:tensorflow:Model 是用形状 (None, 188) 构造的输入,但它是在形状不兼容的输入上调用的 (188,) - WARNING:tensorflow:Model was constructed with shape (None, 188) for input, but it was called on an input with incompatible shape (188,) 警告:警告:tensorflow:Model 是用形状(无,150)构造的,但它是在形状不兼容的输入上调用的(无,1) - WARNING: WARNING:tensorflow:Model was constructed with shape (None, 150) , but it was called on an input with incompatible shape (None, 1) tensorflow:Model 是用形状 (None, None, 6) 构造的,但它是在形状不兼容的输入上调用的 - tensorflow:Model was constructed with shape (None, None, 6), but it was called on an input with incompatible shape Tensorflow:Model 是用形状 (None, 28, 28) 构造的,但它是在形状不兼容的输入上调用的 (None, 28) - Tensorflow:Model was constructed with shape (None, 28, 28) , but it was called on an input with incompatible shape (None, 28) 警告:tensorflow:模型是用形状 (20, 37, 42) 构建的,用于输入 Tensor(“input_5:0”, shape=(20, 37, 42), dtype=float32),但是 - WARNING:tensorflow:Model was constructed with shape (20, 37, 42) for input Tensor(“input_5:0”, shape=(20, 37, 42), dtype=float32), but python 中的神经网络:警告:tensorflow:Model 是用形状(无,7)构建的 - Neural Network in python: WARNING:tensorflow:Model was constructed with shape (None, 7) for input 模型是用形状 (None, 180, 180, 3) 构建的用于输入,但它在形状不兼容的输入 (None, 180, 3) 上被调用? - Model was constructed with shape (None, 180, 180, 3) for input, but it was called on an input with incompatible shape (None, 180, 3)? Model 是用形状(无,65536)构造的,但它是在形状不兼容的输入上调用的(无,65536,无) - Model was constructed with shape (None, 65536) but it was called on an input with incompatible shape (None, 65536, None) 警告:tensorflow:Model 是用形状构造的(无,...) - WARNING:tensorflow:Model was constructed with shape (None, …)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM