(一)BPNN(二分类) 使用软件:Anaconda
Tip: 输出层使用Sigmoid函数
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,Activation
import warnings
warnings.filterwarnings('ignore')
#读取数据
dataset = datasets.load_breast_cancer()
X,y = dataset.data,dataset.target
x = preprocessing.StandardScaler().fit_transform(X)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
#优化算法rmsprop+2层网络
model = Sequential()
model.add(Dense(36,activation='relu',input_dim=30))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
【Keras|Keras搭建神经网络BPNN(分类问题)】model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:2 网络层数:{} 优化算法:rmsprop'.format(len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法rmsprop+3层网络
model = Sequential()
model.add(Dense(36,activation='relu',input_dim=30))
model.add(Dense(36,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:2 网络层数:{} 优化算法:rmsprop'.format(len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法sgd+3层网络
model = Sequential()
model.add(Dense(36,activation='relu',input_dim=30))
model.add(Dense(36,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='sgd',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:2 网络层数:{} 优化算法:sgd'.format(len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法adam+3层网络
model = Sequential()
model.add(Dense(36,activation='relu',input_dim=30))
model.add(Dense(36,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:2 网络层数:{} 优化算法:adam'.format(len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
(二)BPNN(多分类) Tip: 输出层使用Softmax函数
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,Activation
from keras.utils import to_categorical
import warnings
warnings.filterwarnings('ignore')
#读取数据
dataset = datasets.load_iris()
X,Y = dataset.data,dataset.target
num = len(np.unique(Y))
x = preprocessing.StandardScaler().fit_transform(X)
y = to_categorical(Y,num_classes=num)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
#优化算法rmsprop+2层网络
model = Sequential()
model.add(Dense(12,activation='relu',input_dim=x.shape[1]))
model.add(Dense(num,activation='softmax'))
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:{} 网络层数:{} 优化算法:rmsprop'.format(num,len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法rmsprop+3层网络
model = Sequential()
model.add(Dense(12,activation='relu',input_dim=x.shape[1]))
model.add(Dense(12,activation='relu'))
model.add(Dense(num,activation='softmax'))
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:{} 网络层数:{} 优化算法:rmsprop'.format(num,len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法sgd+3层网络
model = Sequential()
model.add(Dense(12,activation='relu',input_dim=x.shape[1]))
model.add(Dense(12,activation='relu'))
model.add(Dense(num,activation='softmax'))
model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:{} 网络层数:{} 优化算法:sgd'.format(num,len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
#优化算法adam+3层网络
model = Sequential()
model.add(Dense(12,activation='relu',input_dim=x.shape[1]))
model.add(Dense(12,activation='relu'))
model.add(Dense(num,activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=100,verbose=0,batch_size=50)
loss,train_acc = model.evaluate(x_train,y_train,batch_size=50)
predict_train = model.predict_classes(x_train,batch_size=50).ravel()
loss,test_acc = model.evaluate(x_test,y_test,batch_size=50)
predict_test = model.predict_classes(x_test,batch_size=50).ravel()
print('分类数:{} 网络层数:{} 优化算法:adam'.format(num,len(model.layers)))
print('Train acc = ',round(train_acc,4),' Test acc = ',round(test_acc,4))
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)