简体   繁体   中英

Enumerate array placed in torch.autograd.Variable

I have variable xy that loads data from file and is used to create x_data and y_data variables of type torch.autograd.Variable . I suppose x_data and y_data content are arrays. But how to enumerate that array in order to print content?

d = r"C:\gdrive\python\diabetes.csv"
import torch
from torch.autograd import Variable 
import numpy as np
import torch.nn.functional as F
xy = np.loadtxt(d,delimiter=',',dtype=np.float32 )
print("***")
print(xy)
x_data = Variable(torch.from_numpy(xy[:,0:-1]))
y_data=  Variable(torch.from_numpy(xy[:,[-1]]))

You can enumerate/iterate over Pytorch tensors the same way you can for numpy arrays and standard Python objects.

You should be able to iterate pytorch tensors just like python lists:

import torch
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F

d = r"C:\gdrive\python\diabetes.csv"
xy = np.loadtxt(d, delimiter=',', dtype=np.float32)
print("***")
x_data = Variable(torch.from_numpy(xy[:,0:-1]))
y_data = Variable(torch.from_numpy(xy[:,[-1]]))

print(x_data.shape)
print(y_data.shape)
for i, (x, y) in enumerate(zip(x_data, y_data)):
    print("example:", i)
    print("features:", x)
    print("label:", y)
    print()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM