You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
lr = 0.0001
epochs = 2500
batch_size = 1000
losses = []
lossfn = torch.nn.CrossEntropyLoss()
for i in range(epochs):
rid = np.random.randint(0,mnist_data.train_data.shape[0],size=batch_size)
x = mnist_data.train_data[rid].float().flatten(start_dim=1)
x /= x.max()
pred = nn(x,w1,w2)
target = mnist_data.train_labels[rid]
loss = lossfn(pred,target)
losses.append(loss)
loss.backward()
with torch.no_grad():
w1 -= lr * w1.grad
w2 -= lr * w2.grad
I get an error returned for this and i have no idea how to fix it
I write in Spyder 5.4.3 with Python 3.11
codeline:
`import torch
import torchvision as TV
import numpy as np
from matplotlib import pyplot as plt
def nn(x,w1,w2):
l1 = x @ w1
l1 = torch.relu(l1)
l2 = l1 @ w2
return l2
w1 = torch.randn(784,200,requires_grad=True)
w2 = torch.randn(200,10,requires_grad=True)
mnist_data = TV.datasets.MNIST("MNIST", train=True, download=False)
plt.figure(figsize=(10,7))
plt.imshow(mnist_data.train_data[0])
plt.axis('off')
lr = 0.0001
epochs = 2500
batch_size = 1000
losses = []
lossfn = torch.nn.CrossEntropyLoss()
for i in range(epochs):
rid = np.random.randint(0,mnist_data.train_data.shape[0],size=batch_size)
x = mnist_data.train_data[rid].float().flatten(start_dim=1)
x /= x.max()
pred = nn(x,w1,w2)
target = mnist_data.train_labels[rid]
loss = lossfn(pred,target)
losses.append(loss)
loss.backward()
with torch.no_grad():
w1 -= lr * w1.grad
w2 -= lr * w2.grad
plt.figure(figsize=(10,7))
plt.xlabel("Training Time", fontsize=22)
plt.ylabel("Loss", fontsize=22)
plt.plot(losses)`
console return:
File ~/anaconda3/lib/python3.11/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
exec(code, globals, locals)
File ~/.spyder-py3/temp.py:49
plt.plot(losses)
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/pyplot.py:2812 in plot
return gca().plot(
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/axes/_axes.py:1688 in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/axes/_base.py:311 in call
yield from self._plot_args(
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/axes/_base.py:496 in _plot_args
x, y = index_of(xy[-1])
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/cbook/init.py:1661 in index_of
y = _check_1d(y)
File ~/anaconda3/lib/python3.11/site-packages/matplotlib/cbook/init.py:1353 in _check_1d
return np.atleast_1d(x)
File <array_function internals>:200 in atleast_1d
File ~/anaconda3/lib/python3.11/site-packages/numpy/core/shape_base.py:65 in atleast_1d
ary = asanyarray(ary)
File ~/anaconda3/lib/python3.11/site-packages/torch/_tensor.py:956 in array
return self.numpy()
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
The text was updated successfully, but these errors were encountered: