~melmon/chizuru-old

fd3fda06f01ff06b60aa8350778da4d6705a2c60 — Melmon 10 months ago b4c47f4
holy shit it's functional
3 files changed, 98 insertions(+), 52 deletions(-)

M chizuru.py
A test.py
M train.py
M chizuru.py => chizuru.py +27 -48
@@ 20,31 20,15 @@
import os
import tensorflow as tf

# Constants
ASCII_CHARNUM = 128
ENVIRONMENT = "rogueinabox"
LOG_INTERVAL = 200

CKPT_PATH = "training/czr-{epoch:04d}.ckpt"
CKPT_DIR = os.path.dirname(CKPT_PATH)

# Hyperparameters
NUM_ITERATIONS = 20000
BATCH_SIZE = 64
ALPHA = 1.0e-3
BETA1 = 0.9
BETA2 = 0.999
EPSILON = 1.0e-8
DECAY = 0.0

CKPT_CALLBACK = tf.keras.callbacks.ModelCheckpoint(
    filepath=CKPT_PATH,
    save_weights_only=True,
    verbose=1,
    save_freq=5*BATCH_SIZE
)


def create_model():
def create_model() -> tf.keras.Model:
    """Instantiates, compiles and returns the Chizuru model."""
    status_input = tf.keras.Input(shape=(64,))
    inv_input = tf.keras.Input(shape=(64,))


@@ 52,45 36,39 @@ def create_model():
    map_input = tf.keras.Input(shape=(21, 79), dtype=tf.int32)
    crop_input = tf.keras.Input(shape=(9, 9), dtype=tf.int32)

    status_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 32)(status_input)
    status_net = tf.keras.layers.Dense(32, activation="relu")(status_net)
    status_net = tf.keras.layers.Dense(32, activation="relu")(status_net)
    #status_net = tf.keras.layers.Flatten()(status_net)
    status_net = tf.keras.layers.Dense(64, activation="relu")(status_input)

    inv_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 32)(inv_input)  # replace this with attention layer maybe?
    inv_net = tf.keras.layers.Dense(32, activation="relu")(inv_net)
    #inv_net = tf.keras.layers.Flatten()(inv_net)
    inv_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 64)(inv_input)  # replace this with attention maybe?
    inv_net = tf.keras.layers.Flatten()(inv_net)
    inv_net = tf.keras.layers.Dense(64, activation="relu")(inv_net)

    equip_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 16)(equip_input)
    equip_net = tf.keras.layers.Dense(16, activation="relu")(equip_net)
    #equip_net = tf.keras.layers.Flatten()(equip_net)
    equip_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 32)(equip_input)
    equip_net = tf.keras.layers.Flatten()(equip_net)
    equip_net = tf.keras.layers.Dense(32, activation="relu")(equip_net)

    map_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 32, input_length=21 * 79)(map_input)
    map_net = tf.keras.layers.Conv2D(32, (3, 3), activation="relu", input_shape=(21, 79))(map_net)
    map_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 64, input_length=21 * 79)(map_input)
    map_net = tf.keras.layers.Conv2D(64, (3, 3), activation="relu", input_shape=(21, 79))(map_net)
    map_net = tf.keras.layers.MaxPooling2D((2, 2))(map_net)
    map_net = tf.keras.layers.Conv2D(64, (3, 3), activation="relu")(map_net)
    map_net = tf.keras.layers.Flatten()(map_net)

    crop_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 32, input_length=9 * 9)(crop_input)
    crop_net = tf.keras.layers.Conv2D(64, (3, 3), activation="relu", input_shape=(9, 9))(crop_net)
    crop_net = tf.keras.layers.Embedding(ASCII_CHARNUM, 64, input_length=9 * 9)(crop_input)
    crop_net = tf.keras.layers.Conv2D(48, (3, 3), activation="relu", input_shape=(9, 9))(crop_net)
    crop_net = tf.keras.layers.Flatten()(crop_net)

    collected = tf.keras.layers.Concatenate()([status_net, inv_net, equip_net])
    print(collected.shape)
    collected = tf.keras.layers.Concatenate()([status_net, inv_net, equip_net, map_net, crop_net])

    # MLP after concat
    pre_mlp = tf.keras.layers.Dense(256, activation="relu")(collected)
    print(pre_mlp.shape)
    # DNN after concat
    pre_dnn = tf.keras.layers.Dense(128, activation="relu")(collected)

    # LSTM
    lstm = tf.keras.layers.LSTM(256)(pre_mlp)
    print(lstm.shape)
    pre_dnn = tf.keras.layers.Reshape((1, -1))(pre_dnn)
    lstm = tf.keras.layers.LSTM(128)(pre_dnn)

    # final MLP
    final_mlp = tf.keras.layers.Dense(128)(lstm)
    # final DNN
    final_dnn = tf.keras.layers.Dense(128)(lstm)

    output = tf.keras.layers.Dense(21)(final_mlp)
    print(output.shape)
    output = tf.keras.layers.Dense(21)(final_dnn)
    # COMMANDS
    # 0  : N MOVE (k)
    # 1  : E MOVE (l)


