如何重塑卷积神经网络模型的数据?

时间:2017-07-25 12:06:09

标签: deep-learning keras

我需要在卷积神经网络模型中输入重塑数据, 但我的问题是代码行:

model = Sequential()
input_traces = Input(shape=(3253,))
model.add(Convolution1D(nb_filter=32, filter_length=3, 
activation='relu',input_shape = input_traces))      

这一行给了我这个错误:

   CNN_Based_Attack.py:139: UserWarning: Update your `Conv1D` call to the Keras 2 API: `Conv1D(activation="relu", input_shape=(None, /in..., padding="same", filters=32, kernel_size=3)`
  model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu',input_dim=input_traces))
Traceback (most recent call last):
  File "CNN_Based_Attack.py", line 139, in <module>
    model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu',input_dim=input_traces))
  File "/home/.local/lib/python2.7/site-packages/keras/models.py", line 430, in add layer(x)
  File "/home/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 557, in __call_self.build(input_shapes[0])
  File "/home/.local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 134, in build
    constraint=self.kernel_constraint)
  File "/home/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 88, in wrapper return func(*args, **kwargs)
  File "/home/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 390, in add_weight
    weight = K.variable(initializer(shape), dtype=dtype, name=name)
  File "/home/.local/lib/python2.7/site-packages/keras/initializers.py", line 200, in __call__
    scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: float() argument must be a string or a number

当我尝试修改它时:

model = Sequential()
model.add(Convolution1D(nb_filter=32, filter_length=3, 
activation='relu',input_shape = (500000, 3253)))        

它给出了这个错误:

/home/.local/lib/python2.7/site-packages/keras/models.py:834: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  warnings.warn('The `nb_epoch` argument in `fit` '
Traceback (most recent call last):
  File "CNN_Based_Attack.py", line 113, in <module>
    model.fit(x_train, y_train, batch_size=15, nb_epoch=30)
  File "/home/.local/lib/python2.7/site-packages/keras/models.py", line 853, in fit
    initial_epoch=initial_epoch)
  File "/home/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1424, in fit
    batch_size=batch_size)
  File "/home/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1300, in _standardize_user_data
    exception_prefix='input')
  File "/home/.local/lib/python2.7/site-packages/keras/engine/training.py", line 127, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking input: expected conv1d_1_input to have 3 dimensions, but got array with shape (500000, 3253)

我真的不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

我假设你使用旧版本的Keras(因为release 2.0nb_filter已更改为filters,因此,您应该遵循旧文档(例如{{3}而不是。

在第一个片段中,我认为问题出在这一部分:input_shape = input_tracesConvolution1D构造函数需要tuple,例如(32, 100, 3),但input_traces初始化为Keras图层。

在第二个剪辑中,您通过了tuple,这是正确的。错误表示它希望input_shape有3个维度而不是2个。首先,我想指出nb_filter表示&#39;每批数据的过滤器数量&#39 ; 。因此,input_shape还必须包含bach_size(如果您不熟悉此概念,则有this one涵盖您需要了解的有关批次的所有内容)。所以,只需传递

Convolution1D(..., input_shape = (batch_size, data_length, numof_channels), ...)

并且一切都应该有用(如果你想知道什么是numof_channels,它类似于图像有3个通道:红色,绿色和蓝色)。如果您想拥有任意bach_size,则可以传递input_shape = (None, data_length, numof_channels)