Guest User

Untitled

a guest
May 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. def train(model, epochs):
  2. total = 0
  3. start = time.time()
  4. entire_hist = []
  5. profits = []
  6. for i in range(epochs):
  7. loss = 0
  8. accuracy = 0
  9. hist = []
  10. env.reset()
  11. game_over = False
  12. input_t = env.observe()
  13.  
  14. while not game_over:
  15. input_tm1 = input_t
  16.  
  17. if np.random.random() <= epsilon:
  18. action = np.random.randint(0, num_actions, size=None)
  19. else:
  20. q = model.predict(input_tm1)
  21. action = np.argmax(q[0])
  22.  
  23. input_t, reward, game_over = env.act(action)
  24.  
  25. exp_replay.remember([input_tm1, action, reward, input_t], game_over)
  26. inputs, targets = exp_replay.get_batch(model, batch_size=batch_size)
  27.  
  28. loss, accuracy = model.train_on_batch(inputs, targets)
  29. hist.append([loss, accuracy, env.main])
  30.  
  31. print(f'counter: {env.counter}, action taken: {action}, reward: {round(reward, 2)}, main: {round(env.main)}, secondary: {env.secondary}')
  32.  
  33. if game_over:
  34. print('GAME OVER!')
  35.  
  36. entire_hist.append(hist)
  37. profits.append(total)
  38. print(f'total profit: {env.total_profit}')
  39. print(f'epoch: {i}, loss: {loss}, accuracy: {accuracy}')
  40. print('n')
  41. print('*'*20)
  42.  
  43.  
  44. end = int(time.time() - start)
  45. print(f'training time: {end} seconds')
  46. return entire_hist, total
Add Comment
Please, Sign In to add comment