【问题标题】:How to create a neural network for regression?如何创建用于回归的神经网络?
【发布时间】:2018-08-07 01:33:05
【问题描述】:

我正在尝试使用 Keras 制作神经网络。我使用的数据是https://archive.ics.uci.edu/ml/datasets/Yacht+Hydrodynamics。我的代码如下:

import numpy as np
from keras.layers import Dense, Activation
from keras.models import Sequential
from sklearn.model_selection import train_test_split

data = np.genfromtxt(r"""file location""", delimiter=',')

model = Sequential()
model.add(Dense(32, activation = 'relu', input_dim = 6))
model.add(Dense(1,))
model.compile(optimizer='adam', loss='mean_squared_error', metrics = ['accuracy'])

Y = data[:,-1]
X = data[:, :-1]

从这里我尝试使用 model.fit(X, Y),但模型的准确性似乎保持在 0。我是 Keras 的新手,所以这可能是一个简单的解决方案,提前道歉。

我的问题是向模型添加回归以提高准确性的最佳方法是什么?提前致谢。

【问题讨论】:

    标签: python numpy machine-learning neural-network keras


    【解决方案1】:

    首先,您必须使用来自sklearn.model_selection 库的train_test_split 类将您的数据集 拆分为training 集和test 集。

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)
    

    此外,您必须使用 StandardScaler 类来 scale 您的值。

    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)
    

    然后,您应该添加更多以获得更好的结果。

    注意

    通常最好应用以下公式来找出所需的隐藏层 的总数。

    Nh = Ns/(α∗ (Ni + No))
    

    在哪里

    • Ni = 输入神经元的数量。
    • 否 = 输出神经元的数量。
    • Ns = 训练数据集中的样本数。
    • α = 任意比例因子,通常为 2-10。

    所以我们的分类器变成:

    # Initialising the ANN
    model = Sequential()
    
    # Adding the input layer and the first hidden layer
    model.add(Dense(32, activation = 'relu', input_dim = 6))
    
    # Adding the second hidden layer
    model.add(Dense(units = 32, activation = 'relu'))
    
    # Adding the third hidden layer
    model.add(Dense(units = 32, activation = 'relu'))
    
    # Adding the output layer
    model.add(Dense(units = 1))
    

    您使用的metric-metrics=['accuracy'] 对应于分类问题。如果您想做回归,请删除metrics=['accuracy']。也就是说,只需使用

    model.compile(optimizer = 'adam',loss = 'mean_squared_error')
    

    Hereregressionclassificationkeras 指标列表

    此外,您必须为 fit 方法定义 batch_sizeepochs 值。

    model.fit(X_train, y_train, batch_size = 10, epochs = 100)
    

    训练完network 后,您可以使用model.predict 方法predict 得到X_test 的结果。

    y_pred = model.predict(X_test)
    

    现在,您可以比较我们从神经网络预测中获得的y_predy_test 这是真实数据。为此,您可以使用matplotlib 库创建plot

    plt.plot(y_test, color = 'red', label = 'Real data')
    plt.plot(y_pred, color = 'blue', label = 'Predicted data')
    plt.title('Prediction')
    plt.legend()
    plt.show()
    

    看来我们的神经网络学得很好

    这是plot 的外观。

    这是完整的代码

    import numpy as np
    from keras.layers import Dense, Activation
    from keras.models import Sequential
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt
    # Importing the dataset
    dataset = np.genfromtxt("data.txt", delimiter='')
    X = dataset[:, :-1]
    y = dataset[:, -1]
    
    # Splitting the dataset into the Training set and Test set
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)
    
    # Feature Scaling
    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)
    
    # Initialising the ANN
    model = Sequential()
    
    # Adding the input layer and the first hidden layer
    model.add(Dense(32, activation = 'relu', input_dim = 6))
    
    # Adding the second hidden layer
    model.add(Dense(units = 32, activation = 'relu'))
    
    # Adding the third hidden layer
    model.add(Dense(units = 32, activation = 'relu'))
    
    # Adding the output layer
    
    model.add(Dense(units = 1))
    
    #model.add(Dense(1))
    # Compiling the ANN
    model.compile(optimizer = 'adam', loss = 'mean_squared_error')
    
    # Fitting the ANN to the Training set
    model.fit(X_train, y_train, batch_size = 10, epochs = 100)
    
    y_pred = model.predict(X_test)
    
    plt.plot(y_test, color = 'red', label = 'Real data')
    plt.plot(y_pred, color = 'blue', label = 'Predicted data')
    plt.title('Prediction')
    plt.legend()
    plt.show()
    

    【讨论】:

    • 漂亮的答案!
    • Brilliant @MihaiAlexandru-Ionut,你能解释一下缩放的必要性吗?
    • @ES1927,很多机器学习算法都使用欧拉距离。因此需要标准化或缩放,以便所有输入都处于可比较的范围内。
    • 我在识别预测图与实际图中的 x 轴代表什么时遇到了一些麻烦。首先我认为这是时代,但在我的数据中,我在时代中改变了什么并不重要,我得到相同的 x 轴范围。有人可以给我解释一下吗?
    • 回答我自己上面的评论,是样本号=)
    猜你喜欢
    • 2018-05-08
    • 2019-01-27
    • 2015-05-11
    • 2015-01-31
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2014-01-15
    • 2018-12-28
    相关资源
    最近更新 更多