本文介绍了Keras为9维特征向量构建网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的数据集.它包含9个功能,它是一个二进制分类问题.特征向量的示例如下所示.每行都有对应的0,1标签.

I have the following simple data set. It consists of 9 features and it is a binary classification problem. An example of the feature vectors are show below. Each row has its corresponding 0,1 label.

30,82,1,2.73,172,117,2,2,655.94
30,174,1,5.8,256,189,3,2,587.28
98.99,84,2,0.84,577,367,3,2,1237.34
30,28,1,0.93,38,35,2,1,112.35
...

我知道CNN广泛用于图像分类,但是我正在尝试将其应用于我手头的数据集.我正在尝试应用每个大小为2的5个过滤器.根据该数据的形状,我一直坚持以正确的方式构建网络.这是我建立网络的功能.

I know CNNs are used extensively for image classification, but I'm trying to apply it to the data set I've at hand. I'm trying to apply 5 filters each of size 2. I've been stuck with getting the network to be built in the right way given the shape of this data. Here is my function that builds the network.

def make_network(num_features,nb_classes):
   model = Sequential()
   model.add(Convolution1D(5,2,border_mode='same',input_shape=(1,num_features)))
   model.add(Activation('relu'))
   model.add(Convolution1D(5,2,border_mode='same'))
   model.add(Activation('relu'))
   model.add(Flatten())
   model.add(Dense(2))
   model.add(Activation('softmax'))

我还将最终调用一个测试函数来测试我创建的模型的准确性.以下功能试图实现这一目标

I will also finally call a testing function to test the accuracy of the model I've created. The following function tries to achieve that

def train_model(model, X_train, Y_train, X_test, Y_test):

    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.3, nesterov=True)
    model.compile(loss='binary_crossentropy', optimizer=sgd)
    model.fit(X_train, Y_train, nb_epoch=100, batch_size=10,
              validation_split=0.1, verbose=1)

    print('Testing...')
    res = model.evaluate(X_test, Y_test,
                         batch_size=batch_size, verbose=1, show_accuracy=True)
    print('Test accuracy: {0}'.format(res[1]))

当我制作模型并将其传递给训练函数时,出现以下错误

When I make the model and pass it the training function I get the following error

Using Theano backend.
Traceback (most recent call last):
  File "./cnn.py", line 69, in <module>
    train_model(model,x_train,y_train,x_test,y_test)
  File "./cnn.py", line 19, in train_model
    validation_split=0.1, verbose=1)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 413, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1011, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 938, in _standardize_user_data
    exception_prefix='model input')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 96, in standardize_input_data
    str(array.shape))
Exception: Error when checking model input: expected convolution1d_input_1 to have 3:(None, 1, 9) dimensions, but got array with shape (4604, 9)

我是Keras的新手.我正在尝试从此处修改代码.任何帮助或指针将不胜感激.预先感谢.

I'm new to Keras. I'm trying to adapt code from here. Any help or pointers would be much appreciated. Thanks in advance.

推荐答案

您的代码model.add(Convolution1D(5,2,border_mode='same',input_shape=(1,num_features)))定义输入应为形状(batch_size, 1, num_features).但是,X_trainX_test可能为(batch_size, 9)形状,这是不一致的.

Your code model.add(Convolution1D(5,2,border_mode='same',input_shape=(1,num_features))) defines that the input should be in shape (batch_size, 1, num_features). However, X_train as well as X_test might be in shape (batch_size, 9), which is inconsistent.

def train_model(model, X_train, Y_train, X_test, Y_test):
    X_train = X_train.reshape(-1, 1, 9)
    X_test = X_test.reshape(-1, 1, 9)

    ....

这篇关于Keras为9维特征向量构建网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 13:12