【问题标题】:What are the weights and bias for the AND perceptron?AND 感知器的权重和偏差是多少?
【发布时间】:2020-05-21 14:37:13
【问题描述】:

我正在实施 AND 感知器,但在决定组合的权重和偏差以使其与 AND 真值表匹配时遇到困难。

这是我编写的代码:

import pandas as pd

# Set weight1, weight2, and bias
weight1 = 2.0
weight2 = -1.0
bias = -1.0

# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [False, False, False, True]
outputs = []

# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
    linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
    output = int(linear_combination >= 0)
    is_correct_string = 'Yes' if output == correct_output else 'No'
    outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])

# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', '  Input 2', '  Linear Combination', '  Activation Output', '  Is Correct'])
if not num_wrong:
    print('Nice!  You got it all correct.\n')
else:
    print('You got {} wrong.  Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))

我必须从上述值中决定 weight1、weight2 和 bias。当有 10 作为输入时,我得到一个输出错误。

感谢您的帮助。

【问题讨论】:

    标签: python pandas neural-network


    【解决方案1】:
    • 方程是对称的:两个输入在功能上是等效的。
    • 以权重作为变量,三个(现在两个)变量中有四个(现在三个)不等式。你在解决那个系统时遇到什么问题?

    系统:

    w = weight (same for both inputs)
    b = bias
    
    0*w + 0*w + b <= 0
    1*w + 0*w + b <= 0
    1*w + 1*w + b >  0
    

    这让你有

    w + b <= 0
    2*w + b > 0
    

    您应该能够从那里描述可能的解决方案。

    【讨论】:

      【解决方案2】:

      尝试使用relu激活函数,看看是否能解决你的问题

      relu(weight1 * test_input[0] + weight2 * test_input[1] + bias)

      1、1 和 -1.5 应该可以工作。

      【讨论】:

      • 1, 1, -1.5 对我有用,但不需要 relu 激活功能。
      • int(linear_combination >= 0) 在某种意义上相当于一个 relu 函数。如果你把它改成int(linear_combination > 0),它就变成了一个relu激活函数。 Prune 提到了许多解决方案。
      • 感谢@Lufy 是的 1,1 和 -1.5 与我一起用于 OR 运算符,AND 运算符呢?
      【解决方案3】:

      AND 感知器:

      weight1 = 1.0
      weight2 = 1.0
      bias = -2.0
      

      或感知器:

      weight1 = 1.0
      weight2 = 1.0
      bias = -1
      

      不是感知器:

      weight1 = 1.0
      weight2 = -2.0
      bias = 0
      

      偏差充当调整线性方程的截距。

      【讨论】:

        【解决方案4】:

        感知器算法

        如果wx+b &gt;=0 and 0 if wx+&lt;0,则预测 = 1

        所以输入是(0, 0), (0, 1), (1, 0), (1, 1) 确保您为 weight1、weight2 和 bias 输入的数字会将 &lt; 0 输出为假,&gt;=0 输出为真

        1. 对于输入 (0,0)

        weight1*0+weight2*0+-2

        1*0+1*0-2 = -2

        1. 对于输入 (0,1)

        1*0+1*1-2 = -1

        1. 用于输入 (1,0)

        1*1+1*0-2 = -1

        1. 用于输入 (1,1)

        1*1+1*1-2 = 0

        因此,我的 weight1 =1,weight2 =1,bias = -2。我得到的所有答案都是正确的

        您可以使用任何您想要的 weight1,weight2 和 bias 值,只要输出说明 AND 操作即可。请记住,您可以申请 OR 和其他操作

        【讨论】:

          猜你喜欢
          • 2020-07-27
          • 2019-12-01
          • 1970-01-01
          • 2014-12-09
          • 2018-01-09
          • 2014-12-11
          • 2023-03-11
          • 2017-02-07
          • 1970-01-01
          相关资源
          最近更新 更多