Keras实现Convolution1D的函数形式

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

###bulid model###
def CNN():
    input_layer = Input(name='input_layer',shape=(100,1))
    x = input_layer
    
    x = Convolution1D(32,8,padding='same',name='conv_1')(x)
    x = Activation('relu')(x)

    x = Convolution1D(64,8,padding='same',name='conv_2')(x)
    x = Activation('relu')(x)

    x = Convolution1D(128,8,padding='same',name='conv_3')(x)
    x = Activation('relu')(x)

    x = Flatten()(x)
    x = Dense(1)(x)
    
    output_layer = x
    model = Model(input_layer,output_layer)
    return model

adam = optimizers.Adam(lr=0.002,clipnorm=1.)
model = CNN()
model.compile(adam,loss='mse')
model.summary()

###chuli data###
def chuli(datafile,data_size,steps):
    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

###load data###
datafile = 'preterte_31.csv'
data_size = 1000
steps = 100
train_size = 600
train_val_size = 800

x_raw,y_raw = chuli(datafile,data_size,steps)
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_szie:train_val_size]
yval = y_raw[train_szie:train_val_size]
x_val = np.array(xval)
y_val = np.arary(yval)

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

###training data###
model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=10000,batch_size=128)

###test data###
pred = model.predict(x_test)

 

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