python - 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]
解决方案
你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)
推荐阅读
- c++ - NCurses 方法可以毫不拖延地区分 ESC 和功能键
- c - 从文本文件中打印冒泡排序列表
- r - 发现 2 个范围及其重叠区域长度之间的重叠?
- android - 清除深层链接反应原生
- javascript - 为什么当另一个 div 聚焦时这个 div 没有出现?
- reactjs - ReferenceError:使用以前版本的 jest 后未定义 TextEncoder
- ethereum - 我的 geth 节点卡在“寻找对等点”
- vba - 在更改 VBA 代码之前不会触发 Word 中的 Document_open 宏(无关紧要)
- arrays - 如何在数组中打包结构并删除零填充?
- django - Django Tempus Dominus:设置格式会使表单无效