使用keras实现简单的前向全连接神经网络

来源:互联网 发布:苹果排雷软件 编辑:程序博客网 时间:2024/05/16 08:12

转载的原博客地址如下:

http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/

下面是对该博客的几个关键点的记录:

1、可以设置随机数种子的时候,建议设置随机数种子,方便结果的复现.

[python] view plain copy
  1. from keras.models import Sequential  
  2. from keras.layers import Dense  
  3. import numpy  
  4. # fix random seed for reproducibility  
  5. seed = 7  
  6. numpy.random.seed(seed)  

2、

toy 数据的地址如下:http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data

加载数据的代码:

[python] view plain copy
  1. # load pima indians dataset  
  2. dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")  
  3. # split into input (X) and output (Y) variables  
  4. X = dataset[:,0:8]  
  5. Y = dataset[:,8]  


3、定义模型

[python] view plain copy
  1. # create model  
  2. model = Sequential()  
  3. model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))  
  4. model.add(Dense(8, init='uniform', activation='relu'))  
  5. model.add(Dense(1, init='uniform', activation='sigmoid'))  
说明:Dense是全连接网络,只有第一层需要设定输入层的结点个数,其他都不需要


4、编译模型,就是自动将我们定义的代码,使用后端的代码tensorflow或theano去实现

[python] view plain copy
  1. # Compile model  
  2. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])  

5、训练模型

[python] view plain copy
  1. # Fit the model  
  2. model.fit(X, Y, nb_epoch=150, batch_size=10)  

6、评估模型

[python] view plain copy
  1. # evaluate the model  
  2. scores = model.evaluate(X, Y)  
  3. print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))  

7、进行预测

[python] view plain copy
  1. # calculate predictions  
  2. predictions = model.predict(X)  
  3. # round predictions  
  4. rounded = [round(x) for x in predictions]  
  5. print(rounded)  

=============================================================

完整代码如下:

[python] view plain copy
  1. # Create first network with Keras  
  2. from keras.models import Sequential  
  3. from keras.layers import Dense  
  4. import numpy  
  5. import sys  
  6. # fix random seed for reproducibility  
  7. seed = 7  
  8. numpy.random.seed(seed)  
  9. # load pima indians dataset  
  10. dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")  
  11. # split into input (X) and output (Y) variables  
  12. X = dataset[:,0:8]  
  13. Y = dataset[:,8]  
  14. # create model  
  15. model = Sequential()  
  16. model.add(Dense(12, input_dim=8, init='normal', activation='relu'))  
  17. model.add(Dense(8, init='normal', activation='relu'))  
  18. model.add(Dense(1, init='normal', activation='sigmoid'))  
  19. # Compile model  
  20. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy','binary_crossentropy'])  
  21. # Fit the model  
  22. model.fit(X, Y, nb_epoch=150, batch_size=10,  verbose=2)  
  23. # evaluate the model  
  24. scores = model.evaluate(X, Y)  
  25. print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))  
  26. # calculate predictions  
  27. predictions = model.predict(X)  
  28. #print predictions  
  29. # round predictions  
  30. rounded = [round(x) for x in predictions]  
  31. #print(rounded)