Issue
X = np.array([
[-2,4,-1],
[4,1,-1],
[1, 6, -1],
[2, 4, -1],
[6, 2, -1],
])
for epoch in range(1,epochs):
for i, x in enumerate(X):
X = torch.tensor([
[-2,4,-1],
[4,1,-1],
[1, 6, -1],
[2, 4, -1],
[6, 2, -1],
])
The looping was fine when it was an numpy array. But i want to work with pytorch tensors so what's alternative to enumerate or How can i loop through the above tensor in the 2nd line?
Solution
enumerate
expects an iterable, so it works just fine with pytorch tensors too:
X = torch.tensor([
[-2,4,-1],
[4,1,-1],
[1, 6, -1],
[2, 4, -1],
[6, 2, -1],
])
for i, x in enumerate(X):
print(x)
tensor([-2, 4, -1])
tensor([ 4, 1, -1])
tensor([ 1, 6, -1])
tensor([ 2, 4, -1])
tensor([ 6, 2, -1])
If you want to iterate over the underlying arrays:
for i, x in enumerate(X.numpy()):
print(x)
[-2 4 -1]
[ 4 1 -1]
[ 1 6 -1]
[ 2 4 -1]
[ 6 2 -1]
Do note however that pytorch's underlying data structure are numpy arrays, so you probably want to avoid looping over the tensor as you're doing, and should be looking at vectorising any operations either via pytorch or numpy.
Answered By - yatu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.