如何利用神经网络做回归问题(全连接以及一维卷积)

源码、留给自己看的
博客地址:http://blog.csdn.net/shizhengxin123/article/details/76147022
1、全连接 回归

from keras.layers import Input, Dense
from keras.models import Model
from keras.layers.core import Flatten, Dense, Dropout,Activation
from keras.layers.normalization import  BatchNormalization  as bn
from  keras.layers.pooling import MaxPooling1D as pool
# This returns a tensor
inputs = Input(shape=(72,))
import  keras.utils 
# a layer instance is callable on a tensor, and returns a tensor
x = Dense(256)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(256)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(128)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(128)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(128)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(128)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(64)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(64)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(64)(x)
x = bn()(x)
x = Activation('relu')(x)
x = Dense(32)(x)
x = bn()(x)
x = Activation('relu')(x)
predictions = Dense(1, activation='linear')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(input=inputs, output=predictions)
model.compile(optimizer='rmsprop',
              loss='mean_squared_error',
             metrics=['mae', 'acc'])

model.fit(train_x,train_y, validation_data=(test_x, test_y),
          nb_epoch=40, batch_size=10000)

2、一维卷积

from keras.layers import Input, Dense
from keras.models import Model
from keras.layers.core import Flatten, Dense, Dropout,Activation
from keras.layers.normalization import  BatchNormalization  as bn
from  keras.layers.pooling import MaxPooling1D as pool
from keras.layers.convolutional import Conv1D as cnn1
# This returns a tensor
inputs = Input(shape=(72,1))
import  keras.utils 
# a layer instance is callable on a tensor, and returns a tensor
x = cnn1(64,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = cnn1(64,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = pool()(x)
x = cnn1(128,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = cnn1(128,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = pool()(x)
x = cnn1(256,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)

x = cnn1(256,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)

x = cnn1(256,3)(inputs)
x = bn()(x)
x = Activation('relu')(x)
x = pool()(x)
# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)

# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)
# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)
# x = pool()(x)

# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)
# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)
# x = cnn1(512,3)(inputs)
# x = bn()(x)
# x = Activation('relu')(x)
# x = pool()(x)
x = Flatten()(x)
x = Dense(512)(x)
x = bn()(x)
x = Activation('relu')(x)
predictions = Dense(1, activation='linear')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(input=inputs, output=predictions)
model.compile(optimizer='rmsprop',
              loss='mean_squared_error',
             metrics=['mae', 'acc'])
model.fit(train_x,train_y, validation_data=(test_x, test_y),
          nb_epoch=40, batch_size=10000)
# model.save_weights('/home/etcp/szx/flower_data/third_park_predict.h5')

你可能感兴趣的:(cnn神经网络)