图断开连接:无法获取张量张量("input_20:0",形状=(?,25,2),dtype=float32)在第"input_20"的值



我尝试将两个cnn与Keras结合起来。下面是代码:

import keras
from keras import Model
from keras.layers.core import Dense, Activation
from keras.layers import Conv2D, Conv1D, MaxPooling2D, Reshape, Concatenate, Dropout , MaxPooling1D, Flatten
from keras.layers import Dense, Input, LeakyReLU
from keras.utils.vis_utils import plot_model
input_p=Input(shape=(2001,1),name='in_p')
input_sp=Input(shape=(25,2),name='in_sp')
###CNN for P wave
model_p_in = Input((2001,1))
#1 2001 to 1000
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p_in)
model_p = LeakyReLU(alpha=0.05)(model_p)
#2 1000 to 500
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#3 500 to 250
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#4 250 to 125
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#5 125 to 62
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#6 62 to 31
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#7 31 to 15
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#8 15 to 7
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#9 7 to 3
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
model_p = Flatten()(model_p)
model_p = Dense(64)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)

###CNN for spectrums
model_sp_in = Input((25,2))
#1 25 to 25
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp_in)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#2 25 to 25
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#3 25 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=2)(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#4 12 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#5 12 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#6 12 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#7 6 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#8 6 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#9 6 to 3
model_sp = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
model_sp = Flatten()(model_sp)
model_sp = Dense(64)(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)

###Combine two CNNs
model_merged = Concatenate()([model_p, model_sp])
model_merged = Dense(128)(model_merged)
output = Dense(2, activation='softmax')(model_merged)
model_final = Model(inputs=[input_p, input_sp], outputs=[output],name='model_p_sp')

plot_model(model_final, to_file='model_final.png')

错误显示:

model_final = Model(inputs=[input_p, input_sp], outputs=[output],name='model_p_sp')
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_20:0", shape=(?, 25, 2), dtype=float32) at layer "input_20". The following previous layers were accessed without issue: []

问题是您使用inputs=[input_p, input_sp]定义了您的模型,但是当您构建模型时,您实际上并没有使用[input_p, input_sp]作为输入层,而是使用[model_p_in, model_sp_in]。请看下面更新后的代码

from keras import Model
from keras.layers import Conv1D, Concatenate, Flatten
from keras.layers import Dense, Input, LeakyReLU
from keras.utils.vis_utils import plot_model
###Input layers
input_p = Input(shape=(2001,1), name='in_p')
input_sp = Input(shape=(25,2), name='in_sp')
###CNN for P wave
#1 2001 to 1000
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(input_p) # -> use "input_p"
model_p = LeakyReLU(alpha=0.05)(model_p)
#2 1000 to 500
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#3 500 to 250
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#4 250 to 125
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#5 125 to 62
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#6 62 to 31
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#7 31 to 15
model_p = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#8 15 to 7
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
#9 7 to 3
model_p = Conv1D(filters=64, kernel_size=3, strides=2)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
model_p = Flatten()(model_p)
model_p = Dense(64)(model_p)
model_p = LeakyReLU(alpha=0.05)(model_p)
###CNN for spectrums
#1 25 to 25
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(input_sp) # -> use "input_sp"
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#2 25 to 25
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#3 25 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=2)(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#4 12 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#5 12 to 12
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#6 12 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#7 6 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#8 6 to 6
model_sp = Conv1D(filters=64, kernel_size=3, strides=1, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
#9 6 to 3
model_sp = Conv1D(filters=64, kernel_size=3, strides=2, padding='same')(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
model_sp = Flatten()(model_sp)
model_sp = Dense(64)(model_sp)
model_sp = LeakyReLU(alpha=0.05)(model_sp)
###Combine two CNNs
model_merged = Concatenate()([model_p, model_sp])
model_merged = Dense(128)(model_merged)
output = Dense(2, activation='softmax')(model_merged)
model_final = Model(inputs=[input_p, input_sp], outputs=[output],name='model_p_sp')
plot_model(model_final, to_file='model_final.png')

相关内容

  • 没有找到相关文章

最新更新