Java教程

第一个神经网络程序实战

本文主要是介绍第一个神经网络程序实战,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense

np.random.seed(10)  # 指定乱数种子
# 载入数据集 
df = pd.read_csv("D:/Keras/Ch05/diabetes.csv")

dataset = df.values
np.random.shuffle(dataset)  # 使用乱数打乱数据
# 分割成输入的训练数据和标签数据 
X = dataset[:, 0:8]
Y = dataset[:, 8]
# 定义模型
model = Sequential()
model.add(Dense(10, input_shape=(8,), activation="relu"))
model.add(Dense(8, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.summary()  #  显示模型摘要信息
#编译模型
model.compile(loss="binary_crossentropy", optimizer="sgd",
              metrics=["accuracy"])
# 训练模型
model.fit(X, Y, epochs=150, batch_size=10)
# 评估模型
loss, accuracy = model.evaluate(X, Y)
print("准确度 = {:.2f}".format(accuracy))

  

 

输出结果为:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_3 (Dense)              (None, 10)                90        
_________________________________________________________________
dense_4 (Dense)              (None, 8)                 88        
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 9         
=================================================================
Total params: 187
Trainable params: 187
Non-trainable params: 0
_________________________________________________________________
Epoch 1/150
77/77 [==============================] - 0s 565us/step - loss: 1.5253 - accuracy: 0.6185
Epoch 2/150
77/77 [==============================] - 0s 559us/step - loss: 0.6898 - accuracy: 0.6419
Epoch 3/150
77/77 [==============================] - 0s 562us/step - loss: 0.6524 - accuracy: 0.6510
Epoch 4/150
77/77 [==============================] - 0s 569us/step - loss: 0.6511 - accuracy: 0.6654
Epoch 5/150
77/77 [==============================] - 0s 551us/step - loss: 0.6418 - accuracy: 0.6602
Epoch 6/150
77/77 [==============================] - 0s 517us/step - loss: 0.6401 - accuracy: 0.6562
Epoch 7/150
77/77 [==============================] - 0s 538us/step - loss: 0.6324 - accuracy: 0.6680
Epoch 8/150
77/77 [==============================] - 0s 516us/step - loss: 0.6289 - accuracy: 0.6549
Epoch 9/150
77/77 [==============================] - 0s 520us/step - loss: 0.6264 - accuracy: 0.6732
Epoch 10/150
77/77 [==============================] - 0s 535us/step - loss: 0.6262 - accuracy: 0.6810
Epoch 11/150
77/77 [==============================] - 0s 576us/step - loss: 0.6207 - accuracy: 0.6745
Epoch 12/150
77/77 [==============================] - 0s 552us/step - loss: 0.6194 - accuracy: 0.6628
Epoch 13/150
77/77 [==============================] - 0s 512us/step - loss: 0.6214 - accuracy: 0.6615
Epoch 14/150
77/77 [==============================] - 0s 538us/step - loss: 0.6227 - accuracy: 0.6680
Epoch 15/150
77/77 [==============================] - 0s 556us/step - loss: 0.6210 - accuracy: 0.6628
Epoch 16/150
77/77 [==============================] - 0s 467us/step - loss: 0.6353 - accuracy: 0.6641
Epoch 17/150
77/77 [==============================] - 0s 523us/step - loss: 0.6283 - accuracy: 0.6693
Epoch 18/150
77/77 [==============================] - 0s 525us/step - loss: 0.6269 - accuracy: 0.6680
Epoch 19/150
77/77 [==============================] - 0s 551us/step - loss: 0.6204 - accuracy: 0.6615
Epoch 20/150
77/77 [==============================] - 0s 571us/step - loss: 0.6191 - accuracy: 0.6680
Epoch 21/150
77/77 [==============================] - 0s 567us/step - loss: 0.6166 - accuracy: 0.6706
Epoch 22/150
77/77 [==============================] - 0s 521us/step - loss: 0.6219 - accuracy: 0.6693
Epoch 23/150
77/77 [==============================] - 0s 640us/step - loss: 0.6199 - accuracy: 0.6823
Epoch 24/150
77/77 [==============================] - 0s 551us/step - loss: 0.6170 - accuracy: 0.6641
Epoch 25/150
77/77 [==============================] - 0s 544us/step - loss: 0.6129 - accuracy: 0.6823
Epoch 26/150
77/77 [==============================] - 0s 553us/step - loss: 0.6132 - accuracy: 0.6602
Epoch 27/150
77/77 [==============================] - 0s 499us/step - loss: 0.6150 - accuracy: 0.6758
Epoch 28/150
77/77 [==============================] - 0s 536us/step - loss: 0.6105 - accuracy: 0.6784
Epoch 29/150
77/77 [==============================] - 0s 518us/step - loss: 0.6097 - accuracy: 0.6706
Epoch 30/150
77/77 [==============================] - 0s 512us/step - loss: 0.6135 - accuracy: 0.6823
Epoch 31/150
77/77 [==============================] - 0s 528us/step - loss: 0.6114 - accuracy: 0.6680
Epoch 32/150
77/77 [==============================] - 0s 499us/step - loss: 0.6040 - accuracy: 0.6784
Epoch 33/150
77/77 [==============================] - 0s 514us/step - loss: 0.6050 - accuracy: 0.6771
Epoch 34/150
77/77 [==============================] - 0s 499us/step - loss: 0.6039 - accuracy: 0.6745
Epoch 35/150
77/77 [==============================] - 0s 519us/step - loss: 0.6071 - accuracy: 0.6745
Epoch 36/150
77/77 [==============================] - 0s 635us/step - loss: 0.6013 - accuracy: 0.6849
Epoch 37/150
77/77 [==============================] - 0s 536us/step - loss: 0.6017 - accuracy: 0.6875
Epoch 38/150
77/77 [==============================] - 0s 563us/step - loss: 0.6068 - accuracy: 0.6654
Epoch 39/150
77/77 [==============================] - 0s 512us/step - loss: 0.5952 - accuracy: 0.6849
Epoch 40/150
77/77 [==============================] - 0s 578us/step - loss: 0.5971 - accuracy: 0.6914
Epoch 41/150
77/77 [==============================] - 0s 503us/step - loss: 0.5950 - accuracy: 0.6888
Epoch 42/150
77/77 [==============================] - 0s 530us/step - loss: 0.5968 - accuracy: 0.6797
Epoch 43/150
77/77 [==============================] - 0s 534us/step - loss: 0.6038 - accuracy: 0.6771
Epoch 44/150
77/77 [==============================] - 0s 602us/step - loss: 0.5965 - accuracy: 0.6810
Epoch 45/150
77/77 [==============================] - 0s 625us/step - loss: 0.5972 - accuracy: 0.6667
Epoch 46/150
77/77 [==============================] - 0s 499us/step - loss: 0.5971 - accuracy: 0.6862
Epoch 47/150
77/77 [==============================] - 0s 541us/step - loss: 0.5886 - accuracy: 0.6810
Epoch 48/150
77/77 [==============================] - 0s 503us/step - loss: 0.5946 - accuracy: 0.6810
Epoch 49/150
77/77 [==============================] - 0s 513us/step - loss: 0.5918 - accuracy: 0.6823
Epoch 50/150
77/77 [==============================] - 0s 499us/step - loss: 0.5967 - accuracy: 0.6797
Epoch 51/150
77/77 [==============================] - 0s 547us/step - loss: 0.5931 - accuracy: 0.6836
Epoch 52/150
77/77 [==============================] - 0s 538us/step - loss: 0.5895 - accuracy: 0.6888
Epoch 53/150
77/77 [==============================] - 0s 503us/step - loss: 0.5946 - accuracy: 0.6914
Epoch 54/150
77/77 [==============================] - 0s 535us/step - loss: 0.5865 - accuracy: 0.6901
Epoch 55/150
77/77 [==============================] - 0s 451us/step - loss: 0.5908 - accuracy: 0.6875
Epoch 56/150
77/77 [==============================] - 0s 565us/step - loss: 0.5860 - accuracy: 0.6953
Epoch 57/150
77/77 [==============================] - 0s 459us/step - loss: 0.5850 - accuracy: 0.6953
Epoch 58/150
77/77 [==============================] - 0s 508us/step - loss: 0.5912 - accuracy: 0.6836
Epoch 59/150
77/77 [==============================] - 0s 567us/step - loss: 0.5830 - accuracy: 0.6979
Epoch 60/150
77/77 [==============================] - 0s 643us/step - loss: 0.5936 - accuracy: 0.6914
Epoch 61/150
77/77 [==============================] - 0s 617us/step - loss: 0.5887 - accuracy: 0.6888
Epoch 62/150
77/77 [==============================] - 0s 591us/step - loss: 0.5870 - accuracy: 0.6888
Epoch 63/150
77/77 [==============================] - 0s 564us/step - loss: 0.5773 - accuracy: 0.6992
Epoch 64/150
77/77 [==============================] - 0s 564us/step - loss: 0.5802 - accuracy: 0.6953
Epoch 65/150
77/77 [==============================] - 0s 596us/step - loss: 0.5832 - accuracy: 0.6862
Epoch 66/150
77/77 [==============================] - 0s 577us/step - loss: 0.5843 - accuracy: 0.6927
Epoch 67/150
77/77 [==============================] - 0s 490us/step - loss: 0.5834 - accuracy: 0.6901
Epoch 68/150
77/77 [==============================] - 0s 564us/step - loss: 0.5845 - accuracy: 0.6784
Epoch 69/150
77/77 [==============================] - 0s 611us/step - loss: 0.5894 - accuracy: 0.6875
Epoch 70/150
77/77 [==============================] - 0s 475us/step - loss: 0.5910 - accuracy: 0.6979
Epoch 71/150
77/77 [==============================] - 0s 554us/step - loss: 0.5833 - accuracy: 0.6836
Epoch 72/150
77/77 [==============================] - 0s 539us/step - loss: 0.5825 - accuracy: 0.7005
Epoch 73/150
77/77 [==============================] - 0s 499us/step - loss: 0.5840 - accuracy: 0.6953
Epoch 74/150
77/77 [==============================] - 0s 577us/step - loss: 0.5813 - accuracy: 0.6940
Epoch 75/150
77/77 [==============================] - 0s 590us/step - loss: 0.5747 - accuracy: 0.6914
Epoch 76/150
77/77 [==============================] - 0s 486us/step - loss: 0.5875 - accuracy: 0.6862
Epoch 77/150
77/77 [==============================] - 0s 528us/step - loss: 0.5818 - accuracy: 0.6875
Epoch 78/150
77/77 [==============================] - 0s 564us/step - loss: 0.5893 - accuracy: 0.6849
Epoch 79/150
77/77 [==============================] - 0s 551us/step - loss: 0.5812 - accuracy: 0.6927
Epoch 80/150
77/77 [==============================] - 0s 512us/step - loss: 0.5847 - accuracy: 0.6810
Epoch 81/150
77/77 [==============================] - 0s 577us/step - loss: 0.5828 - accuracy: 0.6953
Epoch 82/150
77/77 [==============================] - 0s 518us/step - loss: 0.5815 - accuracy: 0.6862
Epoch 83/150
77/77 [==============================] - 0s 591us/step - loss: 0.5846 - accuracy: 0.6927
Epoch 84/150
77/77 [==============================] - 0s 591us/step - loss: 0.5925 - accuracy: 0.6901
Epoch 85/150
77/77 [==============================] - 0s 564us/step - loss: 0.5781 - accuracy: 0.6953
Epoch 86/150
77/77 [==============================] - 0s 564us/step - loss: 0.5832 - accuracy: 0.6862
Epoch 87/150
77/77 [==============================] - 0s 512us/step - loss: 0.5801 - accuracy: 0.6979
Epoch 88/150
77/77 [==============================] - 0s 542us/step - loss: 0.5851 - accuracy: 0.6836
Epoch 89/150
77/77 [==============================] - 0s 531us/step - loss: 0.5755 - accuracy: 0.6927
Epoch 90/150
77/77 [==============================] - 0s 564us/step - loss: 0.5749 - accuracy: 0.7005
Epoch 91/150
77/77 [==============================] - 0s 617us/step - loss: 0.5786 - accuracy: 0.6979
Epoch 92/150
77/77 [==============================] - 0s 551us/step - loss: 0.5817 - accuracy: 0.6927
Epoch 93/150
77/77 [==============================] - 0s 564us/step - loss: 0.5834 - accuracy: 0.6927
Epoch 94/150
77/77 [==============================] - 0s 569us/step - loss: 0.5750 - accuracy: 0.6992
Epoch 95/150
77/77 [==============================] - 0s 486us/step - loss: 0.5847 - accuracy: 0.6758
Epoch 96/150
77/77 [==============================] - 0s 538us/step - loss: 0.5781 - accuracy: 0.6927
Epoch 97/150
77/77 [==============================] - 0s 525us/step - loss: 0.5804 - accuracy: 0.6927
Epoch 98/150
77/77 [==============================] - 0s 525us/step - loss: 0.5749 - accuracy: 0.6992
Epoch 99/150
77/77 [==============================] - 0s 590us/step - loss: 0.5779 - accuracy: 0.6927
Epoch 100/150
77/77 [==============================] - 0s 523us/step - loss: 0.5763 - accuracy: 0.6966
Epoch 101/150
77/77 [==============================] - 0s 591us/step - loss: 0.5765 - accuracy: 0.7005
Epoch 102/150
77/77 [==============================] - 0s 499us/step - loss: 0.5816 - accuracy: 0.6966
Epoch 103/150
77/77 [==============================] - 0s 542us/step - loss: 0.5790 - accuracy: 0.6940
Epoch 104/150
77/77 [==============================] - 0s 577us/step - loss: 0.5772 - accuracy: 0.6966
Epoch 105/150
77/77 [==============================] - 0s 591us/step - loss: 0.5778 - accuracy: 0.6966
Epoch 106/150
77/77 [==============================] - 0s 538us/step - loss: 0.5806 - accuracy: 0.6927
Epoch 107/150
77/77 [==============================] - 0s 551us/step - loss: 0.5715 - accuracy: 0.7005
Epoch 108/150
77/77 [==============================] - 0s 486us/step - loss: 0.5808 - accuracy: 0.6914
Epoch 109/150
77/77 [==============================] - 0s 577us/step - loss: 0.5754 - accuracy: 0.6979
Epoch 110/150
77/77 [==============================] - 0s 538us/step - loss: 0.5746 - accuracy: 0.7005
Epoch 111/150
77/77 [==============================] - 0s 569us/step - loss: 0.5778 - accuracy: 0.7018
Epoch 112/150
77/77 [==============================] - 0s 540us/step - loss: 0.5824 - accuracy: 0.6940
Epoch 113/150
77/77 [==============================] - 0s 604us/step - loss: 0.5740 - accuracy: 0.7018
Epoch 114/150
77/77 [==============================] - 0s 512us/step - loss: 0.5755 - accuracy: 0.6966
Epoch 115/150
77/77 [==============================] - 0s 577us/step - loss: 0.5777 - accuracy: 0.6940
Epoch 116/150
77/77 [==============================] - 0s 499us/step - loss: 0.5811 - accuracy: 0.6901
Epoch 117/150
77/77 [==============================] - 0s 538us/step - loss: 0.5780 - accuracy: 0.6992
Epoch 118/150
77/77 [==============================] - 0s 577us/step - loss: 0.5777 - accuracy: 0.6888
Epoch 119/150
77/77 [==============================] - 0s 617us/step - loss: 0.5762 - accuracy: 0.6927
Epoch 120/150
77/77 [==============================] - 0s 565us/step - loss: 0.5716 - accuracy: 0.7031
Epoch 121/150
77/77 [==============================] - 0s 565us/step - loss: 0.5739 - accuracy: 0.7005
Epoch 122/150
77/77 [==============================] - 0s 656us/step - loss: 0.5792 - accuracy: 0.6836
Epoch 123/150
77/77 [==============================] - 0s 604us/step - loss: 0.5813 - accuracy: 0.7005
Epoch 124/150
77/77 [==============================] - 0s 533us/step - loss: 0.5778 - accuracy: 0.6966
Epoch 125/150
77/77 [==============================] - 0s 509us/step - loss: 0.5758 - accuracy: 0.6992
Epoch 126/150
77/77 [==============================] - 0s 525us/step - loss: 0.5722 - accuracy: 0.6914
Epoch 127/150
77/77 [==============================] - 0s 525us/step - loss: 0.5797 - accuracy: 0.6901
Epoch 128/150
77/77 [==============================] - 0s 559us/step - loss: 0.5728 - accuracy: 0.6966
Epoch 129/150
77/77 [==============================] - 0s 559us/step - loss: 0.5726 - accuracy: 0.6992
Epoch 130/150
77/77 [==============================] - 0s 512us/step - loss: 0.5784 - accuracy: 0.6966
Epoch 131/150
77/77 [==============================] - 0s 551us/step - loss: 0.5750 - accuracy: 0.6992
Epoch 132/150
77/77 [==============================] - 0s 525us/step - loss: 0.5745 - accuracy: 0.6992
Epoch 133/150
77/77 [==============================] - 0s 499us/step - loss: 0.5714 - accuracy: 0.6940
Epoch 134/150
77/77 [==============================] - 0s 549us/step - loss: 0.5749 - accuracy: 0.6966
Epoch 135/150
77/77 [==============================] - 0s 501us/step - loss: 0.5780 - accuracy: 0.6992
Epoch 136/150
77/77 [==============================] - 0s 538us/step - loss: 0.5776 - accuracy: 0.7018
Epoch 137/150
77/77 [==============================] - 0s 546us/step - loss: 0.5731 - accuracy: 0.6979
Epoch 138/150
77/77 [==============================] - 0s 577us/step - loss: 0.5720 - accuracy: 0.7031
Epoch 139/150
77/77 [==============================] - 0s 525us/step - loss: 0.5760 - accuracy: 0.6927
Epoch 140/150
77/77 [==============================] - 0s 545us/step - loss: 0.5751 - accuracy: 0.6979
Epoch 141/150
77/77 [==============================] - 0s 551us/step - loss: 0.5738 - accuracy: 0.7057
Epoch 142/150
77/77 [==============================] - 0s 524us/step - loss: 0.5737 - accuracy: 0.7018
Epoch 143/150
77/77 [==============================] - 0s 551us/step - loss: 0.5740 - accuracy: 0.6953
Epoch 144/150
77/77 [==============================] - 0s 514us/step - loss: 0.5751 - accuracy: 0.6940
Epoch 145/150
77/77 [==============================] - 0s 511us/step - loss: 0.5773 - accuracy: 0.6979
Epoch 146/150
77/77 [==============================] - 0s 503us/step - loss: 0.5705 - accuracy: 0.6992
Epoch 147/150
77/77 [==============================] - 0s 494us/step - loss: 0.5738 - accuracy: 0.6979
Epoch 148/150
77/77 [==============================] - 0s 513us/step - loss: 0.5724 - accuracy: 0.6862
Epoch 149/150
77/77 [==============================] - 0s 512us/step - loss: 0.5743 - accuracy: 0.6940
Epoch 150/150
77/77 [==============================] - 0s 480us/step - loss: 0.5691 - accuracy: 0.6979
24/24 [==============================] - 0s 489us/step - loss: 0.5614 - accuracy: 0.7018
准确度 = 0.70

 

这篇关于第一个神经网络程序实战的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!