ValueError: matmul: 输入操作数 0 没有足够的维度(具有 0,带有签名 (n?,k) 的 gufunc 核心,(k,m?)->(n?,m?需要 1)



我是新的在箱子和程序A.I,我不知道如何解决这个问题,我是在YouTube的帮助下编程,但他没有这个问题,因为我是新的我不知道如何解决它,更不用说它是什么-帮助请D:

问题:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-58-2d579b4cf5ca> in <module>()
16 
17 
---> 18 train(neural_net, X, Y, l2_cost, 0.5)
<ipython-input-58-2d579b4cf5ca> in train(neural_net, X, Y, l2_cost, lr)
11   #Forward pass - Pase adelantado
12   for l, layer in enumerate(neural_net):
---> 13     z = out[-1][1] @ neural_net[l].W + neural_net[l].b
14     a = neural_net[l].act_f[0](z)
15     out.append((z, a))
ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

我在谷歌Colab编程-我真的需要帮助我的代码:"

import numpy as np
import scipy as cs
import matplotlib.pyplot as plt
from sklearn.datasets import make_circles
# Crera Data set
n = 500 #cantidad de cosas a analizar
p = 2 #cantidad de caracterizticas de las cosas a analizar
X, Y = make_circles(n_samples = n, factor = 0.5, noise=0.05)
plt.scatter(X[Y == 0,0], X[Y == 0,1])
plt.scatter(X[Y == 1,0], X[Y == 1,1], c = 'salmon')
plt.show()
#Clase de la capa de la RED
class neural_layer():
def __init__(self, n_conn, n_neur, act_f):
self.act_f = act_f
self.b = np.random.rand(1, n_neur) * 2 - 1
self.W = np.random.rand(n_conn, n_neur) * 2 - 1
#Funciones de Activacion
sigm = (lambda x: 1 / (1 + np.e ** (-x)),
lambda x: x * (1 - x))
relu = lambda x: np.maximum(0, x)
_x = np.linspace(-5, 5, 100)
plt.plot(_x, relu(_x))
l0 = neural_layer(p, 4, sigm)
l1 = neural_layer(4, 8, sigm)
#hasta el infinito....
def create_nn(topology, act_f):
nn = [] #contenedor de cada una de las capas de la red 'Neural Network'
for l, layer in enumerate(topology[:-1]):
nn.append(neural_layer(topology[1], topology[l+1], act_f))
return nn
topology = [p, 4, 8, 16, 8, 4, 1] #topologia de la red
neural_net = create_nn(topology, sigm)
l2_cost = (lambda Yp, Yr: np.mean((Yp - Yr) ** 2), #Error cuadratico medio
lambda Yp, Yr: (Yp - Yr))
def train(neural_net, X, Y, l2_cost, lr=0.5):
out = [(None, X)]
#Forward pass - Pase adelantado
for l, layer in enumerate(neural_net):
z = out[-1][1] @ neural_net[l].W + neural_net[l].b
a = neural_net[l].act_f[0](z)
out.append((z, a))
train(neural_net, X, Y, l2_cost, 0.5)

"

您正在尝试将两个不相容的矩阵相乘。

W形= (4,4)

out[-1][1] shape = (500,2)

在这一行

z = out[-1][1] @ neural_net[l].W + neural_net[l].b

这是你如何修复它

你必须改变下面的行,而不是"1"应该是"l">

恰克:

nn.append(neural_layer(topology[1], topology[l+1], act_f))

nn.append(neural_layer(topology[l], topology[l+1], act_f))

相关内容

  • 没有找到相关文章

最新更新