dqn | Basic DQN implementation | Machine Learning library
kandi X-RAY | dqn Summary
kandi X-RAY | dqn Summary
This is a very basic DQN (with experience replay) implementation, which uses OpenAI's gym environment and Keras/Theano neural networks.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Reset the environment
- Observation time series
- Wrap a qn Environment
- Create q - network
- Sample from replay memory
- Epsilon greedy algorithm
- Forward an action
- Get the name of a job
dqn Key Features
dqn Examples and Code Snippets
def main():
env = gym.make('CartPole-v0')
gamma = 0.99
copy_period = 50
D = len(env.observation_space.sample())
K = env.action_space.n
sizes = [200,200]
model = DQN(D, K, sizes, gamma)
tmodel = DQN(D, K, sizes, gamma)
init = tf.glo
Community Discussions
Trending Discussions on dqn
QUESTION
I am trying to learn a custom environment using the TFAgents package. I am following the Hands-on-ML book (Code in colab see cell 129). My aim is to use DQN agent on a custom-written grid world environment.
Grid-World environment:
...ANSWER
Answered 2021-Jun-02 at 22:36You cannot use TensorSpec
with PyEnvironment
class objects, this is why your attempted solution does not work. A simple fix should be to use the original code
QUESTION
I am implementing simple DQN algorithm using pytorch
, to solve the CartPole environment from gym
. I have been debugging for a while now, and I cant figure out why the model is not learning.
Observations:
- using
SmoothL1Loss
performs worse thanMSEloss
, but loss increases for both - smaller
LR
inAdam
does not work, I have tested using 0.0001, 0.00025, 0.0005 and default
Notes:
- I have debugged various parts of the algorithm individually, and can say with good confidence that the issue is in the
learn
function. I am wondering if this bug is due to me misunderstandingdetach
in pytorch or some other framework mistake im making. - I am trying to stick as close to the original paper as possible (linked above)
References:
...ANSWER
Answered 2021-Jun-02 at 17:39The main problem I think is the discount factor, gamma. You are setting it to 1.0, which mean that you are giving the same weight to the future rewards as the current one. Usually in reinforcement learning we care more about the immediate reward than the future, so gamma should always be less than 1.
Just to give it a try I set gamma = 0.99
and run your code:
QUESTION
I'm developing an Autonomous Agent based on DQN. I am using the gym library to make the environments that I want to test, but I'm stuck in processing the frames of the state. The state that the gym environment returns, using the FrameStack wrapper, has the following observation space:
...ANSWER
Answered 2021-May-24 at 22:09Fixed! I was just doing it in the wrong way.
QUESTION
hello I'm new with PyTorch
and i would like to use Mean squared logarithmic error as a loss function in my neural network for training my DQN agent but i can't find the MSLE in the nn.functional
in PyTorch
what the best way to implement it ?
ANSWER
Answered 2021-May-22 at 09:44well is not hard to do it the MSLE equation as the photo below shows
now, as some user on the PyTorch form suggested
you can be added as a class like this
QUESTION
I am starting to learn about DQN
, and I am trying to solve the FrozenLake-v0
problem from scratch by my self and using Pytorch
so I will put the whole code since it's connected.
ANSWER
Answered 2021-Apr-23 at 20:14since you are calling a tensor that contains a matrix, you need to specify which indices your calling in your case just adding [0]
to the forward statement will solve the problem and in the [actions]
, replace it with [actions.item()]
QUESTION
I am trying to make a custom Gym Environment so that I can use it in a Keras Network. But there is a problem that is happening to me when I try to fit de neural network.
...ANSWER
Answered 2021-Apr-08 at 12:00The input shape should be (1,15) in your case. This is because the actual input shape is increased by an additional dimension to what you specify in shape
as keras processes inputs in batches, with the batch size being the first parameter.
The error message is telling you that inputs of shape (1,1,15) are being passed to the model ( i.e. batch of 1, and input shape (1,15), so ndims=3), but you only have ndims=2. Despite only passing (15,) as the input shape, notice that ndims = 2. This is because Keras is adding an additional dimension for the batch.
So to fix, set shape=(1,15)
which will then be processed as (1,1,15)
which is what Keras expects.
QUESTION
I have an array that has first rows as header:
...ANSWER
Answered 2021-Apr-07 at 08:45Destructure the array, and take the keys (1st item), and the values (the rest). Map the values array, and then map each sub-array of values, take the respective key by the value, and return a pair of [key, value]
. Convert the array pairs to an object with Object.fromEntries()
:
QUESTION
I'm trying to create a DqnAgent
using an own environment. But i'm receiving the following error
AttributeError: 'tuple' object has no attribute 'rank' In call to configurable 'DqnAgent' ()
Below is the code for my environment myEnv
ANSWER
Answered 2021-Feb-27 at 15:49The DqnAgent
expects a TFPyEnvironment
but you're implementing the environment as an PyEnvironment
.
To fix this error you should convert the environment into the TensorFlow implementation before you are creating the agent.
You can do it this way:
QUESTION
I am building a DQN for an Open Gym environment. My observation space is only 1 discrete value but my actions are:
...ANSWER
Answered 2021-Feb-26 at 19:02As you've already noted in your comment, DQN is not compatible with continuous action spaces because of how DQN works; argmax of "a" for Q(s,a)
- It's impossible to check Q(s,a)
for all a
when a
is continuous.
Having said that, when applying this to Policy Gradient methods (which are compatible with continuous action spaces) you will run into the same issue in your question because with policy gradient you need to provide a probability for each action that you take. Something like this could work:
- Actor (a neural net in this case) gives 3 outputs.
- First 2 outputs are probabilities of each discrete value.
- 3rd output is your continuous value.
Take the softmax of the first two outputs which gives you your discrete value and then take the third output which is continuous and this will give you your action. You then need to derive the probability of that action which is given by the combined probability of all of your outputs.
QUESTION
I am wondering how best to feed back the changes my DQN agent makes on its environment, back to itself.
I have a battery model whereby an agent can observe a time-series forecast of 17 steps, and 5 features. It then makes a decision on whether to charge or discharge.
I want to includes its current state of charge (empty, half full, full etc) in its observation space (i.e. somewhere within the (17,5) dataframes I am feeding it).
I have several options, I can either set a whole column to the state of charge value, a whole row, or I can flatten the whole dataframe and set one value to the state of charge value.
Is any of these unwise? It seem a little rudimentary to me to set a whole columns to a single value, but should it actually impact performance? I am wary of flattening the whole thing as I plan to use either conv or lstm layers (although the current model is just dense layers).
...ANSWER
Answered 2021-Feb-03 at 14:59You would not want to add in unnecessary features which are repetitive in the state representation as it might hamper your RL agent convergence later when you would want to scale your model to larger input sizes(if that is in your plan).
Also, the decision of how much of information you would want to give in the state representation is mostly experimental. The best way to start would be to just give in a single value as the battery state. But if the model does not converge, then maybe you could try out the other options you have mentioned in your question.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install dqn
You can use dqn like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page