@@ 132,26 110,27 @@ def create_model():
    return final_model


def get_crop(map: list[list[int]]):  # TODO
def get_crop(_map: list[list[int]]):  # TODO
    """Returns a 9x9 crop of the given Rogue map surrounding the player."""
    pass


def save_checkpoint(model_sv: tf.keras.Model, epoch):
def save_checkpoint(model_sv: tf.keras.Model, epoch) -> None:
    """Saves the model checkpoint with given epoch."""
    model_sv.save_weights(CKPT_PATH.format(epoch=epoch))
    print("Epoch " + str(epoch) + " saved to " + CKPT_PATH.format(epoch=epoch) + "~")


def load_checkpoint(model_ld: tf.keras.Model, epoch):
    """Loads a model checkpoint at a given epoch."""
def load_checkpoint(model_ld: tf.keras.Model, epoch) -> tf.keras.Model:
    """Loads a model checkpoint at a given epoch. Returns the loaded model."""
    model_ld.load_weights(CKPT_PATH)
    print("File " + CKPT_PATH.format(epoch=epoch) + " loaded to current model~")
    return model_ld


if __name__ == "__main__":
    model = create_model()
    tf.keras.utils.plot_model(model, "stuff.png", show_shapes=True)
    save_checkpoint(model, 0)
    # save_checkpoint(model, 0)

# †昇天†

A test.py => test.py +54 -0
@@ 0,0 1,54 @@
import tensorflow as tf

def concat_cnn_dnn(cnn_input, dnn_input_dim):
    """
    Function to define a neural network that concatenates a convolutional neural network (CNN) and a dense neural
    network (DNN) with an embedding layer followed by an LSTM layer using TensorFlow.

    Args:
        cnn_input (tf.Tensor): Input tensor for the CNN.
        dnn_input_dim (int): Dimensionality of the input tensor for the embedding layer in the DNN.
        dnn_input_len (int): Length of the input tensor for the embedding layer in the DNN.

    Returns:
        tf.Tensor: Output tensor of the concatenated CNN and DNN with LSTM layer.
    """
    # Define CNN
    cnn = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(cnn_input)
    cnn = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(cnn)
    cnn = tf.keras.layers.Flatten()(cnn)

    # Define DNN with an Embedding layer
    dnn = tf.keras.layers.Embedding(input_dim=dnn_input_dim, output_dim=128)(dnn_input)
    dnn = tf.keras.layers.Flatten()(dnn)

    # Concatenate CNN and DNN
    concatenated = tf.keras.layers.concatenate([cnn, dnn])
    concatenated = tf.keras.layers.Reshape((1, -1))(concatenated)

    # Add LSTM layer
    lstm = tf.keras.layers.LSTM(units=256, activation='relu')(concatenated)

    return lstm

# Define input tensors for CNN and DNN
cnn_input = tf.keras.Input(shape=(28, 28, 1))  # Example input shape for a CNN

dnn_input_len = 512  # Example length of input for the Embedding layer
dnn_input = tf.keras.Input(shape=(dnn_input_len,))
dnn_input_dim = 1000  # Example dimensionality of input for the Embedding layer

# Call the function to concatenate CNN and DNN with LSTM layer
concatenated_output = concat_cnn_dnn(cnn_input, dnn_input_dim)

# Define the rest of your model architecture on top of the concatenated output
# For example, you can add more layers, output layers, etc.

# Create the model
model = tf.keras.Model(inputs=[cnn_input, dnn_input], outputs=concatenated_output)

# Compile the model and set loss, optimizer, and metrics
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Print model summary
model.summary()

M train.py => train.py +17 -4
@@ 1,17 1,27 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# This code is governed under the GNU General Public Licence v3.0.

"""Runs and trains the Chizuru agent again and again until the program is halted or until
epoch count reaches a provided number."""

from chizuru import Chizuru
from chizuru import create_model, save_checkpoint, load_checkpoint, get_crop
import tensorflow as tf
import datetime
import os

# Globals
# Constants
LOG_DIR = "logs/czr" + datetime.datetime.now().strftime("%Y-%m-%d--%H:%M:%S")
TENSORBOARD_CALLBACK = tf.keras.callbacks.TensorBoard(log_dir=LOG_DIR, histogram_freq=1)
CKPT_PATH = "training/czr-{epoch:04d}.ckpt"
CKPT_DIR = os.path.dirname(CKPT_PATH)
BATCH_SIZE = 64
CKPT_CALLBACK = tf.keras.callbacks.ModelCheckpoint(
    filepath=CKPT_PATH,
    save_weights_only=True,
    verbose=1,
    save_freq=5*BATCH_SIZE
)

# Hyperparameters
NUM_ITERATIONS = 20000


@@ 22,7 32,10 @@ EPSILON_START = 1.0
EPSILON_END = 0.01
EPSILON_DECAY = 150000
TARGET_UPDATE_FREQUENCY = 10000
# End of hyperparameters
ALPHA = 1.0e-3
BETA1 = 0.9
BETA2 = 0.999


if __name__ == "__main__":
    pass