code:
import random
import tensorflow as tf
import keras.config
@tf.keras.utils.register_keras_serializable()
def argmax_output(x):
return tf.expand_dims(tf.argmax(x, axis=1), axis=-1)
def create_neural_network(input_dim, min_1, max_1, best_agent_filename="agent.h5", noise_scale=0.1, reduce_retracing=True):
try:
# Define the neural network architecture
input_layer = tf.keras.layers.Input(shape=(input_dim,))
dense_1 = tf.keras.layers.Dense(64, activation='relu')(input_layer)
dense_2 = tf.keras.layers.Dense(64, activation='relu')(dense_1)
dense_3 = tf.keras.layers.Dense(3, activation='softmax')(dense_2) # Three continuous values as output
# Define custom output layer for discrete choice
output_1 = tf.keras.layers.Lambda(lambda x: tf.expand_dims(tf.argmax(x, axis=1), axis=-1), name="argmax_layer")(dense_3)
# Define output layer with min/max constraints
output_2 = tf.keras.layers.Dense(1, activation='linear',kernel_constraint=tf.keras.constraints.MinMaxNorm(min_value=min_1,max_value=max_1, rate=1,axis=0))(dense_2)
model = tf.keras.models.Model(inputs=input_layer, outputs=[output_1, output_2]) # Multiple outputs
# Load a pre-trained model (optional)
if best_agent_filename:
try:
best_model = tf.keras.models.load_model(best_agent_filename, custom_objects={"argmax_output": argmax_output})
for layer, best_layer in zip(model.layers, best_model.layers):
weights = best_layer.get_weights()
if weights:
# Add noise to weights for exploration
weights = [w + noise_scale * tf.random.normal(w.shape) for w in weights]
layer.set_weights(weights)
except Exception as e:
model = None
except Exception as e:
model = None # Set model to None on creation failure
return model
/ eror pic