Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch 2 - Code doesn't run #7

Closed
LMolr opened this issue Jul 26, 2019 · 1 comment
Closed

Ch 2 - Code doesn't run #7

LMolr opened this issue Jul 26, 2019 · 1 comment

Comments

@LMolr
Copy link

LMolr commented Jul 26, 2019

Almost all the inline code snippets in chapter 2 are not working.

E.g:

Page 34

Code

plt.xlabel("Plays")
plt.ylabel("Avg Reward")
for i in range(500):
  if random.random() > eps:
    choice = get_best_arm(pastRewards, actions)
  else:
    choice = np.where(arms == np.random.choice(arms))[0][0]
  thisAV = np.array([[choice, reward(arms[choice])]])
  av = np.vstack((av, thisAV))
  percCorrect = 100*(len(av[np.where(av[:,0] == np.argmax(arms))])/len(av))
  runningMean = np.mean(av[:,1])
  plt.scatter(i, runningMean)

Error

NameError: name 'pastRewards' is not defined

Page 37

Code

for i in range(500):
  choice = np.random.choice(arms, p=av_softmax)
  counts[choice] += 1
  k = counts[choice]
  rwd = reward(arms[choice])
  old_avg = av[choice]
  new_avg = old_avg + (1/k)*(rwd - old_avg)
  av[choice] = new_avg
  av_softmax = softmax(av)

Error

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Page 41

Code

>>> x = torch.Tensor([2,4]) #input data
>>> m = torch.randn(2, requires_grad=True) #parameter 1
>>> b = torch.randn(1, requires_grad=True) #parameter 2
>>> y = m*x+b #linear model
>>> loss = (torch.sum(y_known - y))**2 #loss function
>>> loss.backward() #calculate gradients
>>> m.grad
tensor([ 0.7734, -90.4993])

Error

NameError: name 'y_known' is not defined

Code

model = torch.nn.Sequential(
  torchh.nn.Linear(10, 150),
  torch.nn.ReLU(),
  torch.nn.Linear(150, 4),
  torch.nn.ReLU(),
)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

Error

NameError: name 'torchh' is not defined
@outlace
Copy link
Collaborator

outlace commented Oct 15, 2019

We've uploaded new code Chx_book.ipynb notebooks that should all work for all chapters

@outlace outlace closed this as completed May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants