[英]Neural network by using keras with python 3.6
作為神經網絡的新手,我正在嘗試以python語言構建神經網絡。
而且我發現keras
軟件包(帶有后端tensorflow)是用python語言構建神經網絡的最簡單方法。
因此,我通過遵循網站( http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/ )上的示例代碼,使用數據構建了如下所示的代碼。
# Create MLP in Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(10)
# load my data 170515_data
dataset = numpy.loadtxt("170515_data.csv", delimiter="\t")
# split into input (X) and output (Y) variables
X = dataset[:,0:5]
Y = dataset[:,5]
# create model
model = Sequential()
model.add(Dense(5, input_dim=5, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='softmax'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=80)
# evaluate the model
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
我直接遵循了示例代碼,並更改了示例代碼以適合我的數據。 但是,我沒有得到想要的結果。 它向我顯示了精度0。
Epoch 150/150
2880/2880 [==============================] - 0s - loss: 801944.8802 - acc: 0.0000e+00
32/2880 [..............................] - ETA: 12s
我的數據中有5列輸入和1個輸出(數據數量:2880行),如下所示,
380 17.00017 9.099979 4 744 889.7142
380 17.27766 9.099979 4 744 886.3223
380 17.49084 9.099979 4 744 884.9797
380 17.56913 9.099979 4 744 884.5085
380 17.69351 9.099979 4 744 883.8726
380 17.67508 9.099979 4 744 885.1917
380 17.64061 9.099979 4 744 887.0289
380 17.44456 9.099979 4 744 888.9369
380 17.27089 9.099979 4 744 890.7271
380 16.95173 9.099979 4 744 894.1897
380 16.43643 9.099979 4 744 898.0527
380 16.14516 9.099979 4 744 898.618
380 16.03739 9.099979 4 744 897.4402
380 16.08521 9.099979 4 744 895.0376
380 16.3003 9.099979 4 744 891.528
380 16.69974 9.099979 4 744 886.5107
380 17.14181 9.099979 4 744 882.9069
380 17.43957 9.099979 4 744 881.3522
380 17.61813 9.099979 4 744 880.3629
380 17.85716 9.179981 4 744 880.0096
380 17.91395 9.299984 4 744 881.5642
380 17.74821 9.399986 4 744 884.4379
380 17.39483 9.519989 4 744 889.0076
380 16.86244 9.639992 4 744 894.3074
380 16.18542 9.759995 4 744 898.5709
380 16 9.879997 4 744 898.3824
380 16.12275 9.879997 4 744 895.1318
380 16.47226 9.879997 4 744 890.5858
380 16.87342 9.879997 4 744 886.3694
380 17.22237 9.839996 4 744 883.8726
380 17.408 9.739994 4 744 882.4357
有人可以給我一個建議或答案嗎?
我在小批量生產中做錯了還是應該設置更多?
請引導我走正確的道路。
提前致謝。
首先,我看到第1列和第4列包含相同的值,因此您可能會看到是否可以獲得包含相關信息的更多數據,否則您可能會丟棄它。
此URL處理的是Pimas Indian數據,這是一個分類問題,您的數據正試圖預測800范圍內的數字,因此可能需要使用其他方法,在這種情況下,是回歸預測建模問題。 嘗試以下模板:
# Vanilla Regression Model
import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
# load dataset
dataframe = pandas.read_csv("file.csv", delim_whitespace=True, header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,1:4] # Discard first column...
Y = dataset[:,5]
# define base model
def baseline_model():
# create model
model = Sequential()
model.add(Dense(4, input_dim=4, init='normal', activation='relu'))
model.add(Dense(1, init='normal'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.