text: import gym
  import numpy as np
  from itertools import count
  
  import torch
  import torch.nn as nn
  import torch.nn.functional as F
  import torch.optim as optim
  import torch.autograd as autograd
  from torch.autograd import Variable
  
  gamma = 0.99
  seed = 543
  render=False
  log_interval = 10
  
  
  env = gym.make('CartPole-v0')
  env.seed(seed)
  torch.manual_seed(seed)
  
  
  class Policy(nn.Module):
      def __init__(self):
          super(Policy, self).__init__()
          self.affine1 = nn.Linear(4, 128)
          self.affine2 = nn.Linear(128, 2)
  
          self.saved_actions = []
          self.rewards = []
  
      def forward(self, x):
          x = F.relu(self.affine1(x))
          action_scores = self.affine2(x)
          return F.softmax(action_scores)
  
  
  policy = Policy()
  optimizer = optim.Adam(policy.parameters(), lr=1e-2)
  
  
  def select_action(state):
      state = torch.from_numpy(state).float().unsqueeze(0)
      probs = policy(Variable(state))
      action = probs.multinomial()
      policy.saved_actions.append(action)
      return action.data
  
  
  def finish_episode():
      R = 0
      rewards = []
      for r in policy.rewards[::-1]:
          R = r + gamma * R
          rewards.insert(0, R)
      rewards = torch.Tensor(rewards)
      rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps)
      for action, r in zip(policy.saved_actions, rewards):
          action.reinforce(r)
      optimizer.zero_grad()
      autograd.backward(policy.saved_actions, [None for _ in policy.saved_actions])
      optimizer.step()
      del policy.rewards[:]
      del policy.saved_actions[:]
  
  
  running_reward = 10
  for i_episode in count(1):
      state = env.reset()
      for t in range(10000): # Don't infinite loop while learning
          action = select_action(state)
          state, reward, done, _ = env.step(action[0,0])
          if render:
              env.render()
          policy.rewards.append(reward)
          if done:
              break
  
      running_reward = running_reward * 0.99 + t * 0.01
      finish_episode()
      if i_episode % log_interval == 0:
          print('Episode {}\tLast length: {:5d}\tAverage length: {:.2f}'.format(
              i_episode, t, running_reward))
      if running_reward > 200:
          print("Solved! Running reward is now {} and "
                "the last episode runs to {} time steps!".format(running_reward, t))
          break