首页 > 解决方案 > Keras 中的连接层

问题描述

我正在尝试在我的模型中接受 2 个输入。但它有一个奇怪的问题。

x1= layers.Input((20000,))
x2= layers.Reshape((200,100), input_shape=(20000,))(x1)

y1= layers.Input((200000,))
y2= layers.Reshape((2000,100), input_shape=(200000,))(y1)

combine = layers.Concatenate(axis=1)([x2, y2])
model = tf.keras.Model(inputs=[x1, y1], outputs=combine)
model.predict([datasetA, datasetB])

如果我接受一个输入,模型就可以运行。

model = tf.keras.Model(inputs=[x1], output=x2)
model.predict(datasetA)

但是如果我接受两个输入,模型就死了。

Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.PrefetchDataset'>"}), <class 'NoneType'>

即我的数据集的结构是:

<PrefetchDataset shapes: ((None, 20000), (None,)), types: (tf.int64, tf.int32)>
1 Dataset -> 5 Batche_data and 5 batch_Label
Each Batch_data -> 3 records
Each record -> [20000]

Each batch_Label -> 3 records
Each label -> 0 / 1 for classification

我该如何解决这个问题?

model.summary()
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            [(None, 20000)]      0                                            
__________________________________________________________________________________________________
input_4 (InputLayer)            [(None, 200000)]     0                                            
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 200, 100)     0           input_3[0][0]                    
__________________________________________________________________________________________________
reshape_3 (Reshape)             (None, 2000, 100)    0           input_4[0][0]                    
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 2200, 100)    0           reshape_2[0][0]                  
                                                                 reshape_3[0][0]                  

标签: pythontensorflowkeras

解决方案


tensoflow.keras是用来导入图层的吗?你也可以打印model.summary 唯一可能有问题的是你通过模型的形状。

此代码工作正常。张量流 == 2.0.0

import tensorflow as tf
import numpy as np

datasetA = np.zeros((10,20000),dtype=int)
datasetB = np.zeros((10,200000),dtype=int)

x1= tf.keras.layers.Input((20000,))
x2= tf.keras.layers.Reshape((200,100), input_shape=(20000,))(x1)

y1= tf.keras.layers.Input((200000,))
y2= tf.keras.layers.Reshape((2000,100), input_shape=(200000,))(y1)

combine = tf.keras.layers.Concatenate(axis=1)([x2, y2])
model = tf.keras.Model(inputs=[x1, y1], outputs=combine)
model.summary()
model.predict([datasetA, datasetB])
print('the predict done')], outputs=combine)

推荐阅读