Skip to main content
 首页 » 编程设计

python之如何在 Keras 中展平不同大小的数据并在下一层中使用它

2024年09月07日8mengfanrong

我想构建一个 CNN,它可以是行数不变但列数可变的馈送矩阵。我从this blog开始但它在某些时候使用了 Flatten 层。根据this GitHub issue , Flatten 层不能与未完全定义形状的数据一起使用。我用了this answer像这样构建一个 CNN:

from keras import Input, Model 
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Lambda, Activation 
from keras.optimizers import SGD 
import keras.backend as K 
 
input = Input(shape=(None, 60, 1)) 
        x = Conv2D(list_of_neurons[0], (3, 3), padding='same', activation='relu')(input) 
        x = MaxPooling2D(pool_size=(2, 2))(x) # None x 30 
 
        x = Conv2D(list_of_neurons[1], (3, 3), padding='same', activation='relu')(x) 
        x = MaxPooling2D(pool_size=(2, 2))(x) # None x 15 
 
        x = Conv2D(list_of_neurons[2], (3, 3), padding='same', activation='relu')(x) 
        x = MaxPooling2D(pool_size=(3, 3))(x) # None x 5 
 
        x = Lambda(lambda k: K.batch_flatten(k))(x) 
        x = Dense(list_of_neurons[3])(x) 
        x = Activation("relu")(x) 
        x = Dropout(dropout)(x) 
        output = Dense(1, activation='sigmoid')(x) 
 
        self._model = Model(inputs=input, outputs=output) 
        self._model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) 
        self._model.summary() 

但是,当我尝试运行脚本时出现错误:

Traceback (most recent call last): 
  File "/home/kuba/Dropbox/MGR Jakub Kustra/implementacja/first_cnn.py", line 66, in <module> 
    mlp = MLP([60, 60, 120, 120], 0.3) 
  File "/home/kuba/Dropbox/MGR Jakub Kustra/implementacja/models.py", line 22, in __init__ 
    x = Dense(list_of_neurons[3])(x) 
  File "/home/kuba/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 576, in __call__ 
    self.build(input_shapes[0]) 
  File "/home/kuba/anaconda3/lib/python3.6/site-packages/keras/layers/core.py", line 830, in build 
    constraint=self.kernel_constraint) 
  File "/home/kuba/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper 
    return func(*args, **kwargs) 
  File "/home/kuba/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 397, in add_weight 
    weight = K.variable(initializer(shape), 
  File "/home/kuba/anaconda3/lib/python3.6/site-packages/keras/initializers.py", line 204, in __call__ 
    scale /= max(1., float(fan_in + fan_out) / 2) 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 

错误发生在initializers.py中的这一行:

def __call__(self, shape, dtype=None): 
    fan_in, fan_out = _compute_fans(shape) 
    scale = self.scale 
    if self.mode == 'fan_in': 
        scale /= max(1., fan_in) 
    elif self.mode == 'fan_out': 
        scale /= max(1., fan_out) 
    else: 
        scale /= max(1., float(fan_in + fan_out) / 2) # <<<<<<<< HERE 
    if self.distribution == 'normal': 
        stddev = np.sqrt(scale) 
        return K.truncated_normal(shape, 0., stddev, 
                                  dtype=dtype, seed=self.seed) 
    else: 
        limit = np.sqrt(3. * scale) 
        return K.random_uniform(shape, -limit, limit, 
                                dtype=dtype, seed=self.seed) 

fan_in 变量是None。我认为 Dense 层可以提供不同大小的数据。我该如何克服这个问题?

请您参考如下方法:

不幸的是,由于一个简单的原因,您的方案无法实现:您的Flatten 后面跟着一个Dense 层。为了为 Dense 层分配张量,为什么现在应该输入形状 - 因为分配器应该分配形状为 (input_shape, output_shape) 的权重矩阵。这是您的错误来源。

您提供的问题中提出的解决方案解决了一些不同的任务。那里的问题与这样一个事实有关 - 有时需要指定批处理的形状,这就是 batch_flatten 旨在解决的问题。