title: Splitting cnn layer in keras
body: <p>I'd like to get rid of some channels in CNN. It consists of two models which are combined to create a simple model. </p>
  
  <p>It creates correct output but in intermediate layer but still gives an error later.</p>
  
  <pre><code>def highAccuracyModelTillConv2(input_img): 
    conv1_1 = (Conv2D(32, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay), input_shape=x_train.shape[1:], activation='elu', name = 'HighAccuracyConv1'))(input_img)
    conv1_2 = BatchNormalization()(conv1_1)
    conv2_1 = (Conv2D(32, (3,3), padding='same', activation= 'elu', name = 'HighAccuracyConv2',kernel_regularizer=regularizers.l2(weight_decay)))(conv1_2)
    conv2_2 = BatchNormalization()(conv2_1)  
    return conv2_1
  </code></pre>
  
  <pre><code>import tensorflow as tf
  def cifar10ClassifierTransfer(input_img, conv2_high):
  
    # Add additional inputs to 
  
    conv1_1 = (Conv2D(32, (3,3), padding='same', kernel_regularizer=regularizers.l2(weight_decay), input_shape=x_train.shape[1:], activation='elu'))(input_img)
    conv1_2 = BatchNormalization()(conv1_1)
    conv2_1 = (Conv2D(32, (3,3), padding='same', activation= 'elu',kernel_regularizer=regularizers.l2(weight_decay)))(conv1_2)
    channels = [1,2,4]
    branch_outputs = []
    orig_channel = conv2_1
    branch_outputs.append(orig_channel)
    for i in channels:
        # Slicing the ith channel:
        out = Lambda(lambda x: x[:,:,:, i])(conv2_high)
        # Setting up your per-channel layers (replace with actual sub-models):
        layer_out =  keras.backend.expand_dims(out, axis = 3)
        branch_outputs.append(layer_out)
    out = Concatenate()(branch_outputs)
    print(out.shape)
  
    out = Dense(num_classes, activation='softmax')(out)
  
    return out  
  
  </code></pre>
  
  <pre><code>high_acc_output = highAccuracyModelTillConv2(input_img)
  modelFilt = Model(input_img, 
                cifar10ClassifierTransfer(input_img,
                high_acc_output))
  </code></pre>
  
  <p>This is the error I get when I create the model despite correct correct value of intermediate output. Any help would be appreciated.</p>
  
  <pre><code>(?, 32, 32, 35)
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-257-685f377c5c1a&gt; in &lt;module&gt;()
        3 modelFilt = Model(input_img, 
        4               cifar10ClassifierTransfer(input_img,
  ----&gt; 5               high_acc_output))
        6 # #data augmentation
        7 # datagen = ImageDataGenerator(
  
  6 frames
  /usr/local/lib/python3.6/dist-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index)
     1323             ValueError: if a cycle is detected.
     1324         """
  -&gt; 1325         node = layer._inbound_nodes[node_index]
     1326 
     1327         # Prevent cycles.
  
  AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
  </code></pre>