0

如何找到weight1、weight2和bias的值?找到任何问题的这 3 个值的通用数学方法是什么!

import pandas as pd


weight1 = 0.0
weight2 = 0.0
bias = 0.0

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

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])


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))
4

5 回答 5

1

当您的输入为 [(0,0), (0,1), (1,0), (1,1)] 时,该问题要求您评估 weight1、weight2 和偏差,以产生 [False, False,假,真]。在这种情况下,“假”将是一个负数的结果。相反,“真”将是一个正数的结果。因此,您评估以下内容:

x1*weight1 + x2*weight2 + bias' 是正还是负

例如,设置 weight1=1、weight2=1 和 bias=-1.1(可能的解决方案),您将获得第一个输入:

0*1 + 0*1 + (-1.1) = -1.1 这是负数,这意味着它的计算结果为False

对于下一个输入:

0*1 + 1*1 + (-1.1) = -0.1 这是负数,意味着它的计算结果为False

对于下一个输入:

1*1 + 0*1 + (-1.1) = -0.1 这是负数,这意味着它的计算结果为False

最后一个输入:

1*1 + 1*1 + (-1.1) = +0.9 这是正数,这意味着它的计算结果为True

于 2018-11-05T01:55:30.547 回答
1

以下内容也对我有用:

weight1 = 1.5
weight2 = 1.5
bias = -2
于 2018-11-14T13:51:45.140 回答
0

以下对我有用:

weight1 = 1.5
weight2 = 1.5
bias = -2

当我更好地理解为什么会更新

于 2018-09-22T17:27:21.837 回答
0

好吧,在正规方程的情况下,您不需要偏置单元。因此,这可能是您所追求的(请记住,我已将您的TrueFalse值分别重铸为1and 0):

import numpy as np

A = np.matrix([[0, 0], [0, 1], [1, 0], [1, 1]])
b = np.array([[0], [0], [0], [1]])

x = np.linalg.inv(np.transpose(A)*A)*np.transpose(A)*b

print(x)

产量:

[[ 0.33333333]
 [ 0.33333333]]

此处提供了有关该解决方案的更多详细信息。

于 2018-03-14T19:14:19.303 回答
0

X1w1 + X2W2 + 偏差测试是:

linear_combination >= 0

从给定的输入值:

test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]

AND 值仅在测试中计算为 true 一次,因此典型 AND 运算的输出应为:

1   1   True 
1   0   False
0   1   False
0   0   False

给定,当我们在等式中输入测试输入:X1w1 + X2W2 + 偏差时,应该只有一个真实结果。如上所述,我们的测试是方程的线性组合应该大于或等于零。我相信问题正在寻找的是,从测试运行中可以看出,这个输出只有一个是真的。因此,要获得错误值,输出应该是负计算。最简单的方法是用小值和负偏差来测试方程。我试过了

weight1 = 1
weight2 = 1
bias = -2
于 2020-01-15T04:51:47.173 回答