#๐Ÿ”’ Problem with loading basic ai module

5 messages ยท Page 1 of 1 (latest)

raw monolith
#

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

next thicketBOT
#

@raw monolith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pallid gull
#

Use codeblocks, and post the error itself - not an image.

next thicketBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.