【翻译自 :How to Develop a Neural Net for Predicting Disturbances in the Ionosphere】

【说明:Jason Brownlee PhD大神的文章个人很喜欢,所以闲暇时间里会做一点翻译和学习实践的工作,这里是相应工作的实践记录,希望能帮到有需要的人!】

为新数据集开发神经网络预测模型可能具有挑战性。

一种方法是首先检查数据集并为可能使用的模型开发思路,然后探索数据集上简单模型的学习动态,然后最后使用健壮的测试工具为数据集开发和调整模型。

此过程可用于为分类和回归预测建模问题开发有效的神经网络模型。

在本教程中,您将发现如何为电离层二进制分类数据集开发多层Perceptron神经网络模型。完成本教程后,您将知道:

如何加载和汇总电离层数据集以及如何使用结果建议要使用的数据准备和模型配置。
如何探索数据集上简单MLP模型的学习动态。
如何开发出对模型性能的可靠估计,调整模型性能以及对新数据进行预测。

教程概述

本教程分为四个部分。 他们是:

电离层二进制分类数据集
神经网络动态学习
评估和调整MLP模型
最终模型和做出预测

电离层二进制分类数据集

第一步是定义和探索数据集。我们将使用“电离层”标准二进制分类数据集。该数据集涉及预测某个结构是否在大气中或不给定雷达回波。您可以在此处了解有关数据集的更多信息:

电离层数据集(ionosphere.csv)
                                                                                 电离层数据集详细信息(ionosphere.names)
       您可以在下面看到数据集的前几行。

1,0,0.99539,-0.05889,0.85243,0.02306,0.83398,-0.37708,1,0.03760,0.85243,-0.17755,0.59755,-0.44945,0.60536,-0.38223,0.84356,-0.38542,0.58212,-0.32192,0.56971,-0.29674,0.36946,-0.47357,0.56811,-0.51171,0.41078,-0.46168,0.21266,-0.34090,0.42267,-0.54487,0.18641,-0.45300,g
1,0,1,-0.18829,0.93035,-0.36156,-0.10868,-0.93597,1,-0.04549,0.50874,-0.67743,0.34432,-0.69707,-0.51685,-0.97515,0.05499,-0.62237,0.33109,-1,-0.13151,-0.45300,-0.18056,-0.35734,-0.20332,-0.26569,-0.20468,-0.18401,-0.19040,-0.11593,-0.16626,-0.06288,-0.13738,-0.02447,b
1,0,1,-0.03365,1,0.00485,1,-0.12062,0.88965,0.01198,0.73082,0.05346,0.85443,0.00827,0.54591,0.00299,0.83775,-0.13644,0.75535,-0.08540,0.70887,-0.27502,0.43385,-0.12062,0.57528,-0.40220,0.58984,-0.22145,0.43100,-0.17365,0.60436,-0.24180,0.56045,-0.38238,g
1,0,1,-0.45161,1,1,0.71216,-1,0,0,0,0,0,0,-1,0.14516,0.54094,-0.39330,-1,-0.54467,-0.69975,1,0,0,1,0.90695,0.51613,1,1,-0.20099,0.25682,1,-0.32382,1,b
1,0,1,-0.02401,0.94140,0.06531,0.92106,-0.23255,0.77152,-0.16399,0.52798,-0.20275,0.56409,-0.00712,0.34395,-0.27457,0.52940,-0.21780,0.45107,-0.17813,0.05982,-0.35575,0.02309,-0.52879,0.03286,-0.65158,0.13290,-0.53206,0.02431,-0.62197,-0.05707,-0.59573,-0.04608,-0.65697,g
...

我们可以看到这些值都是数字,并且可能在[-1,1]范围内。 这表明可能不需要某种缩放比例。我们还可以看到标签是一个字符串(“ g”和“ b”),这表明在拟合模型之前需要将值编码为0和1。我们可以直接从URL将数据集作为pandas DataFrame加载; 例如:

