使用软件: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'])
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))
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))