Keras搭建神经网络BPNN(回归问题)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets,preprocessing
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import mean_squared_error

import warnings
warnings.filterwarnings('ignore')
#读取数据
dataset = datasets.load_boston()
X,Y = dataset.data,dataset.target
scale_x = preprocessing.MinMaxScaler(feature_range=(0,1))
scale_y = preprocessing.MinMaxScaler(feature_range=(0,1))
x = scale_x.fit_transform(X)
y = scale_y.fit_transform(Y.reshape(-1,1))
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
dim = x.shape[1]
print('--------------------')
model = Sequential()
model.add(Dense(28,activation='relu',input_dim=dim))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='sgd',loss='mse')
model.fit(x_train,y_train,epochs=100,batch_size=20,verbose=0)
predict_train = scale_y.inverse_transform(model.predict(x_train,batch_size=20))
actual_train = scale_y.inverse_transform(y_train)
train_acc = mean_squared_error(actual_train,predict_train)
predict_test = scale_y.inverse_transform(model.predict(x_test,batch_size=20))
actual_test = scale_y.inverse_transform(y_test)
test_acc = mean_squared_error(actual_test,predict_test)
print('网络层数:{} 优化算法:sgd'.format(len(model.layers)))
print('Train mse = ',round(train_acc,4),' Test mse = ',round(test_acc,4))
print('--------------------')
model = Sequential()
model.add(Dense(28,activation='relu',input_dim=dim))
model.add(Dense(28,activation='relu'))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='sgd',loss='mse')
model.fit(x_train,y_train,epochs=100,batch_size=20,verbose=0)
predict_train = scale_y.inverse_transform(model.predict(x_train,batch_size=20))
actual_train = scale_y.inverse_transform(y_train)
train_acc = mean_squared_error(actual_train,predict_train)
predict_test = scale_y.inverse_transform(model.predict(x_test,batch_size=20))
actual_test = scale_y.inverse_transform(y_test)
test_acc = mean_squared_error(actual_test,predict_test)
print('网络层数:{} 优化算法:sgd'.format(len(model.layers)))
print('Train mse = ',round(train_acc,4),' Test mse = ',round(test_acc,4))
【Keras搭建神经网络BPNN(回归问题)】print('--------------------')
model = Sequential()
model.add(Dense(28,activation='relu',input_dim=dim))
model.add(Dense(28,activation='relu'))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='rmsprop',loss='mse')
model.fit(x_train,y_train,epochs=100,batch_size=20,verbose=0)
predict_train = scale_y.inverse_transform(model.predict(x_train,batch_size=20))
actual_train = scale_y.inverse_transform(y_train)
train_acc = mean_squared_error(actual_train,predict_train)
predict_test = scale_y.inverse_transform(model.predict(x_test,batch_size=20))
actual_test = scale_y.inverse_transform(y_test)
test_acc = mean_squared_error(actual_test,predict_test)
print('网络层数:{} 优化算法:rmsprop'.format(len(model.layers)))
print('Train mse = ',round(train_acc,4),' Test mse = ',round(test_acc,4))
print('--------------------')
model = Sequential()
model.add(Dense(28,activation='relu',input_dim=dim))
model.add(Dense(28,activation='relu'))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='adam',loss='mse')
model.fit(x_train,y_train,epochs=100,batch_size=20,verbose=0)
predict_train = scale_y.inverse_transform(model.predict(x_train,batch_size=20))
actual_train = scale_y.inverse_transform(y_train)
train_acc = mean_squared_error(actual_train,predict_train)
predict_test = scale_y.inverse_transform(model.predict(x_test,batch_size=20))
actual_test = scale_y.inverse_transform(y_test)
test_acc = mean_squared_error(actual_test,predict_test)
print('网络层数:{} 优化算法:adam'.format(len(model.layers)))
print('Train mse = ',round(train_acc,4),' Test mse = ',round(test_acc,4))
Keras搭建神经网络BPNN(回归问题)
文章图片

    推荐阅读