# load the ionosphere dataset and summarize the shape
from pandas import read_csv
# define the location of the dataset
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
# load the dataset
df = read_csv(url, header=None)
# summarize shape
print(df.shape)

运行示例将直接从URL加载数据集并报告数据集的形状。

在这种情况下,我们可以看到该数据集具有35个变量(34个输入和一个输出),并且该数据集具有351行数据。

对于神经网络来说,这不是很多数据行,这表明一个小型的网络(可能带有正则化)将是合适的。

这也表明使用k折交叉验证是一个好主意,因为与火车/测试拆分相比,它将提供对模型性能的更可靠的估计,并且因为单个模型将适合秒而不是数小时或数天。 最大的数据集。

(351, 35)

接下来,我们可以通过查看摘要统计信息和数据图来了解有关数据集的更多信息。

# show summary statistics and plots of the ionosphere dataset
from pandas import read_csv
from matplotlib import pyplot
# define the location of the dataset
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
# load the dataset
df = read_csv(url, header=None)
# show summary statistics
print(df.describe())
# plot histograms
df.hist()
pyplot.show()

运行示例之前,先加载数据,然后输出每个变量的摘要统计信息。

我们可以看到每个变量的平均值都在几十个范围内,范围从-1到1。这证实了可能不需要缩放数据。

               0      1           2   ...          31          32          33
count  351.000000  351.0  351.000000  ...  351.000000  351.000000  351.000000
mean     0.891738    0.0    0.641342  ...   -0.003794    0.349364    0.014480
std      0.311155    0.0    0.497708  ...    0.513574    0.522663    0.468337
min      0.000000    0.0   -1.000000  ...   -1.000000   -1.000000   -1.000000
25%      1.000000    0.0    0.472135  ...   -0.242595    0.000000   -0.165350
50%      1.000000    0.0    0.871110  ...    0.000000    0.409560    0.000000
75%      1.000000    0.0    1.000000  ...    0.200120    0.813765    0.171660
max      1.000000    0.0    1.000000  ...    1.000000    1.000000    1.000000

然后为每个变量创建一个直方图。

我们可以看到许多变量具有高斯或类高斯分布。

在每个变量上使用幂变换,以使概率分布减少偏斜,这可能会有所改善,这可能会改善模型性能。

现在我们已经熟悉了数据集,让我们探讨如何开发神经网络模型。

神经网络动态学习

我们将使用TensorFlow为数据集开发一个多层感知器(MLP)模型。我们不知道学习超参数的哪种模型架构对这个数据集将是好的还是最好的,所以我们必须进行实验并发现什么是行之有效的。假设数据集很小,则小批量可能是个好主意,例如16或32行。入门时,使用Adam版本的随机梯度下降法是个好主意,因为它会自动适应学习率,并且在大多数数据集上都能很好地工作。在认真评估模型之前,最好回顾一下学习动态并调整模型体系结构和学习配置,直到我们拥有稳定的学习动态,然后再充分利用模型。我们可以通过简单的训练/测试数据拆分并查看学习曲线图来实现。这将帮助我们了解我们是学习过度还是学习不足;然后我们可以相应地调整配置。首先,我们必须确保所有输入变量均为浮点值,并将目标标签编码为整数值0和1。

# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)

接下来,我们可以将数据集分为输入和输出变量,然后分为67/33训练和测试集。

# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

我们可以定义一个最小的MLP模型。 在这种情况下,我们将使用一个包含10个节点的隐藏层和一个输出层(任意选择)。 我们将在隐藏层中使用ReLU激活功能和“ he_normal”权重初始化,因为它们是一种很好的做法。该模型的输出是用于二进制分类的S型激活,我们将使二进制交叉熵损失最小化。

# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')

我们将模型拟合为200个训练时期(任意选择),批处理大小为32,因为它是一个很小的数据集。我们正在原始数据上拟合模型,我们认为这可能是个好主意,但这是重要的起点。

# fit the model
history = model.fit(X_train, y_train, epochs=200, batch_size=32, verbose=0, validation_data=(X_test,y_test))

训练结束时,我们将评估模型在测试数据集上的性能,并将性能报告为分类准确性。

# predict test set
yhat = model.predict_classes(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % score)

最后,我们将在训练过程中在火车和测试集上绘制交叉熵损失的学习曲线。

# plot learning curves
pyplot.title('Learning Curves')
pyplot.xlabel('Epoch')
pyplot.ylabel('Cross Entropy')
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='val')
pyplot.legend()
pyplot.show()

综上所述,下面列出了在电离层数据集上评估我们的第一个MLP的完整示例。

# fit a simple mlp model on the ionosphere and review learning curves
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
history = model.fit(X_train, y_train, epochs=200, batch_size=32, verbose=0, validation_data=(X_test,y_test))
# predict test set
yhat = model.predict_classes(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % score)
# plot learning curves
pyplot.title('Learning Curves')
pyplot.xlabel('Epoch')
pyplot.ylabel('Cross Entropy')
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='val')
pyplot.legend()
pyplot.show()

运行示例首先使模型适合训练数据集,然后报告测试数据集的分类准确性。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到该模型实现了约88%的准确度,这是我们可能可以改进的良好性能基准。

Accuracy: 0.888

然后创建列车和测试装置上的损耗线图。

我们可以看到该模型似乎收敛了,但是已经过度拟合了训练数据集。

让我们尝试增加模型的容量。对于相同的学习超参数,这将减慢学习速度,并可能提供更好的准确性。我们将添加具有八个节点的第二个隐藏层,可以任意选择。

# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(1, activation='sigmoid'))

完整实例如下:

# fit a deeper mlp model on the ionosphere and review learning curves
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
history = model.fit(X_train, y_train, epochs=200, batch_size=32, verbose=0, validation_data=(X_test,y_test))
# predict test set
yhat = model.predict_classes(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % score)
# plot learning curves
pyplot.title('Learning Curves')
pyplot.xlabel('Epoch')
pyplot.ylabel('Cross Entropy')
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='val')
pyplot.legend()

运行示例首先使模型适合训练数据集,然后报告测试数据集的准确性。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,尽管训练/测试拆分的高方差意味着该评估不可靠,但我们可以看到准确性略有提高,达到了约93%。

Accuracy: 0.931

然后绘制训练和测试装置上的损耗学习曲线。 我们可以看到模型仍然显示出过拟合行为。

最后,我们可以尝试更广泛的网络。我们将第一隐藏层中的节点数从10增加到50,第二隐藏层中的节点数从8增加到10。这将增加模型的容量,减慢学习速度,并可能进一步改善结果。

# define model
model = Sequential()
model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(1, activation='sigmoid'))

我们还将训练单元的数量从200个减少到100个。

# fit the model
history = model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0, validation_data=(X_test,y_test)

完整实例如下:

# fit a wider mlp model on the ionosphere and review learning curves
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
history = model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0, validation_data=(X_test,y_test))
# predict test set
yhat = model.predict_classes(X_test)
# evaluate predictions
score = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % score)
# plot learning curves
pyplot.title('Learning Curves')
pyplot.xlabel('Epoch')
pyplot.ylabel('Cross Entropy')
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='val')
pyplot.legend()
pyplot.show()

运行示例首先使模型适合训练数据集,然后报告测试数据集的准确性。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,该模型可获得更好的准确性分数,其值约为94%。 我们暂时将忽略模型性能。

Accuracy: 0.940

创建了学习曲线的线图,表明该模型达到了合理的拟合并且有足够的时间收敛。

现在,我们对数据集中的简单MLP模型的动态学习有了一些了解,我们可以看看评估模型的性能以及调整模型的配置。

评估和调整MLP模型

k倍交叉验证过程可以提供更可靠的MLP性能估计,尽管它可能非常慢。这是因为必须拟合和评估k个模型。 当数据集大小较小(例如电离层数据集)时,这不是问题。我们可以使用StratifiedKFold类并手动枚举每个折叠,对模型进行拟合,评估,然后在过程结束时报告评估得分的平均值。

# prepare cross validation
kfold = KFold(10)
# enumerate splits
scores = list()
for train_ix, test_ix in kfold.split(X, y):# fit and evaluate the model......
...
# summarize all scores
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

我们可以使用此框架通过一系列不同的数据准备,模型架构和学习配置来开发MLP模型性能的可靠估计。

重要的是,在使用k-fold交叉验证来评估性能之前,我们首先对上一部分中的数据集模型的学习动态有了了解。 如果我们开始直接调整模型,我们可能会获得良好的结果,但是如果没有,我们可能不知道为什么,例如 模型超出或不足。

如果我们再次对模型进行较大更改,则最好返回并确认模型在适当收敛。下面列出了评估上一节中的基本MLP模型的此框架的完整示例。

# k-fold cross-validation of base model for the ionosphere dataset
from numpy import mean
from numpy import std
from pandas import read_csv
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# prepare cross validation
kfold = StratifiedKFold(10)
# enumerate splits
scores = list()
for train_ix, test_ix in kfold.split(X, y):# split dataX_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]# determine the number of input featuresn_features = X.shape[1]# define modelmodel = Sequential()model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dense(1, activation='sigmoid'))# compile the modelmodel.compile(optimizer='adam', loss='binary_crossentropy')# fit the modelmodel.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)# predict test setyhat = model.predict_classes(X_test)# evaluate predictionsscore = accuracy_score(y_test, yhat)print('>%.3f' % score)scores.append(score)
# summarize all scores
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

运行示例将在评估过程的每次迭代中报告模型性能,并在运行结束时报告分类精度的平均值和标准偏差。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到MLP模型的平均准确度约为93.4%。

我们将使用此结果作为基准,以查看是否可以实现更好的性能。

>0.972
>0.886
>0.943
>0.886
>0.914
>0.943
>0.943
>1.000
>0.971
>0.886
Mean Accuracy: 0.934 (0.039)

接下来,让我们尝试添加正则化以减少模型的过拟合。

在这种情况下,我们可以在网络的隐藏层之间添加Dropout层。 例如:

# define model
model = Sequential()
model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dropout(0.4))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))

完整实例如下:

# k-fold cross-validation of the MLP with dropout for the ionosphere dataset
from numpy import mean
from numpy import std
from pandas import read_csv
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# prepare cross validation
kfold = StratifiedKFold(10)
# enumerate splits
scores = list()
for train_ix, test_ix in kfold.split(X, y):# split dataX_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]# determine the number of input featuresn_features = X.shape[1]# define modelmodel = Sequential()model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))model.add(Dropout(0.4))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dropout(0.4))model.add(Dense(1, activation='sigmoid'))# compile the modelmodel.compile(optimizer='adam', loss='binary_crossentropy')# fit the modelmodel.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)# predict test setyhat = model.predict_classes(X_test)# evaluate predictionsscore = accuracy_score(y_test, yhat)print('>%.3f' % score)scores.append(score)
# summarize all scores
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

运行报告运行结束时分类准确度的平均值和标准偏差。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到具有遗漏的MLP模型取得了更好的结果,准确度约为94.6%,而没有遗漏的93.4%

Mean Accuracy: 0.946 (0.043)

最后,我们将尝试将批量大小从32减少到8。

这将导致更多的噪波梯度,也可能减慢模型学习问题的速度。

# fit the model
model.fit(X_train, y_train, epochs=100, batch_size=8, verbose=0)

完整实例如下:

# k-fold cross-validation of the MLP with dropout for the ionosphere dataset
from numpy import mean
from numpy import std
from pandas import read_csv
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# prepare cross validation
kfold = StratifiedKFold(10)
# enumerate splits
scores = list()
for train_ix, test_ix in kfold.split(X, y):# split dataX_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]# determine the number of input featuresn_features = X.shape[1]# define modelmodel = Sequential()model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))model.add(Dropout(0.4))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dropout(0.4))model.add(Dense(1, activation='sigmoid'))# compile the modelmodel.compile(optimizer='adam', loss='binary_crossentropy')# fit the modelmodel.fit(X_train, y_train, epochs=100, batch_size=8, verbose=0)# predict test setyhat = model.predict_classes(X_test)# evaluate predictionsscore = accuracy_score(y_test, yhat)print('>%.3f' % score)scores.append(score)
# summarize all scores
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

