python - AND 感知器的权重和偏差是多少?

标签 python pandas neural-network

我正在实现 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))

我必须根据提到的值来决定权重1、权重2和偏差。当输入为 10 时,我得到一个输出错误。

感谢您的帮助。

最佳答案

  • 该方程是对称的:两个输入在功能上是等效的。
  • 将权重作为变量,三个(现在是两个)变量中有四个(现在是三个)不等式。您在解决该系统问题上陷入了哪些困境?

系统:

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

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

关于python - AND 感知器的权重和偏差是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53822167/

相关文章:

python - 在 PyTorch 中使用 Conv2D 时,首先发生填充还是膨胀?

python - Ctypes 从双指针读取数据

python - 我正在尝试获取图像的 Base64 字符串

Python:TypeError:最多输入 1 个参数,得到 5 个

python - Pandas 函数 pandas.read_sql_table() 返回一个 DataFrame,其中值的顺序错误

python - 这是 seaborn.lineplot hue 参数中的错误吗?

python - 没有设置密码。 django 保存没有表单的用户

python - 如何正确地将小时数添加到 pandas.tseries.index.DatetimeIndex?

python - 具有 Shap ValueError : Layer sequential_1 was called with an input that isn't a symbolic tensor 的 DeepExplainer

python-3.x - 将 Keras 生成器转换为 Tensorflow 数据集以训练 Resnet50