Code Monkey home page Code Monkey logo

rl's Introduction

Notebooks and code for Alpha Architect post on reinforcement learning.

  • Tic-Tac-Toe.ipynb - Table-based reinforcement learning to play Tic-Tac-Toe, and analogous if pointless deep learning algo
  • Cart-Pole.ipynb - Building deep reinforcement learning algos from scratch with Keras for OpenAI environments like Cartpole and LunarLander.
  • Ray_tune.ipynb - Similar but with state of the art RL from UC Berkeley Ray project
  • Trading_with_RL.ipynb - Algos to trade fake market data, inspired by Gordon Ritter paper Machine Learning for Trading. This should run in Google Colab.

Typical installation procedure:

  • Install Anaconda python data science distribution

  • Make an environment like

    conda create --name tf tensorflow

    or if you have Nvidia GPU

    conda create --name tf_gpu tensorflow-gpu 

    This should install requirements like working Nvidia drivers

  • Upgrade TensorFlow to latest version with

    pip install --upgrade tensorflow
  • Install additional requirements as necessary - requirements.txt has python modules installed at time of testing.

    pip install -r requirements.txt
  • TensorFlow Docker install may also be a good way to start but has not been tested.

  • Run notebooks using

    jupyter notebook

rl's People

Contributors

dependabot[bot] avatar druce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rl's Issues

DQN_Agent in shm

This is a work of art, it would be wonderful to find examples inspired by you with PPO and other agents.

I uncommented and tried to test the DQN_Agent in https://github.com/druce/rl/blob/master/Trading_with_RL.ipynb https://github.com/druce/rl/blob/master/Trading%20with%20RL.ipynb because even if it is slow, it still has great value for those who are learning, like myself. I only removed the “import resources” and its dependents because I am running it on Windows

I tried "
N_EPISODES = 10000
ticks_per_episode = 1256
nstocks = 1

env = Market(shm_market_gen,
nstocks=1,
episode_length=ticks_per_episode)

agent = DQN_Agent(state_size=nstocks*32,
action_size=2,
)

start_time = time.time()

for e in range(N_EPISODES):
agent.run_episode()
agent.score_episode(e, N_EPISODES)

if e and (e+1) % agent.save_interval == 0:
    agent.save()

elapsed_time = time.time() - start_time
print("Train time: ", elapsed_time)
agent.rlplot()"
Unfortunately, I got:

Traceback (most recent call last):
File "C:\Users\guest\AppData\Roaming\Python\Python310\site-packages\IPython\core\interactiveshell.py", line 3526, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\guest\AppData\Local\Temp\ipykernel_11800\4180752183.py", line 19, in
agent.run_episode()
File "C:\Users\guest\AppData\Local\Temp\ipykernel_11800\165701735.py", line 198, in run_episode
self.action = self.act(self.state.reshape([1, self.state_size]))
ValueError: cannot reshape array of size 8 into shape (1,32)"

Trading With RL Pickle Error - TypeError: can't pickle _thread.RLock objects

Love the work you've posted! Super thorough.

I'm trying to implement the Trading with RL notebook into Google Colab but running into an issue. I will document my moves here for reproducibility.

Basically, I copied, cell for cell, the notebook from this repository into a Google Colab notebook.

First, you have to install backtrader into the environment. I did this by putting this at the very beginning of the imports:

try:
    import backtrader as bt
except:
    print('Backtrader not installed yet. Installing now...')
    !pip install backtrader
    print('Backtrader installed.')
    print('Restart and Run All now.')
    exit()

This way, it'll prompt the user to Restart and Run All once Backtrader is installed.

Second issue I figured out was to make sure this notebook is being run with eager execution disabled, so I added:

from tensorflow.compat import v1
v1.disable_eager_execution()

...to the imports at the top as well.

Now my issue is this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-e8262df5188b> in <module>()
     38 
     39     if e and (e+1) % agent.save_interval == 0:
---> 40         agent.save()
     41 
     42 elapsed_time = time.time() - start_time

<ipython-input-20-21179c258b19> in save(self)
    149         self.predict_model.save("%s_predict.h5" % fullname)
    150         # can't save / load train model due to custom loss
--> 151         pickle.dump(self, open("%s.p" % fullname, "wb"))
    152 
    153     def load(filename, memory=True):

TypeError: can't pickle _thread.RLock objects

This happens when the cell with this at the beginning is run (i.e. the cell after the class REINFORCE_Agent(Agent): cell) :

N_EPISODES = 2000
ticks_per_episode = 1256
nstocks = 1
lag = 1

Googling that error, I found this answer that might help? But hoping to get some help troubleshooting this one? Would greatly appreciate it as I can't wait to get this working online.

Thanks!

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.