【问题标题】:How to resolve the error 'mat1 and mat2 shapes cannot be multiplied (1000x1 and 3x512)' in NeuroDiffEq?如何解决 NeuroDiffEq 中的错误“mat1 和 mat2 形状不能相乘(1000x1 和 3x512)”?
【发布时间】:2021-05-17 05:47:45
【问题描述】:

我是神经网络的新手,对它们的使用方式有基本的了解。我正在尝试使用人工神经网络(ANN),特别是使用 NeuroDiffEq 包来解决具有边界条件的球形拉普拉斯方程:u(r=0)=u(r=1)=0 对于所有 theta 和 phi Python。以下是相同的代码

import numpy as np
import matplotlib.pyplot as plt
import torch
from neurodiffeq import diff 
from neurodiffeq.networks import FCNN 
from neurodiffeq.conditions import DirichletBVPSpherical
from neurodiffeq.solvers import SolverSpherical
from neurodiffeq.monitors import MonitorSpherical
from neurodiffeq.generators import Generator3D
%matplotlib notebook

laplace = lambda u, r, theta, phi: [
diff(((r**2)*diff(u,r,order=1)), r, order=1)/r**2 + 
diff((np.sin(theta))*diff(u,theta,order=1), theta, order=1)/((r**2)*(np.sin(theta))) +
diff(u,phi,order=2)/(r*np.sin(theta))**2
]

conditions = [
    DirichletBVPSpherical(r_0=0.0,f=0.0,r_1=1.0,g=0.0)
]

nets = [
FCNN(n_input_units=3, n_output_units=1, hidden_units=[512]),
]

monitor=MonitorSpherical(r_min=0.0,r_max=1.0,check_every=10,shape=(10,10,10),r_scale='linear',theta_min=0,theta_max=np.pi,phi_min=0,phi_max=2*np.pi)
monitor_callback = monitor.to_callback()

solver = SolverSpherical(
    pde_system=laplace,
    conditions=conditions,
    r_min=0.0,
    r_max=1.0,
    nets=nets,
    train_generator=Generator3D(grid=(10, 10, 10), xyz_min=(0.0, 0.0, 0.0), xyz_max=(1.0, 1.0, 1.0), method='equally-spaced'),
    valid_generator=Generator3D(grid=(10, 10, 10), xyz_min=(0.0, 0.0, 0.0), xyz_max=(1.0, 1.0, 1.0), method='equally-spaced-noisy'),
)

solver.fit(max_epochs=200, callbacks=[monitor_callback])

solution_neural_net_laplace = solver.get_solution()

我收到以下错误

mat1 and mat2 shapes cannot be multiplied (1000x1 and 3x512)

对于解决此错误的任何帮助,我将不胜感激。提前致谢!

【问题讨论】:

  • 我更新了我的答案,检查它是否有效,如果有效,记得接受。
  • 是的,我试过了,但没有用。我把 hidden_​​units=512 和 n_input_units=512。但我不明白为什么 n_input_units 应该是 512,因为我给网络的输入是 r、theta、phi(即 3)。

标签: python matrix pde


【解决方案1】:

问题是mat1 的形状不正确,不能与mat2 相乘。可能是您使用了 10x10x10 = 1000 的网格,因此请尝试将其设为其他内容,即 8x8x8 = 512,或者您可以尝试将输入单位设为 = 1000,看看是否能解决问题。

也可能是n_input_units = 512n_input_units = 1000,与n_hidden_units = [something else](取决于您对网格所做的更改)

【讨论】:

  • 是的,我尝试这样做,但它会产生相同的错误。我认为 mat1 的列和 mat2 的行必须相等。
  • 它会产生完全相同的错误吗?还是括号里的形状有变化?
  • 将网格更改为 8.8.8=512 会将错误更改为“mat1 和 mat2 形状不能相乘(512x1 和 3x512)。”
  • 我认为问题在于 n_input_units,应该是 512 而不是 3。
猜你喜欢
  • 2022-07-15
  • 2021-05-25
  • 2021-11-14
  • 2021-09-09
  • 2021-09-23
  • 1970-01-01
  • 2023-01-28
  • 2021-09-17
  • 2022-08-22
相关资源
最近更新 更多