【问题标题】:Testing for the optimal number of neurons - Keras/Tensorflow测试最佳神经元数量 - Keras/Tensorflow
【发布时间】:2018-06-28 21:42:01
【问题描述】:

我试图为一个简单的三层神经网络找到最佳的神经元数量。为此,我保持我的训练/测试拆分的随机状态固定,并在中间层的神经元数量上进行迭代。

我有 6 个参数用于使用三层预测第 7 个参数 - 输入(6 个神经元)、隐藏(i 个神经元)和输出(1 个神经元)。

但是,每次我运行它时,我都会得到完全不同的答案,这些答案并不一致——让我不知道有多少中间层是“最好的”。

我使用的是一个相对较小的数据集 - 100 个样本。网络是否使用随机权重/偏差初始化?还是我还缺少其他东西?对 tensorflow/keras 学习者的任何帮助都会有很大帮助!

results = []
for i in range(1,10):
    x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2, random_state = 45)
    model = Sequential()
    model.add(Dense(6,input_dim = x.shape[1], activation = "relu"))
    model.add(Dense(i,activation = "relu"))
    model.add(Dense(1))
    model.compile(loss = "mean_squared_error", optimizer = "adam")
    monitor = EarlyStopping(monitor = "val_loss", min_delta = 1e-3, patience= 9000, verbose = 0, mode="auto")

    model.fit(x,y,validation_data = (x_test,y_test), callbacks = [monitor], verbose= 0, epochs = 1000)

    pred = model.predict(x_test)
    score = np.sqrt(metrics.mean_squared_error(pred,y_test))
    print ("Score (RMSE): {}".format(score))
    results.append(score)

如果有帮助,这些是我每次运行所获得的结果范围(我认为这有点相似):

hidden_layers = [1,2,3,4,5,6,7,8,9]
Run1 = [1.8300211429595947, 0.7832328081130981,1.144912600517273,1.17598557472229,1.9758267402648926,0.49578756,
        0.6556473970413208,0.696390688419342,0.5946451425552368]
Run2 = [1.422674,1.566674,1.91101,0.86435,1.229273,0.94930,0.7424377,1.2183,0.85622]
Run3 = [1.4056072,1.790036,0.55659616,1.5427451,1.8569565,0.54280525,0.69169235,0.72319275,0.48972014]
Run4 = [0.78299254,1.6193594,0.90550566,1.1891861,0.87066174,0.9133969,1.6031398,0.59118015,0.42699912]
Run5 = [1.842247,1.5956467,1.0008113,0.95922214,2.015607,1.5420123,0.5894643,0.65639037,1.9998837]

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    此事件可能发生在任何神经网络项目中。当您使用相同的输入和相同数量的神经元运行模型时,您将获得不同的性能。

    此事件的主要原因是 Python 中的随机数生成器。

    如果您想为每次模拟运行提供相同的结果,您必须设置随机数生成器的种子。

    例如:

    random.seed(5)
    

    在我的一个项目中,我使用以下命令:

    random.seed(np.abs(noise_ratio*100))
    

    它可以包含您的代码变量。

    所以,如果我想更正您的代码:

    results = []
    for i in range(1,10):
        random.seed(10)
        x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2, random_state = 45)
        model = Sequential()
        model.add(Dense(6,input_dim = x.shape[1], activation = "relu"))
        model.add(Dense(i,activation = "relu"))
        model.add(Dense(1))
        model.compile(loss = "mean_squared_error", optimizer = "adam")
        monitor = EarlyStopping(monitor = "val_loss", min_delta = 1e-3, patience= 9000, verbose = 0, mode="auto")
    
        model.fit(x,y,validation_data = (x_test,y_test), callbacks = [monitor], verbose= 0, epochs = 1000)
    
        pred = model.predict(x_test)
        score = np.sqrt(metrics.mean_squared_error(pred,y_test))
        print ("Score (RMSE): {}".format(score))
        results.append(score)
    

    除了这个解决方案,一些研究人员认为你必须使用 k-fold 或运行你的代码十次,并给出结果的平均值作为最终结果。我建议第二种方式。

    写一个循环10次,并给出结果的平均值作为最终结果。

    【讨论】:

    • 抱歉这么久才回来,我做了很多测试!与我一起将 kernel_initializer 定义为“uniform”时,设置随机种子有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 2021-10-09
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多