运行报告运行结束时分类准确度的平均值和标准偏差。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到带有遗漏的MLP模型取得了更好的结果,准确度约为94.9%。

Mean Accuracy: 0.949 (0.042)

我们将使用此配置作为最终模型。

我们可以继续测试模型架构的替代配置(更多或更少的节点或层),学习超参数(更多或更少的批处理)以及数据转换。

我将其保留为练习; 让我知道你发现了什么。 您可以获得更好的结果吗?将您的结果发表在下面的评论中,我很乐意看到您所得到的。接下来,让我们看看如何拟合最终模型并使用它进行预测。

最终模型和做出预测

选择模型配置后,我们可以在所有可用数据上训练最终模型,并使用它对新数据进行预测。

在这种情况下,我们将使用具有辍学和小批量的模型作为最终模型。

尽管可以在整个数据集而不是数据集的训练子集上进行操作,但我们仍可以像以前一样准备数据并拟合模型。

# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
le = LabelEncoder()
y = le.fit_transform(y)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dropout(0.4))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')

然后,我们可以使用此模型对新数据进行预测。首先,我们可以定义一行新数据。

# define a row of new data
row = [1,0,0.99539,-0.05889,0.85243,0.02306,0.83398,-0.37708,1,0.03760,0.85243,-0.17755,0.59755,-0.44945,0.60536,-0.38223,0.84356,-0.38542,0.58212,-0.32192,0.56971,-0.29674,0.36946,-0.47357,0.56811,-0.51171,0.41078,-0.46168,0.21266,-0.34090,0.42267,-0.54487,0.18641,-0.45300]

注意:我从数据集的第一行中提取了这一行,期望的标签是“ g”。

然后我们可以做出预测。

# make prediction
yhat = model.predict_classes([row])

然后将预测的变换反转,这样我们就可以在正确的标签中使用或解释结果。

# invert transform to get label for class
yhat = le.inverse_transform(yhat)

在这种情况下,我们将仅报告预测。

# report prediction
print('Predicted: %s' % (yhat[0]))

综上所述,下面列出了为电离层数据集拟合最终模型并使用其对新数据进行预测的完整示例。

# fit a final model and make predictions on new data for the ionosphere dataset
from pandas import read_csv
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/ionosphere.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
le = LabelEncoder()
y = le.fit_transform(y)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(50, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dropout(0.4))
model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
model.fit(X, y, epochs=100, batch_size=8, verbose=0)
# define a row of new data
row = [1,0,0.99539,-0.05889,0.85243,0.02306,0.83398,-0.37708,1,0.03760,0.85243,-0.17755,0.59755,-0.44945,0.60536,-0.38223,0.84356,-0.38542,0.58212,-0.32192,0.56971,-0.29674,0.36946,-0.47357,0.56811,-0.51171,0.41078,-0.46168,0.21266,-0.34090,0.42267,-0.54487,0.18641,-0.45300]
# make prediction
yhat = model.predict_classes([row])
# invert transform to get label for class
yhat = le.inverse_transform(yhat)
# report prediction
print('Predicted: %s' % (yhat[0]))

运行示例可以使模型适合整个数据集,并为单行新数据做出预测。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到模型为输入行预测了“ g”标签。

Predicted: g

