【问题标题】:Why is my MLP always outputting -1 no matter what?为什么无论如何我的 MLP 总是输出 -1?
【发布时间】:2020-04-25 05:58:47
【问题描述】:

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

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)

【问题讨论】:

    标签: python neural-network perceptron


    【解决方案1】:

    我想通了。对于节点 22 和 23,计算需要在一个符号函数内,所以 j for 循环应该是这样的:

    for j in range(np.size(x,0)):
        node22[j] = np.sign(np.matmul(w1,x[j,:]))
        node23[j] = np.sign(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])
    '''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-12
      • 2020-06-26
      • 2019-09-28
      • 2020-08-21
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多