Tensorflow2 Keras调节单元和激活功能



我正在尝试设置一个Keras调谐器,以同时调整层数和激活函数。网络试图将一个2D函数扭曲为另一个2D功能。我一直收到错误:

ValueError: Unknown activation function: sigmoidtanh

我的代码如下:

python

def euclidean_distance_loss(y_true, y_pred):
return tf.reduce_sum(tf.math.squared_difference(y_pred, y_true), axis=1)
def model_builder(hp):
model = tf.keras.Sequential()
tf.keras.layers.InputLayer(input_shape=(2,)),
#===Tune number of layers===#
for i in range(hp.Int('num_layers', 2, 5)):
#===Tune number of units and activation in each layer===#
hp_units = hp.Int('units_' + str(i), min_value=12, max_value=512, step=10)
activation = hp.Choice('activation_' + str(i), ['linear', 'relu', 'selu', 'sigmoid', 'tanh'])
model.add(tf.keras.layers.Dense(units=hp_units, activation=activation))
#===Add Final Layer===#
model.add(tf.keras.layers.Dense(units=2, activation='linear'))
#===Compile===#
model.compile(optimizer='adam', loss=euclidean_distance_loss, metrics=['MeanSquaredError'])
return model
b_tuner = BayesianOptimization( model_builder,
objective = 'val_mean_squared_error',
max_trials = 10,
executions_per_trial = 15,
directory = 'output',
project_name = 'b_tune_nn' )

b_tuner.search(x_train_points, y_train_points, epochs=25, validation_split=0.2)

现在如果我写:

model.add(tf.keras.layers.Dense(units=hp_units, activation='tanh'))

一切都很好。我在指定我的模型时做错了什么?

如果你需要更多的上下文,请告诉我。

适用于我的完整代码:

import kerastuner as kt
import tensorflow as tf
from kerastuner import BayesianOptimization
import numpy as np
x_train = np.random.randn(1000,20)
y_train = np.random.randn(1000,2)
def euclidean_distance_loss(y_true, y_pred):
return tf.reduce_sum(tf.math.squared_difference(y_pred, y_true), axis=1)
def model_builder(hp):
model = tf.keras.Sequential()
tf.keras.layers.InputLayer(input_shape=(2,)),
#===Tune number of layers===#
for i in range(hp.Int('num_layers', 2, 5)):
#===Tune number of units and activation in each layer===#
hp_units = hp.Int('units_' + str(i), min_value=12, max_value=512, step=10)
activation = hp.Choice('activation_' + str(i), ['linear', 'relu', 'selu', 'sigmoid', 'tanh'])
model.add(tf.keras.layers.Dense(units=hp_units, activation=activation))
#===Add Final Layer===#
model.add(tf.keras.layers.Dense(units=2, activation='linear'))
#===Compile===#
model.compile(optimizer='adam', loss=euclidean_distance_loss, metrics=['MeanSquaredError'])
return model

b_tuner = BayesianOptimization( model_builder,
objective = 'val_mean_squared_error',
max_trials = 10,
executions_per_trial = 15,
directory = 'output',
project_name = 'b_tune_nn' )

b_tuner.search(x_train, y_train, epochs=25, validation_split=0.2)

编辑:

pip uninstall keras-tuner
pip install -U keras-tuner # get the latest version

相关内容

  • 没有找到相关文章

最新更新