具有混合输入数据的功能性API。收到此错误:AttributeError:'tuple'对象没有属性'ndim'

时间:2019-09-23 13:37:26

标签: python tensorflow keras layer categorical-data

我已经阅读并搜索了类似的问题,但是即使我尝试了几种方法,也无法找到答案。

我有一个这样的数据集 4.000.000代表单元格的不同字符串(分类数据) 和4.000.000经度,纬度,表示这些单元格所在的位置 这是数据集的片段(数字数据)


device_id                    seconds      latitude     longitude

jg4M/taYRc2cBJPGa8c7vw==       752        53.392060    -2.069796

我创建了可以运行的模型,但是尝试安装它时遇到了问题。 该模型具有一组用于分类数据的图层和一组单独的用于数值数据的图层。我将它们合并。 一切正常,直到适合部位 这是代码。


device_id = Input(name = 'data_tf', shape = [1,1])
visible = Input(name = 'X_train', shape=(3,1,1))

#this is the categorical layers  
top_words = 10376
embedding_vector_length = 22
x = Embedding(top_words, embedding_vector_length)(device_id)
x = Dense(2, activation='sigmoid')(x)
modelx = Model(inputs=device_id, outputs = x)


#this are the numerical layers
hidden1 = Dense(10, activation='relu')(visible)
hidden2 = Dense(20, activation='relu')(hidden1)
hidden3 = Dense(10, activation='relu')(hidden2)
output = Dense(2, activation='sigmoid')(hidden3)

modely = Model(inputs=visible, outputs=output)

#merge the two set of layers and all this works perfectly 
merge = concatenate([x, output], axis =1)

#layers after concatenate 

hidden1 = Dense(10, activation='relu')(merge)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(2, activation='sigmoid')(hidden2)
model = Model(inputs=[device_id,visible], outputs=output)

这是该模型的摘要:


Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
X_train (InputLayer)            (None, 3, 1, 1)      0                                            
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 3, 1, 10)     20          X_train[0][0]                    
__________________________________________________________________________________________________
data_tf (InputLayer)            (None, 1, 1)         0                                            
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 3, 1, 20)     220         dense_2[0][0]                    
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 1, 1, 22)     228272      data_tf[0][0]                    
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 3, 1, 10)     210         dense_3[0][0]                    
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 1, 1, 2)      46          embedding_1[0][0]                
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 3, 1, 2)      22          dense_4[0][0]                    
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 4, 1, 2)      0           dense_1[0][0]                    
                                                                 dense_5[0][0]                    
__________________________________________________________________________________________________
dense_6 (Dense)                 (None, 4, 1, 10)     30          concatenate_1[0][0]              
__________________________________________________________________________________________________
dense_7 (Dense)                 (None, 4, 1, 10)     110         dense_6[0][0]                    
__________________________________________________________________________________________________
dense_8 (Dense)                 (None, 4, 1, 2)      22          dense_7[0][0]                    
==================================================================================================
Total params: 228,952
Trainable params: 228,952
Non-trainable params: 0
__________________________________________________________________________________________________
None

然后

model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy'])

但这是无效的部分:

model.fit((visible,device_id),X_trainY, batch_size=10, epochs=5, verbose=1)


AttributeError: 'tuple' object has no attribute 'ndim'

我已经搜索了4天类似的问题,但没有一个适合我。

有人可以帮忙吗?

更新 我在下面将瑞沙布·萨哈拉瓦特(Rishabh Sahrawat)的建议放在方括号中,现在遇到了这个问题

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-9b35f717e3e7> in <module>
----> 1 model.fit([visible,device_id],X_trainY, batch_size=10, epochs=5, verbose=1)

//anaconda3/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

//anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

//anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
     90         data = data.values if data.__class__.__name__ == 'DataFrame' else data
     91         data = [data]
---> 92     data = [standardize_single_array(x) for x in data]
     93 
     94     if len(data) != len(names):

//anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in <listcomp>(.0)
     90         data = data.values if data.__class__.__name__ == 'DataFrame' else data
     91         data = [data]
---> 92     data = [standardize_single_array(x) for x in data]
     93 
     94     if len(data) != len(names):

//anaconda3/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_single_array(x)
     23                 'When feeding symbolic tensors to a model, we expect the'
     24                 'tensors to have a static batch size. '
---> 25                 'Got tensor with shape: %s' % str(shape))
     26         return x
     27     elif x.ndim == 1:

ValueError: When feeding symbolic tensors to a model, we expect thetensors to have a static batch size. Got tensor with shape: (None, 3, 1, 1)

1 个答案:

答案 0 :(得分:0)

由于您没有粘贴完整的错误日志,所以从您共享的内容来看,我认为问题在于用于输入数据(分类,数字)的圆括号。 尝试使用model.fit([visible,device_id], X_trainY, batch_size=10, epochs=5, verbose=1)

这样的方括号

编辑

device_id的定义中,shape参数应使用圆括号而不是方括号。或者,也许有一个可以解释的原因。

您的EDIT中出现新错误的原因可能是由于在model.fit中使用了图层名称,而不是提供了实际数据。查看here(跳至共享层)如何以及以何种方式馈入共享层模型。另外,看看类似的issue

我在您的代码中发现的另一件事是在这里: model = Model(inputs=[device_id,visible], outputs=output),在这里,模型首先需要device_id数据,然后是visible数据,但是在fit的原始代码中,您需要相反的方式。 model.fit((visible,device_id),X_trainY, batch_size=10, epochs=5, verbose=1),此处的visibledevice_id必须是实际数据,而不是图层名称。