gpt4 book ai didi

python - 如何在for循环中索​​引张量?

转载 作者:行者123 更新时间:2023-12-04 07:57:57 30 4
gpt4 key购买 nike

我收到一条错误消息,指出我的张量必须是 long、byte 或 bool 张量。调试告诉我它没有得到索引 [i] , 但实际行 [i] 张量 x .我需要该索引来获取另一个张量的另一行 y ,寿。
任何想法如何解决这个问题?

number_batches_in = int(len(dataset_in)/batch_size)
number_batches_out = int(len(dataset_out)/batch_size)

x = torch.empty(size=(number_batches_in, 800))
y = torch.empty(size=(number_batches_out,1200), dtype=torch.float64)

for index, (x1, x2) in enumerate(dataloader_in):
batch = torch.cat((x1, x2), 0)
x[index] = batch

for index, (y1, y2, y3) in enumerate(dataloader_out):
batch = torch.cat((y1, y2, y3), 0)
y[index] = batch


model = Network(800,1200,3,800,200)
SAVE_PATH = "trained/model.dat"
epochs = 5
learning_rate = 0.001
optimizer = optim.Adam(model.parameters(),lr=learning_rate, eps=1e-08)
hist_error = []
hist_loss = []
beta = 0.5

for epoch in range(epochs):
epoch_error = []
epoch_loss = []
for i in x:
optimizer.zero_grad()
pred = model.forward(i)
y_true = y[i]
loss = torch.mean(torch.sum((pred - y_true)) ** 2)
loss.backward()
optimizer.step()
error = torch.mean(torch.sqrt((pred - y[i]) ** 2)).detach().numpy()
epoch_error.append(error)
epoch_loss.append(loss.data.detach().numpy())
hist_error.append(np.mean(epoch_error))
hist_loss.append(np.mean(epoch_loss))
print("Epoch %d -- loss %f, RMS error %f " % (epoch+1, hist_loss[-1], hist_error[-1]))
torch.save(model.state_dict(), SAVE_PATH)
print("Model saved to %s" % SAVE_PATH)
错误信息:
Traceback (most recent call last):
File "/home/samim/miniconda3/envs/deep/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-54864ad18480>", line 1, in <module>
runfile('/home/samim/Documents/train.py', wdir='/home/samim/Documents/')
File "/home/samim/.local/share/JetBrains/PyCharm2020.3/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/home/samim/.local/share/JetBrains/PyCharm2020.3/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/samim/Documents/train.py", line 61, in <module>
y_true = y[i]
IndexError: tensors used as indices must be long, byte or bool tensors

最佳答案

要么 enumerate :

...
for j, row_x in enumerate(x):
optimizer.zero_grad()
pred = model.forward(row_x)
y_true = y[j]
loss = torch.mean(torch.sum((pred - y_true)) ** 2)
...
或者更好, zip :
...
for row_x, y_true in zip(x, y):
optimizer.zero_grad()
pred = model.forward(row_x)
loss = torch.mean(torch.sum((pred - y_true)) ** 2)
...

关于python - 如何在for循环中索​​引张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66606405/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com