Code Monkey home page Code Monkey logo

Comments (1)

QuanyiLi avatar QuanyiLi commented on July 20, 2024

You can apply the method to any well-trained MLP policies after training. Usually, the weights of this MLP policy can be stored locally in TF or Torch format. We then convert them into numpy format like what play/torch_to_numpy.py does, so we can reconstruct the policy with numpy. That is what ppo_inference_tf does. In the numpy policy execution process, the neuron activations are recorded, enabling further analysis. Thus, if you want to use ppo_inference_tf , you have to ensure your policy is a MLP policy trained with TF and convert it to numpy format. An example code is as follows given a policy trained with rllib:

remove_value_network = True
path = "expert_weights.npz"

with open(ckpt_path, "rb") as f:
    data = f.read()
unpickled = pickle.loads(data)
worker = pickle.loads(unpickled.pop("worker"))
if "_optimizer_variables" in worker["state"]["default_policy"]:
    worker["state"]["default_policy"].pop("_optimizer_variables")
pickled_worker = pickle.dumps(worker)
weights = worker["state"]["default_policy"]
if remove_value_network:
    weights = {k: v for k, v in weights.items() if "value" not in k}
np.savez_compressed(path, **weights)
print("Numpy agent weight is saved at: {}!".format(path))

Actually, you can record the neuron activation information in your torch or TF policy directly by logging information in the forward function supposing it is a torch policy. In this way, the numpy conversion process can be removed. We additionally convert them into numpy format for aligning the inference process for both TF and Torch policies.

from policydissect.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.