为什么我的MLP始终输出-1?

问题描述 投票:0回答:1

我需要获取从单位平方统一生成的二维输入数据,然后基于两个假设函数h1(x) = w1^T*xh2(x) = w2^T*x的XOR将数据标记为1或-1,其中w1 = [0, 1, -1]和[C0 ]。从那里,我应该使用theta符号函数通过三层多层感知器运行数据。由于某种原因,无论什么情况,我的MLP都会为所有点输出-1。我的错误在哪里?这是我的代码:

w2 = [0, 1, 1]
python neural-network perceptron
1个回答
0
投票

我知道了。对于节点22和23,计算需要在符号函数内,因此j for循环应如下所示:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-.5, .5, (100,3))
x[:,0] = 1
y = np.ones(100)
w1 = np.array([0, 1, -1]).T
w2 = np.array([0, 1, 1]).T
h1 = np.sign(w1[1]*x[:,1] + w1[2]*x[:,2])
h2 = np.sign(w2[1]*x[:,1] + w2[2]*x[:,2])
for i in range(np.size(x,0)):
    if (h1[i]<0 and h2[i]>0) or (h1[i]>0 and h2[i]<0):
        y[i] = 1
    else:
        y[i] = -1
print("h1:")
print(h1)
print("h2:")
print(h2)

#nodes 21 and 31 are just 1 with weights -1.5 and 1.5, respectively
node22 = np.ones((100,1))
node23 = np.ones((100,1))
node32 = np.ones((100,1))
node33 = np.ones((100,1))
out = np.ones((100,1))
for j in range(np.size(x,0)):
    node22[j] = np.matmul(w1,x[j,:])
    node23[j] = np.matmul(w2,x[j,:])
    node32[j] = np.sign(-1.5 + node22[j] - node23[j])
    node33[j] = np.sign(-1.5 - node22[j] + node23[j])
    out[j] = np.sign(1.5 + node32[j] + node33[j])
print("Layer 2, Node 2:")
print(node22)
print("Layer 2, Node 3:")
print(node23)
print("Layer 3, Node 2:")
print(node32)
print("Layer 3, Node 3:")
print(node33)
print("f:")
print(out)

error = 0
for k in range(np.size(x,0)):
    if y[k] != out[k]:
        error +=1
error = error/np.size(x,0)
print(error)
© www.soinside.com 2019 - 2024. All rights reserved.