Keras实现简单的Convolution1D

from keras.models import Sequential
from keras.layers import Convolution1D,Activation,Dense,Flatten
from keras import optimizers
import numpy as np
import pandas as pd

###build model###
model = Sequential()
model.add(Convolution1D(64,8,padding='same',name='conv_1',input_shape=(240,1)))
model.add(Activation('relu'))

model.add(Convolution1D(128,8,padding='same',name='conv_2'))
model.add(Activation('relu'))

model.add(Convolution1D(128,8,padding='same',name='conv_3'))
model.add(Activation('relu'))

model.add(Flatten())
model.add(Dense(1))
adam = optimizers.Adam(lr=0.002,clipnorm=1.)
model.compile(adam,loss='mse')

model.summary()

###load data###

datafile = 'pretrate_32.csv'

data_size = 1000
train_size = 600
train_val_size = 800
test_size = 200
steps = 240

def make_data(datafile,steps,data_size):
    data = pd.read_csv(datafile)
    X = []
    Y = []
    for i in range(data_size-steps):
        x = data.loc[i:i+steps-1,['load']].values.tolist()
        y = data.loc[i+steps,['load']].tolist()
        X.append(X)
        Y.append(y)
    return X,Y

x_raw,y_raw = make_data(datafile,steps,data_szie)
xtrain = x_raw[0:train_size]
ytrain = y_raw[0:train_size]
x_train = np.array(xtrain)
y_train = np.array(ytrain)

xval = x_raw[train_size:train_val_size]
yval = y_raw[train_size:train_val_size]
x_val = np.array(xval)
y_val = np.array(yval)

xtest = x_raw[train_val_size:data_size]
ytest = y_raw[train_val_size:data_size]
x_test = np.array(xtest)
y_test = np.array(ytest)

###training###

model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=20,batch_size=240)

 

你可能感兴趣的:(深度学习,深度学习,卷积神经网络,一维卷积,Convolution1D)