作为神经网络的新手,我试图用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
有人可以给我一个建议或答案吗??
我是否在迷你游戏中做错了,或者我应该设置更多?
请引导我走正确的道路。
提前致谢。
答案 0 :(得分:2)
首先,我看到第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()))