是否有规则可以找到并设置DNN隐藏层的神经元数?

发布于 2025-02-08 20:00:51 字数 491 浏览 2 评论 0原文

我的情况是:多类分类问题,具有5个功能(我的数据中的列),15个类,单标签。 我的模型是:一个带有5个神经元的输入层,只有一个带有relu的隐藏层,一个带有SoftMax的输出层。 我有两个问题:

  1. 输入层有多少个神经元?确定它是根据功能的数量加上偏差设置的?我尝试调整输入层中神经元的数量,例如77个神经元,性能有所改善,因此我感到困惑。
  2. 我尝试了随机搜索简历以找到隐藏层的数量,神经元的数量和学习率,我在Scikit学习中使用了随机搜索,然后BEST_PARAMS将显示这样的东西:

{'Learning_rate':0.0023716395806862335,'n_layer':1,'n_neurons':291}

因此,问题是,如果它显示出best_params'n_layer':2,但是'n_neurons':2,但是'N_neurons':每一层为291个神经元,模型中有2个隐藏层?

先感谢您!

My situation is that: multiclass classification problem, with 5 features (columns in my data), 15 classes, single label.
My model is : one input layer with 5 neurons, just one hidden layer with ReLU, and one output layer with softmax.
I have two questions:

  1. How many neurons for the input layer? Is it certain that it is set according to the number of features plus bias? I tried tweaking the number of neurons in the input layer, say 77 neurons, the performance improved so I am confused.
  2. I tried Randomized Search cv to find the number of hidden layer, number of neurons and learning rate, I used Randomizedsearchcv in Scikit learn, then the best_params will display something like this:

{'learning_rate': 0.0023716395806862335, 'n_layer': 1, 'n_neurons': 291}

So, the question is that, let's say,if it showed best_params 'n_layer': 2, but 'n_neurons': 291. so is it interpreted as 291 neurons per each layer, and 2 hidden layers in the model?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2025-02-15 20:00:51

您第一个问题的答案:输入层的形状设置基础数字。在您的问题中,您需要5个功能,然后输入层需要5个功能,在我的示例中,我有784个功能,然后输入层形状应为784。

是的,我们有规则可以找到DNN层中神经元的数量。我强烈建议您使用 keras tuner。 Kerastuner找到了最佳的超参数值,用于使用贝叶斯优化的模型超频带和随机搜索算法。我写作。 fashion_mnist数据集的一个示例,其中包括您在问题中解释的模型。我使用epoch = 2,您可以将此搜索与较大的时期一起使用。对于此问题,kerastuner发现第一层的最佳NUM神经元= 416 (< - 您想找到此)和最佳Learning_rate-0.0001

# !pip install -q -U keras-tuner
import tensorflow as tf
import keras_tuner as kt

(img_train, label_train), (img_test, label_test) = tf.keras.datasets.fashion_mnist.load_data()

# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_train = img_train.reshape(60000, -1)
img_test = img_test.astype('float32') / 255.0
img_test = img_test.reshape(10000, -1)
label_train = tf.keras.utils.to_categorical(label_train, 10)
label_test = tf.keras.utils.to_categorical(label_test, 10)

def model_builder(hp):
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Input(784,))

  # Tune the number of units in the first Dense layer
  # Choose an optimal value between 32-512
  hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
  model.add(tf.keras.layers.Dense(units=hp_units, activation='relu'))
  model.add(tf.keras.layers.Dense(10, activation='softmax'))

  # Tune the learning rate for the optimizer
  # Choose an optimal value from 0.01, 0.001, or 0.0001
  hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])

  model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
                loss = 'categorical_crossentropy', metrics = ['accuracy'])
  return model


tuner = kt.Hyperband(model_builder,objective='val_accuracy',max_epochs=3,
                     factor=3,directory='my_dir',project_name='intro_to_kt')

stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
tuner.search(img_train, label_train, epochs=2, validation_split=0.2, callbacks=[stop_early])
# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"BEST num neurons for Dense Layer : {best_hps.get('units')}")
print(f"BEST learning_rate : {best_hps.get('learning_rate')}")

输出:

Trial 11 Complete [00h 00m 14s]
val_accuracy: 0.8530833125114441

Best val_accuracy So Far: 0.8823333382606506
Total elapsed time: 00h 01m 03s
INFO:tensorflow:Oracle triggered exit
BEST num neurons for Dense Layer : 416 #  <- You want this
BEST learning_rate : 0.001

The answer to your first question: input layer's shape set base num of features. In your problem you need 5 features then the input layer needs to be 5 and in my example, I have 784 features then the input layer shape should be 784.

Yes, We have the rule to find the number of neurons in the layer for DNN. I highly recommend you to use Keras Tuner. KerasTuner finds the best hyperparameter values for your models with Bayesian Optimization, Hyperband, and Random Search algorithms. I write an example with the fashion_mnist dataset with the model that you explain in your question. I use epoch=2, you can use this search with larger epochs for your problem. For this problem, KerasTuner finds that the best num neuron for the first layer = 416 (<- you want to find this) and best learning_rate-0.0001.

# !pip install -q -U keras-tuner
import tensorflow as tf
import keras_tuner as kt

(img_train, label_train), (img_test, label_test) = tf.keras.datasets.fashion_mnist.load_data()

# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_train = img_train.reshape(60000, -1)
img_test = img_test.astype('float32') / 255.0
img_test = img_test.reshape(10000, -1)
label_train = tf.keras.utils.to_categorical(label_train, 10)
label_test = tf.keras.utils.to_categorical(label_test, 10)

def model_builder(hp):
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Input(784,))

  # Tune the number of units in the first Dense layer
  # Choose an optimal value between 32-512
  hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
  model.add(tf.keras.layers.Dense(units=hp_units, activation='relu'))
  model.add(tf.keras.layers.Dense(10, activation='softmax'))

  # Tune the learning rate for the optimizer
  # Choose an optimal value from 0.01, 0.001, or 0.0001
  hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])

  model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
                loss = 'categorical_crossentropy', metrics = ['accuracy'])
  return model


tuner = kt.Hyperband(model_builder,objective='val_accuracy',max_epochs=3,
                     factor=3,directory='my_dir',project_name='intro_to_kt')

stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
tuner.search(img_train, label_train, epochs=2, validation_split=0.2, callbacks=[stop_early])
# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"BEST num neurons for Dense Layer : {best_hps.get('units')}")
print(f"BEST learning_rate : {best_hps.get('learning_rate')}")

Output:

Trial 11 Complete [00h 00m 14s]
val_accuracy: 0.8530833125114441

Best val_accuracy So Far: 0.8823333382606506
Total elapsed time: 00h 01m 03s
INFO:tensorflow:Oracle triggered exit
BEST num neurons for Dense Layer : 416 #  <- You want this
BEST learning_rate : 0.001
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文