本文主要是介绍AND 感知器练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
AND 感知器练习
AND 感知器的权重和偏置项是什么?
把权重 (weight1
, weight2
) 和偏置项 bias
设置成正确的值,使得 AND 可以实现上图中的运算。
在这里,在上图中可以看出有两个输入(我们把第一列叫做input1
,第二列叫做 input2
),在感知器公式的基础上,我们可以计算输出。
首先,线性组合就是权重与输入的点积:linear_combination = weight1*input1 + weight2*input2
,然后我们把值带入单位阶跃函数与偏置项相结合,给到我们一个(0 或 1)的输出:
import pandas as pd# TODO: Set weight1, weight2, and bias
weight1 = 1.0
weight2 = 1.0
bias = -2# DON'T CHANGE ANYTHING BELOW
# 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] + biasoutput = 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))
这篇关于AND 感知器练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!