Home > Enterprise >  Graph disconnected: cannot obtain value for tensor Tensor("input_20:0", shape=(?, 25, 2),
Graph disconnected: cannot obtain value for tensor Tensor("input_20:0", shape=(?, 25, 2),

Time:11-07

I try to combine two CNNs with Keras. Here is the code:

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

The error shows:

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: []

CodePudding user response:

The problem is that you defined your model with inputs=[input_p, input_sp], but you are not actually using [input_p, input_sp] as input layers when you build the model, you are using [model_p_in, model_sp_in] instead. See the updated code below.

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')
  • Related