Keras实现一维卷积(Conv1D)实例

实战一维卷积(Conv1D):kaggle叶子分类问题

通过例子理解:
(1)一维卷积是如何工作的;
(2)输入数据是如何构建的;
在csv文件中,数据集或者特征集的保存方式nxm,其中n为样本个数,m为特征个数。这里有3类特征,64个magin特征, 64个shape特征, 64个texture特征。m=192.


这里有一篇比较详细解释代码的博客:用Keras实现简单一维卷积 ,亲测可用一维卷积实例,及Kaggle竞赛代码解读
需要的数据可以在kaggle上下载,上面的那篇文章也有下载链接。


直接看代码:

import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedShuffleSplit
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Convolution1D, Dropout
from keras.optimizers import SGD
from keras.utils import np_utils

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

def encode(train, test):
    label_encoder = LabelEncoder().fit(train.species)
    labels = label_encoder.transform(train.species)
    classes = list(label_encoder.classes_)

    train = train.drop(['species', 'id'], axis=1)
    test = test.drop('id', axis=1)

    return train, labels, test, classes

train, labels, test, classes = encode(train, test)

# standardize train features
scaler = StandardScaler().fit(train.values)
scaled_train = scaler.transform(train.values)

# split train data into train and validation
sss = StratifiedShuffleSplit(test_size=0.1, random_state=23)
for train_index, valid_index in sss.split(scaled_train, labels):
    X_train, X_valid = scaled_train[train_index], scaled_train[valid_index]
    y_train, y_valid = labels[train_index], labels[valid_index]
    

nb_features = 64 # number of features per features type (shape, texture, margin)   
nb_class = len(classes)

# reshape train data
X_train_r = np.zeros((len(X_train), nb_features, 3))
X_train_r[:, :, 0] = X_train[:, :nb_features]
X_train_r[:, :, 1] = X_train[:, nb_features:128]
X_train_r[:, :, 2] = X_train[:, 128:]

# reshape validation data
X_valid_r = np.zeros((len(X_valid), nb_features, 3))
X_valid_r[:, :, 0] = X_valid[:, :nb_features]
X_valid_r[:, :, 1] = X_valid[:, nb_features:128]
X_valid_r[:, :, 2] = X_valid[:, 128:]

# Keras model with one Convolution1D layer
# unfortunately more number of covnolutional layers, filters and filters lenght 
# don't give better accuracy
model = Sequential()
model.add(Convolution1D(nb_filter=512, filter_length=1, input_shape=(nb_features, 3)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dropout(0.4))
model.add(Dense(2048, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(nb_class))
model.add(Activation('softmax'))


y_train = np_utils.to_categorical(y_train, nb_class)
y_valid = np_utils.to_categorical(y_valid, nb_class)

sgd = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9)
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])

nb_epoch = 15
model.fit(X_train_r, y_train, nb_epoch=nb_epoch, validation_data=(X_valid_r, y_valid), batch_size=16)

输出:

Using TensorFlow backend.
Train on 891 samples, validate on 99 samples
Epoch 1/15
891/891 [==============================] - 72s - loss: 4.3931 - acc: 0.0976 - val_loss: 3.6244 - val_acc: 0.4040
Epoch 2/15
891/891 [==============================] - 71s - loss: 1.8143 - acc: 0.6375 - val_loss: 0.4019 - val_acc: 0.9091
Epoch 3/15
891/891 [==============================] - 74s - loss: 0.2671 - acc: 0.9405 - val_loss: 0.2921 - val_acc: 0.9091
Epoch 4/15
891/891 [==============================] - 77s - loss: 0.0977 - acc: 0.9798 - val_loss: 0.2581 - val_acc: 0.9495
Epoch 5/15
891/891 [==============================] - 78s - loss: 0.0496 - acc: 0.9955 - val_loss: 0.0563 - val_acc: 0.9899
Epoch 6/15
891/891 [==============================] - 75s - loss: 0.0074 - acc: 1.0000 - val_loss: 0.0807 - val_acc: 0.9697
Epoch 7/15
891/891 [==============================] - 73s - loss: 0.0068 - acc: 0.9978 - val_loss: 0.0781 - val_acc: 0.9798
Epoch 8/15
891/891 [==============================] - 78s - loss: 0.0124 - acc: 0.9966 - val_loss: 0.0547 - val_acc: 0.9899
Epoch 9/15
891/891 [==============================] - 77s - loss: 0.0079 - acc: 0.9978 - val_loss: 0.0584 - val_acc: 0.9798
Epoch 10/15
891/891 [==============================] - 73s - loss: 0.0031 - acc: 1.0000 - val_loss: 0.0624 - val_acc: 0.9798
Epoch 11/15
891/891 [==============================] - 75s - loss: 0.0090 - acc: 0.9978 - val_loss: 0.0514 - val_acc: 0.9899
Epoch 12/15
891/891 [==============================] - 79s - loss: 0.0046 - acc: 0.9989 - val_loss: 0.0557 - val_acc: 0.9899
Epoch 13/15
891/891 [==============================] - 78s - loss: 0.0020 - acc: 1.0000 - val_loss: 0.0516 - val_acc: 0.9899
Epoch 14/15
891/891 [==============================] - 76s - loss: 0.0013 - acc: 1.0000 - val_loss: 0.0505 - val_acc: 0.9899
Epoch 15/15
891/891 [==============================] - 72s - loss: 0.0013 - acc: 1.0000 - val_loss: 0.0512 - val_acc: 0.9899



转自 : Alex Lazarev Simple Keras 1D CNN + features split

你可能感兴趣的:(#,CNN)