Python pytorch不适用

Python pytorch不适用,python,pytorch,slice,Python,Pytorch,Slice,如何在pytorch中进行切片?我试过窄的和小的切片?两者都不用于输出列车数据和测试数据。有什么解决问题的建议吗 x1 = (max-min)*torch.rand(1, 21, dtype=torch.float) + min x2 = (max-min)*torch.rand(1, 21, dtype=torch.float) + min zipped_list = zip(x1, x2) y = torch.empty(1, 21) y = [torch.sin(2*x1+2) * tor

如何在pytorch中进行切片?我试过窄的和小的切片?两者都不用于输出列车数据和测试数据。有什么解决问题的建议吗

x1 = (max-min)*torch.rand(1, 21, dtype=torch.float) + min
x2 = (max-min)*torch.rand(1, 21, dtype=torch.float) + min
zipped_list = zip(x1, x2)
y = torch.empty(1, 21)
y = [torch.sin(2*x1+2) * torch.cos(0.5*x2)+0.5 for (x1, x2) in zipped_list]


print(y)

train_data = y.narrow(0,1)
test_data = y[11:21]
print(train_data)
输出为

AttributeError: 'list' object has no attribute 'narrow'
但是,当我执行正常切片时,测试数据将无法正确切片

train_data = y[0:11]
test_data = y[11:21]

在代码中,
y
是PyTorch张量:

y=torch.empty(1,21)
然后将其替换为PyTorch张量的
列表(实际上,只有一个):

y=[torch.sin(2*x1+2)*torch.cos(0.5*x2)+0.5表示压缩列表中的(x1,x2)]
因此,需要得到
y
的第一个元素,即张量,然后将其切片:

打印(y[0][:5])

这是否意味着即使它是一个张量,但y是张量列表,所以在这种情况下不能使用窄值?@MIT是的,你是对的
y
是一个简单的Python列表,它没有
shown
方法<代码>属性错误
因此被抛出。您只能使用PyTorch张量:
y[0]。窄带()
Train Data is: [tensor([-0.0515,  0.4574,  0.5141,  0.4865,  0.9266,  1.0984,  0.5364,  0.7042,
         0.1741, -0.4839,  0.4332,  0.2962,  0.2311,  0.6169,  0.4321,  0.4088,
         0.2443,  0.1982,  0.7978,  0.6651, -0.4453])]
Test Data is: []