【问题标题】:Creating a multi-channel network: 'Concatenate' object has no attribute 'shape'创建多通道网络:“连接”对象没有属性“形状”
【发布时间】:2020-10-13 13:22:54
【问题描述】:

我正在尝试制作如下的多输入模型,但在定义以下内容时遇到了麻烦:

  1. 每个单独输入的实际输入形状
  2. 何时应使用扁平化或不使用
  3. 将我的两个单独模型连接在一起

我想构建这样的东西:

-First Dense Layer-      - First Dense layer -
         |                        |
         |                        |
Second Dense layer          Second Dense layer
                      |
                      |
            Final Dense layer (Single Output)

但是在运行我的模型时出现以下错误:

AttributeError: 'Concatenate' object has no attribute 'shape'

我的代码

def build_nn_model(x_input1_train, x_input2_train):
    
    """
    Creates the a multi-channel ANN, capable of accepting multiple inputs.

    :param: none
    :return: the model of the ANN with a single output given
    """

    x_input1= np.expand_dims(x_input1,1)

    # define two sets of inputs for models
    input1= Input(shape = (x_input1.shape[1], 1))
    input2= Input(shape = (x_input2.shape[1], 1))

    # The first branch operates on the first input
    x = Dense(units = 128, activation="relu")(input1)
    x = BatchNormalization()(x)
    
    x = Dense(units = 128, activation="relu")(x)
    x =Flatten()(x)
    x = BatchNormalization()(x)  
    
    x = Model(inputs=input1, outputs=x)

    # The second branch operates on the second input
    y = Dense(units = 128, activation="relu")(input2)
    y = BatchNormalization()(y)
    
    y = Dense(units = 128, activation="relu")(y)
    y =Flatten()(y)
    y = BatchNormalization()(y)  
    
    y = Model(inputs=inp_embeddings, outputs=y)
    
    # combine the output of the two branches
    combined = Concatenate([x.output, y.output])
    
    # Apply a FC layer and then a regression activation on the combined outputs
    #z = Dense(2, activation="relu")(combined)
    #z = Dense(1, activation="linear")(z)
    
    outputs = Dense(128, activation='relu')(combined)
    #out = Dropout(0.5)(out)
    outputs = Dense(1)(out)

    # The model will accept the inputs of the two branches and then output a single value
    model = Model(inputs = [x.input, y.input], outputs = out)

    #model = Model(inputs=[x.input, y.input], outputs=z)

    # Compile the NN
    model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])

    # ANN Summary
    model.summary()
    
    return model

输入1

array([55., 46., 46., ..., 60., 60., 45.])

形状:(2400,)

输入2

array([[-2.00370455, -2.35689664, -1.96147382, ...,  2.11014128,
         2.59383321,  1.24209607],
       [-1.97130549, -2.19063663, -2.02996445, ...,  2.32125568,
         2.27316046,  1.48600614],
       [-2.01526666, -2.40440917, -1.94321752, ...,  2.15266657,
         2.68460488,  1.23534095],
       ...,
       [-2.1359458 , -2.52428007, -1.75701785, ...,  2.25480819,
         2.68114281,  1.75468981],
       [-1.95868206, -2.23297167, -1.96401751, ...,  2.07427239,
         2.60306072,  1.28556955],
       [-1.80507278, -2.62199521, -2.08697271, ...,  2.34080577,
         2.48254585,  1.52028871]])>

形状:(2400, 3840)

【问题讨论】:

    标签: python tensorflow keras deep-learning neural-network


    【解决方案1】:

    您需要将括号添加到Concatenate 层。我是Concatenate()([x.output, y.output])

    您也可以在不使用展平操作的情况下编写模型。你的数据是二维的,所以你不需要做奇怪的操作。您需要使用 flatten 从 3D(或更大尺寸)传递到 2D,但在您的情况下,您可以毫无问题地从 2D 开始

    这里是一个完整的例子

    n_sample = 2400
    X1 = np.random.uniform(0,1, (n_sample,))  # (2400,)
    X2 = np.random.uniform(0,1, (n_sample,3840))  # (2400,3840)
    Y = np.random.uniform(0,1, (n_sample,))  # (2400,)
    
    input1= Input(shape = (1, ))
    input2= Input(shape = (3840, ))
    
    # The first branch operates on the first input
    x = Dense(units = 128, activation="relu")(input1)
    x = BatchNormalization()(x)
    x = Dense(units = 128, activation="relu")(x)
    x = BatchNormalization()(x)
    x = Model(inputs=input1, outputs=x)
    
    # The second branch operates on the second input (Protein Embeddings)
    y = Dense(units = 128, activation="relu")(input2)
    y = BatchNormalization()(y)
    y = Dense(units = 128, activation="relu")(y)
    y = BatchNormalization()(y)  
    y = Model(inputs=input2, outputs=y)
    
    # combine the output of the two branches
    combined = Concatenate()([x.output, y.output])
    
    out = Dense(128, activation='relu')(combined)
    out = Dropout(0.5)(out)
    out = Dense(1)(out)
    
    # The model will accept the inputs of the two branches and then output a single value
    model = Model(inputs = [x.input, y.input], outputs = out)
    model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])
    
    model.fit([X1,X2], Y, epochs=3)
    

    here the notebook

    【讨论】:

      【解决方案2】:

      尝试 np.expand_dims(x,1) 获取 input1 获得形状:(2400,1)

      然后np.column_stack((input1,input2))

      示例代码:

      import numpy as np
      
      x = np.array([55., 46., 46.])
      
      input1 = np.expand_dims(x,1)
      
      input2=np.array([[-2.00370455, -2.35689664, -1.96147382,  2.11014128,
               2.59383321,  1.24209607],
             [-1.97130549, -2.19063663, -2.02996445, 2.32125568,
               2.27316046,  1.48600614],
             [-2.01526666, -2.40440917, -1.94321752, 2.15266657,
               2.68460488,  1.23534095]])
      
      print(input1)
      print(input2)
      input_cs=np.column_stack((input1,input2))
      print(input_cs)
      

      出来了

      [[55.]
       [46.]
       [46.]]
      
      [[-2.00370455 -2.35689664 -1.96147382  2.11014128  2.59383321  1.24209607]
       [-1.97130549 -2.19063663 -2.02996445  2.32125568  2.27316046  1.48600614]
       [-2.01526666 -2.40440917 -1.94321752  2.15266657  2.68460488  1.23534095]]
      
      [[55.         -2.00370455 -2.35689664 -1.96147382  2.11014128  2.59383321
         1.24209607]
       [46.         -1.97130549 -2.19063663 -2.02996445  2.32125568  2.27316046
         1.48600614]
       [46.         -2.01526666 -2.40440917 -1.94321752  2.15266657  2.68460488
         1.23534095]]
      >>> 
      

      【讨论】:

      • 感谢您正确查看此内容。我在哪里以及为什么要使用 input_cs?
      猜你喜欢
      • 1970-01-01
      • 2020-05-18
      • 2014-01-27
      • 2021-09-05
      • 2016-05-09
      • 2015-05-07
      • 2017-02-11
      • 2018-11-22
      • 2020-04-24
      相关资源
      最近更新 更多