带有data_format channels_first的Conv1D会在Keras

时间:2018-05-25 20:45:51

标签: tensorflow keras

我正在尝试使用一个Conv1D操作定义一个基本的ConvNet,如下所示:

n_class = 10

# channels last (default)
input_shape = (100, 1)
inp = Input(shape=input_shape)
x = Conv1D(4, kernel_size=9, activation=activations.relu, padding='same')(inp)

x = MaxPool1D(pool_size=5)(x)
x = Flatten()(x)
out = Dense(n_class, activation='softmax')(x)

model = Model(inputs=inp, outputs=out)

并且工作正常(默认情况下使用data_format ='channels_last')。但是,如果我想使用data_format ='channels_first':

# "channels_first" inputs with shape (batch, channels, length).
input_shape = (1, 100)
inp = Input(shape=input_shape)
x = Conv1D(4, kernel_size=9, activation=activations.relu, data_format='channels_first', padding='same')(inp)

x = MaxPool1D(pool_size=5, data_format='channels_first')(x)
x = Flatten()(x)
out = Dense(n_class, activation='softmax')(x)

model = Model(inputs=inp, outputs=out)

定义Conv1D层时出现以下错误:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/edward/Desktop/baselines/cnn_mel.py", line 720, in <module>
    model = get_1d_dummy_model(params_learn=params_learn, params_extract=params_extract)
  File "/Users/edward/Desktop/baselines/architectures.py", line 71, in get_1d_dummy_model
    x = Conv1D(4, kernel_size=9, activation=activations.relu, data_format='channels_first', padding='same')(inp)
  File "/Users/edward/miniconda3/envs/baseline/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/edward/miniconda3/envs/baseline/lib/python3.6/site-packages/keras/layers/convolutional.py", line 337, in __init__
    **kwargs)
TypeError: __init__() got multiple values for keyword argument 'data_format'

对于做错了什么的任何见解?谢谢!

1 个答案:

答案 0 :(得分:1)

您似乎没有使用最新的代码(不仅仅是最新版本)。对于Conv1D层,即使在最新版本2.1.6中也不支持data_format='channels_first'。您需要克隆并使用master分支中的代码。该支持由this commit于5/7/2018添加。文档始终与主文件同步,这可能会造成混淆。 The idea (from the Keras creator François Chollet)就是那个

  仅存在

版本以强制PyPI用户升级。它们没有意义。你应该经常同步掌握。

您可以找到一些旧的Keras文档here