關於Keras:
Keras是一個高級神經網絡API,用Python編寫,可以在TensorFlow,CNTK或Theano之上運行。後端
使用一下命令安裝:網絡
pip install keras
在Keras實施深度學習的步驟dom
使用Dense類描述徹底鏈接的層。 咱們能夠指定層中神經元的數量做爲第一個參數,將初始化方法指定爲第二個參數做爲init,並使用激活參數肯定激活函數。 既然定義了模型,咱們就能夠編譯它。 編譯模型使用封面下的高效數字庫(所謂的後端),如Theano或TensorFlow。 到目前爲止,咱們已經定義了咱們的模型並將其編譯爲有效計算。 如今是時候在PIMA數據上運行模型了。 咱們能夠經過調用模型上的fit()函數來訓練或擬合咱們的數據模型。函數
import numpy as np import pandas as pd import keras from keras.models import Sequential from keras.layers import Dense # Initializing the seed value to a integer. seed = 7 np.random.seed(seed) # Loading the data set (PIMA Diabetes Dataset) dataset = pd.read_csv(r'C:/Users/Administrator/Desktop/pima-indians-diabetes.csv') dataset.head() dataset.shape # Loading the input values to X and Label values Y using slicing. X = np.mat(dataset.iloc[:, 0:8]) Y = np.mat(dataset.iloc[:,8]).reshape(-1,1) # Initializing the Sequential model from KERAS. model = Sequential() # Creating a 16 neuron hidden layer with Linear Rectified activation function. model.add(Dense(16, input_dim=8, init='uniform', activation='relu')) # Creating a 8 neuron hidden layer. model.add(Dense(8, init='uniform', activation='relu')) # Adding a output layer. model.add(Dense(1, init='uniform', activation='sigmoid')) # Compiling the model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fitting the model history=model.fit(X, Y, nb_epoch=150, batch_size=10) scores = model.evaluate(X, Y) print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
import matplotlib.pyplot as plt loss =history.history['loss'] val_loss = history.history['acc'] epochs = range(1, len(loss) + 1) plt.figure(figsize=(10,6)) plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'r', label='acc') plt.legend() plt.show()