Conv2d创建时出现Keras错误:TypeError:无法将序列乘以'float'类型的非整数

时间:2019-07-18 19:52:26

标签: python python-3.x tensorflow keras

我正在尝试构建多层CNN,并且我对以前构建的模型使用了类似的逻辑,但是现在当我运行它时,它会挂在input_shape参数上。

我正在使用:

python 3.6.8
tensorflow 1.11.0
keras 2.1.6-tf

我已经注释掉了input_shape参数,然后该模型将被构造和编译,但是显然这并不能构成可用的模型。我试图使所有数字彼此成倍,以查看问题是否在于除法导致非整数,但无济于事。

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
import numpy as np


def model():
    new_model = Sequential()
    for i in range(4):
        new_model.add(Conv2D(
                      filters=(3,3), kernel_size = 1
                      , activation='linear', padding='valid'
                      , input_shape=np.array([9,9,9])))

    return cnn_model

if __name__ == '__main__':
    model()

这将导致以下经过精简的堆栈跟踪。

Traceback (most recent call last):
  File "example.py", line 19, in <module>
    model()
  File "example.py", line 14, in model
    , input_shape=np.array([9,9,9])))
  File "/home/jb/.local/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 426, in _method_wrapper
    method(self, *args, **kwargs)
...
  File "/home/jb/.local/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 464, in __call__
    fan_in, fan_out = _compute_fans(scale_shape)
  File "/home/jb/.local/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 1300, in _compute_fans
    fan_out = shape[-1] * receptive_field_size
TypeError: can't multiply sequence by non-int of type 'float'

请让我知道我是否在俯视某事。

1 个答案:

答案 0 :(得分:0)

来自keras doc

  

在将此层用作模型的第一层时,请提供关键字参数input_shape(整数元组,不包括批处理轴),例如input_shape =(128,128,3)用于data_format =“ channels_last”中的128x128 RGB图片。

因此input_shape仅适用于第一个转换层。删除所有后续图层的形状,即可自动计算形状。

编辑:kernelfilter的参数也被交换。应该是

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,Input
import numpy as np


def model():
    new_model = Sequential()

    new_model.add(Conv2D(filters = 1,kernel_size = (3,3),activation = 'linear',padding = 'valid',input_shape = (9,9,3)))
    for i in range(3):
        new_model.add(Conv2D(
                      filters = 1,kernel_size = (3,3)
                      , activation='linear', padding='valid'))
    new_model.compile(optimizer='rmsprop',loss='mse')

    return new_model


model().summary()

Model: "sequential_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_46 (Conv2D)           (None, 7, 7, 1)           28        
_________________________________________________________________
conv2d_47 (Conv2D)           (None, 5, 5, 1)           10        
_________________________________________________________________
conv2d_48 (Conv2D)           (None, 3, 3, 1)           10        
_________________________________________________________________
conv2d_49 (Conv2D)           (None, 1, 1, 1)           10        
=================================================================
Total params: 58
Trainable params: 58
Non-trainable params: 0

只需忽略名称,我已经加载了另一个模型,因此图层的名称从46开始。