2017-07-25 84 views
0

我需要让输入我的卷积神经网络模型重塑数据, 但我的问题是代码行:如何为卷积神经网络模型重塑我的数据?

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 

当我尝试mdify它:

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) 

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

回答

0

我假设你使用的是旧版本Keras的(因为release 2.0nb_filter已更改为filters,因此,你应该遵循旧的文件(如this one)来代替。

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

在第二个文档片断,你通过了tuple代替,这是正确的错误说。日在它期望input_shape有3个维度,而不是2.首先,我想指出nb_filter意味着“每批数据的过滤器数量”。因此,input_shape还必须包含bach_size(如果您不熟悉此概念,则有wonderful answer涵盖了您需要了解的有关批次的所有信息)。所以,仅仅通过

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

,一切都应该工作(在情况下,如果你想知道什么是numof_channels,它类似于图像如何有3个渠道:红,绿,蓝)。如果您想拥有任意bach_size,则可以通过input_shape = (None, data_length, numof_channels)

+0

非常感谢您的帮助,在我的情况下,我不会分析图像,而是浮点数据(某些芯片消耗量采集),因此我没有任何通道,我必须将它们放到numod_channel = None – tierrytestu

+0

@ tierrytestu如果你只有浮点值,那么你的情况'numof_channels = 1'。我不太了解Keras 1,但我希望我的答案能奏效。 – FalconUA