我有以下问题。我有一个类似这样的输入:x = [1,2,3,4,5,6,7,8,9,10]
现在我希望将字母作为输入,而不是数字。它应该是这样的:x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
将出现以下错误消息:
input = var (torch.Tensor ([x for _ in range (30)])) ValueError: too many dimensions 'str'.
如何使用字母作为输入?当前代码如下所示:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as var
import torch.optim as optim
class Nnetz(nn.Module):
def __init__(self):
super(Nnetz, self).__init__()
self.lin1 = nn.Linear(10, 10)
self.lin2 = nn.Linear(10, 10)
self.lin3 = nn.Linear(10, 10)
self.lin4 = nn.Linear(10, 10)
self.lin5 = nn.Linear(10, 10)
self.lin6 = nn.Linear(10, 10)
def forward(self, x):
x = F.relu(self.lin1(x))
x = self.lin2(x)
x = self.lin3(x)
x = self.lin4(x)
x = self.lin5(x)
x = self.lin6(x)
return x
def num_flat_features(self, x):
size = x.size()[1:]
num = 1
for i in size:
num *= i
return num
netz = Nnetz()
for i in range(100):
x = [10,9,8,7,6,5,4,3,2,1,]
input = var(torch.Tensor([x for _ in range(10)]))
out = netz(input)
x = [1,2,3,4,5,6,7,8,9,10]
target = var(torch.Tensor([x for _ in range(10)]))
criterion = nn.MSELoss()
loss = criterion(out, target)
print(loss)
netz.zero_grad()
loss.backward()
optimizer = optim.SGD(netz.parameters(), lr=0.01)
optimizer.step()
目前没有回答
相关问题 更多 >
编程相关推荐