运行时错误:大小不匹配,m1:[5]x 10],m2:[5 x 32],位于/pyttorc/aten/src/TH/g



我需要你的帮助。运行下面的代码抛出:

RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp

我看了类似的问题,但它们与图像有关,建议将输入变平,我尝试了一下,但没有成功。

我使用的是Python 3.6.8和torch 1.1.0

代码示例:

state = [[307, 1588204935.0, 1.0869, 1.08708, 1.08659, 1.08662, 1.08708, 1.08724, 1.08674, 1.08677],
[370, 1588204920.0, 1.08668, 1.08709, 1.08661, 1.08693, 1.08682, 1.08724, 1.08677, 1.08708],
[243, 1588204905.0, 1.08637, 1.08671, 1.08624, 1.08669, 1.08651, 1.08686, 1.08639, 1.08683],
[232, 1588204890.0, 1.08614, 1.08656, 1.08609, 1.08636, 1.08628, 1.0867, 1.08626, 1.0865],
[349, 1588204875.0, 1.086, 1.08623, 1.08585, 1.08614, 1.08614, 1.08638, 1.08597, 1.0863]]
def predict(state, state_dim=5, action_dim=3, hidden_dim=32, lr=0.05):
""" Compute Q values for all actions using the DQL. """
criterion = torch.nn.MSELoss()
model = torch.nn.Sequential(torch.nn.Linear(state_dim, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, hidden_dim*2),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim*2, action_dim))
optimizer = torch.optim.Adam(model.parameters(), lr)
with torch.no_grad():
return model(torch.Tensor(state)) # Error throw here
predict(state).tolist()

将我的state_dim5更改为10解决了我的问题。

参考:https://discuss.pytorch.org/t/runtimeerror-size-mismatch-m1-5-x-10-m2-5-x-32-at-pytorch-aten-src-th-generic-thtensormath-cpp/79714/2?u=masilive_sifanele

最新更新