How to use neural network to realize logic 'and' and 'or'?

1.Realize logic ‘and’

1.1precondition

我们有两个输入,分别是 x 1 x_1 & x 2 x_2 ,且他们只能取0或者1.
神经网络的模型为
在这里插入图片描述
同时,我们有 h θ ( x ) = 1 1 + e θ T x h_{\theta}(x)=\frac{1}{1+e^{\theta^Tx}} 以及 h θ ( z ) h_\theta(z) 的图形
在这里插入图片描述

1.2way to realize it

由函数可以看出,只要输入z(即 θ 0 + θ 1 x 1 + θ 2 x 2 \theta_0+\theta_1x_1+\theta_2x_2 )大于4,就能得到1,否则就让其值小于-4.
我们赋值为 θ 0 = 30 , θ 1 = 20 , θ 2 = 20 \theta_0=-30,\theta_1=20,\theta_2=20 ,就能得到以上结果,用一个真值表来说明

x 1 x_1 x 2 x_2 z y
1 1 10 \approx 1
1 0 -10 \approx 0
0 1 -10 \approx 0
0 0 -30 \approx 0

由真值表来看,我们实现了“与”逻辑。

2.Realize logic ‘or’,‘not’ and ‘notand’

2.1Realize logic ‘or’

由上面的例子,我们可以得到这样一组 θ \theta 来实现逻辑或。
θ 0 = 20 , θ 1 = 30 , θ 2 = 30 \theta_0=-20,\theta_1=30,\theta_2=30

2.2Realize logic ‘not’

x 0 x_0 y
1 0
0 1

根据真值表设计 θ \theta 值,可以得到
θ 0 = 10 , θ 1 = 20 \theta_0=10,\theta_1=-20

2.3Realize logic ‘nand’

所谓 ‘nand’,其实是这样的逻辑, ( n o t   x 1 ) a n d ( n o t   x 2 ) (not \ x_1)and(not \ x_2) ,列出其真值表

x 1 x_1 x 2 x_2 y
1 1 0
1 0 0
0 1 0
0 0 1

由上述真值表我们可以得到
θ 0 = 10 , θ 1 = 20 , θ 2 = 20 \theta_0=10,\theta_1=-20,\theta_2=-20

3.Realize logic ‘XNOR’

'XNOR’逻辑是相同为1,不同为0,可以得到真值表为

x 1 x_1 x 2 x_2 y
1 1 1
1 0 0
0 1 0
0 0 1

对于这样的逻辑,我们无法直接用一层网络来实现,需要用两层网络来实现,我们使用如下的神经网络
在这里插入图片描述
红色代表and逻辑,绿色代表Nand逻辑,蓝色代表OR逻辑。这也许代表了神经网络的学习方式,每一层学出一条特征,这些特征再通过一定的方式组合起来,在下一层中起到作用。

4.deep thinking on it

我们怎样确定每一层使用的模型?应该使用多少层?这是需要解决的问题。
下面的视频链接是用机器学习的方式来识别手写的邮政编码数字,很有意思,有条件的同学可以看一看。
https://www.youtube.com/watch?v=yxuRnBEczUU
在这里插入图片描述

原创文章 265 获赞 80 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xd15010130025/article/details/105173865