如何开发神经网络来预测电离层中的干扰相关推荐

  1. 使用神经网络进行预测,图神经网络 社交网络

    什么是神经网络法? . 神经网络的介绍2006-10-2314:58原文摘自:()Introduction----------------------------------------------- ...

  2. [INFOCOM 2019] NeuralWalk:使用神经网络的在线社交网络中的信任评估

    NeuralWalk: Trust Assessment in Online Social Networks with Neural Networks | IEEE Conference Public ...

  3. GNN通俗笔记:图神经网络在推荐/广告中的应用

    原始视频:七月在线公开课<图神经网络在推荐广告场景中的应用>,课件可以打开视频页面下载 分享老师:推荐吴老师,推荐/广告算法专家,曾任部门算法负责人,年薪....不低 字幕校对:天保,全程 ...

  4. bp神经网络时间序列预测,bp神经网络有几个阶段

    什么是BP神经网络? . BP算法的基本思想是:学习过程由信号正向传播与误差的反向回传两个部分组成:正向传播时,输入样本从输入层传入,经各隐层依次逐层处理,传向输出层,若输出层输出与期望不符,则将误差 ...

  5. keras构建卷积神经网络_在python中使用tensorflow s keras api构建卷积神经网络的初学者指南...

    keras构建卷积神经网络 初学者的深度学习 (DEEP LEARNING FOR BEGINNERS) Welcome to Part 2 of the Neural Network series! ...

  6. 图神经网络时间序列预测,时间序列神经网络预测

    有哪些深度神经网络模型 目前经常使用的深度神经网络模型主要有卷积神经网络(CNN).递归神经网络(RNN).深信度网络(DBN).深度自动编码器(AutoEncoder)和生成对抗网络(GAN)等. ...

  7. 神经网络 并行预测_研究人员研究了为什么神经网络可以有效地进行预测

    人工智能,机器学习和神经网络是日常生活中越来越多的术语.面部识别,对象检测以及人的分类和分割是机器学习算法的常见任务,这些算法现在已得到广泛使用.所有这些过程的基础都是机器学习,这意味着计算机可以捕获 ...

  8. pytorch神经网络因素预测_实战:使用PyTorch构建神经网络进行房价预测

    微信公号:ilulaoshi / 个人网站:lulaoshi.info 本文将学习一下如何使用PyTorch创建一个前馈神经网络(或者叫做多层感知机,Multiple-Layer Perceptron ...

  9. 神经网络之预测共享单车使用情况

    神经网络之预测共享单车使用情况 该项目为优达学城Deep Learning Foundation Nanodegree Program的Neural Network阶段项目 原始数据和代码可以在 ht ...

  10. 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明

    以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...

最新文章

  1. CVE-2010-3333
  2. vSphere PowerCLI安装及命令
  3. SDK 和 API 的区别是什么?
  4. React Hooks 完全使用指南
  5. Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers 高维前缀和 + 容斥
  6. jdk8分组统计字段和_JDK 8流和分组
  7. 计算机操作系统(8):进程的控制
  8. 【老杜】MySQL—day02
  9. 查看计算机上隐藏用户,隐藏或显示 InetOrgPerson 对象类 - Windows Server | Microsoft Docs...
  10. [BZOJ] 1610: [Usaco2008 Feb]Line连线游戏
  11. Bailian3470 整理扑克牌【贪心+二分】
  12. 【渝粤题库】陕西师范大学500901 基础物理专题(力、热) 作业(专升本)
  13. oc基础-protocol协议的使用
  14. fh 幅频特性曲线怎么画fl_只要有上、下限截止频率fH、fL、中频电压增益和(),就可绘出基本共射放大电路的幅频特性曲线...
  15. H桥——电机控制电路
  16. 测试人员如何做好需求分析
  17. win10 虚拟机如何连网
  18. OSChina 周日乱弹 ——领导问:功能几天能开发完?怎么回
  19. oracle中both,ORACLE:scope=both|memery|spfile
  20. Angular2 Directive讲解

热门文章

  1. 架设samba服务器
  2. 2016年的第一天上班,顺便开通了博客
  3. 字符串部分函数的实现
  4. CentOS7/RHEL7 systemd详解
  5. linux date 得到指定 datemonth 月的 开始一天 结束一天
  6. app上传遇到的一些问题
  7. HTML与XML数据的结合小总结
  8. Android短信页面
  9. 读python源码--对象模型
  10. 过滤字符串 和 select选择后控件值变更