asieradzk / rl_matrix Goto Github PK
View Code? Open in Web Editor NEWDeep Reinforcement Learning in C#
License: Other
Deep Reinforcement Learning in C#
License: Other
This will help the project discussion
I was having difficulty using reinforcement learning for more complex problems, so I made a simple project to test the learning potential. In the code below, I am trying to teach the Boolean identity function to the agent, but the agent gives random answers before and after training.
using OneOf;
using RLMatrix; //NuGet v0.2.0
namespace ReinforcementLearning.Trivial;
internal class Program
{
static void Main(string[] args)
{
var options = new PPOAgentOptions(lr: .005f);
List<IEnvironment<float[]>> environments = [new TrivialEnvironment(), new TrivialEnvironment(), new TrivialEnvironment()];
var agent = new PPOAgent<float[]>(options, environments, new PPONetProviderBase<float[]>(16, 2));
Console.WriteLine($"Pre-training: {Evaluate(agent)}");
for (int i = 0; i < 10000; i++)
{
Console.Write(i);
agent.Step();
Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b");
}
Console.WriteLine($"Post-training: {Evaluate(agent)}");
}
private static float Evaluate(PPOAgent<float[]> agent, int count = 1000)
{
float[] state = new float[1];
int correct = 0;
for (int i = 0; i < count; i++)
{
state[0] = TrivialEnvironment.RandomValue();
(int[] greedyDiscreteActions, float[] meanContinuousActions) = agent.SelectAction(state, false);
if (state[0] == greedyDiscreteActions[0])
{
correct++;
}
}
return (float)correct / count;
}
private sealed class TrivialEnvironment : IEnvironment<float[]>
{
public const float CorrectAnswerReward = 1;
public const float WrongAnswerPenalty = -1;
private readonly float[] state = [RandomValue()];
public static int RandomValue()
{
return Random.Shared.Next(2);
}
public int stepCounter { get; set; }
public int maxSteps { get; set; } = int.MinValue;
public bool isDone { get; set; }
public OneOf<int, (int, int)> stateSize { get; set; } = 1;
public int[] actionSize { get; set; } = [2];
public float[] GetCurrentState() => state;
public TrivialEnvironment() => Initialise();
public void Initialise() => Reset();
public void Reset()
{
stepCounter = 0;
isDone = false;
}
public float Step(int[] actionsIds)
{
float input = state[0];
float output = actionsIds[0];
state[0] = RandomValue();
return input == output ? CorrectAnswerReward : WrongAnswerPenalty;
}
}
}
Am I using the library incorrectly?
I have written reasons why this is important for your degree; dotnet/TorchSharp#710 (reply in thread)
Hello, I'm currently in the process of porting a (failed) TorchSharp agent over to RL Matrix. Honestly thanks so much for this library.
I'm just wondering if you're planning on using the TorchSharp-cuda-windows or TorchSharp-cuda-linux packages eventually? I was using cuda in my previous environment and was hoping it was in the plans
EDIT: I stepped through the debugger and saw that the tensors were already using cuda. I just assumed it didn't since I didn't see it in the README.
I guess while I'm here writing this, I was also wondering if there was an example for a IContinuousEnvironment<>
? This is the environment I need since I don't need discrete actions, but the examples were all regular IEnvironment<>
from what I saw. Maybe I missed it as well lol
I'm asking since I'm running into this error:
My env is extremely basic, I am trying to set up 3 inputs and 3 (continuous) outputs with 0 discrete outputs.
private sealed class Env : IContinuousEnvironment<float[]>
{
public int stepCounter { get; set; }
public int maxSteps { get; set; }
public bool isDone { get; set; }
public OneOf<int, (int, int)> stateSize { get; set; }
public int[] actionSize { get; set; }
public (float min, float max)[] continuousActionBounds { get; set; }
private readonly float[] _inputs;
public Env()
{
_inputs = new float[3];
Initialise();
}
public void Initialise()
{
// new physics
stepCounter = 0;
maxSteps = 100_000;
stateSize = 3;
actionSize = [3];
continuousActionBounds = [(-1f, 1f), (-1f, 1f), (-1f, 1f)];
// physics reset
isDone = false;
}
public float[] GetCurrentState()
{
_inputs[0] = 1f;
_inputs[1] = 0.5f;
_inputs[2] = -1f;
return _inputs;
}
public void Reset()
{
// physics reset
isDone = false;
stepCounter = 0;
}
public float Step(int[] actionsIds)
{
throw new NotImplementedException();
}
public float Step(int[] discreteActions, float[] continuousActions)
{
return continuousActions[0];
}
}
I also noticed stepCounter
never increases from 0
It is now possible to do Test Driven Development using VS2022 to create Gym 2D and 3D RL environments for testing
using the latest gdUnit4API TestAdaptor on VS2022
Thank you for the RLMatrix.
I have a complex environment that cannot be simulated, in which interdependent observations are generated one at a time, so running training on an entire episode at once is not an option. I had to add a TrainObservation() based on your TrainEpisode() code. Please add this minor fix.
Thanks
Hey there, I'm curious about how to save and load models trained using Godot. I've been experimenting with reinforcement learning algorithms, and now I'm at a point where I want to persist my trained models for future use. Can anyone guide me through the process? Any tips or examples would be greatly appreciated! Thanks in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.