Tensorflow2 Keras 调整单元和激活函数
Tensorflow2 Keras Tuning both units and activation function
我正在尝试设置 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
我正在尝试设置 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