用 Python 从零开始创建神经网络(一):编码我们的第一个神经元

编码我们的第一个神经元

  • 引言
  • 1. A Single Neuron:
  • Example 1
  • Example 2
  • 2. A Layer of Neurons:
  • Example 1
  • 引言

    本教程专为那些对神经网络已有基础了解、但尚未动手实践过的读者而设计。尽管网上充斥着各种教程,但很多内容要么过于简略,要么直接进入高级主题,让初学者难以跟上。本指南将带领你从零开始,用 Python 构建一个简单的神经网络模型,逐步拆解每一步,帮助你真正理解神经网络的工作原理,为今后的深入学习打下坚实基础。

    1. A Single Neuron:

    最简单基础的单个神经元:

    Example 1

    inputs = [1, 2, 3]
    weights = [0.2, 0.8, -0.5]
    bias = 2
    output = (inputs[0]*weights[0] + 
              inputs[1]*weights[1] + 
              inputs[2]*weights[2] + bias)
    print("output:", output)
    
    >>>
    output: 2.3
    


    代码的可视化:https://nnfs.io/bkr/

    Example 2

    inputs = [1.0, 2.0, 3.0, 2.5]
    weights = [0.2, 0.8, -0.5, 1.0]
    bias = 2.0
    output = (inputs[0]*weights[0] +
              inputs[1]*weights[1] +
              inputs[2]*weights[2] +
              inputs[3]*weights[3] + bias)
    print(output)
    
    >>>
    output: 2.3
    output: 4.8
    


    代码的可视化:https://nnfs.io/djp/

    2. A Layer of Neurons:

    一层神经元:


    Example 1

    假设我们有这样一个场景:一层有 3 个神经元,4 个输入。

    inputs = [1, 2, 3, 2.5]
    
    weights1 = [0.2, 0.8, -0.5, 1]
    weights2 = [0.5, -0.91, 0.26, -0.5]
    weights3 = [-0.26, -0.27, 0.17, 0.87]
    
    bias1 = 2
    bias2 = 3
    bias3 = 0.5
    
    outputs = [
            # Neuron 1:
            inputs[0]*weights1[0] +
            inputs[1]*weights1[1] +
            inputs[2]*weights1[2] +
            inputs[3]*weights1[3] + bias1,
            # Neuron 2:
            inputs[0]*weights2[0] +
            inputs[1]*weights2[1] +
            inputs[2]*weights2[2] +
            inputs[3]*weights2[3] + bias2,
            # Neuron 3:
            inputs[0]*weights3[0] +
            inputs[1]*weights3[1] +
            inputs[2]*weights3[2] +
            inputs[3]*weights3[3] + bias3
            ]
    
    print("outputs:", outputs)
    
    >>>
    outputs: [4.8, 1.21, 2.385]
    

    代码的可视化:https://nnfs.io/mxo/

    在这段代码中,我们有三组权重和三个偏差,它们定义了三个神经元。每个神经元都“连接”到相同的输入。不同之处在于每个神经元对输入应用的权重和偏差是分开的。这称为全连接神经网络——当前层的每个神经元都与前一层的每个神经元相连。这是一种非常常见的神经网络类型,但应该注意,并非一定要像这样完全连接。到目前为止,我们只展示了一个包含很少神经元的单层的代码。想象一下编码更多层和更多神经元,这将变得非常具有挑战性。相对于使用我们当前的方法,我们可以使用循环来扩展并动态处理输入和层的大小。我们已将分开的权重变量转换为一个权重列表,这样我们可以遍历它们,并且我们改变了代码使用循环而不是硬编码的操作。

    inputs = [1, 2, 3, 2.5]
    weights = [[0.2, 0.8, -0.5, 1],
               [0.5, -0.91, 0.26, -0.5],
               [-0.26, -0.27, 0.17, 0.87]]
    biases = [2, 3, 0.5]
    
    # Output of current layer
    layer_outputs = []
    # For each neuron
    for neuron_weights, neuron_bias in zip(weights, biases):
        # Zeroed output of given neuron
        neuron_output = 0
        # For each input and weight to the neuron
        for n_input, weight in zip(inputs, neuron_weights):
            # Multiply this input by associated weight
            # and add to the neuron’s output variable
            neuron_output = neuron_output + n_input*weight
        # Add bias
        neuron_output = neuron_output + n_input*weight
        # Put neuron’s result to the layer’s output list
        layer_outputs.append(neuron_output)
        
    print("layer_outputs:", layer_outputs)
    
    print(list(zip(weights, biases)))
    print(list(zip(weights, biases))[0])
    print(type(list(zip(weights, biases))[0]))
    
    >>>
    layer_outputs: [4.8, 1.21, 2.385]
    
    [([0.2, 0.8, -0.5, 1], 2), ([0.5, -0.91, 0.26, -0.5], 3), ([-0.26, -0.27, 0.17, 0.87], 0.5)]
    ([0.2, 0.8, -0.5, 1], 2)
    <class 'tuple'>
    

    这做的和之前一样,只是以一种更动态和可扩展的方式。如果你在某个步骤感到困惑,可以打印print()出对象来看看它们是什么以及发生了什么。zip()函数让我们能够同时迭代多个可迭代对象(在这种情况下是列表)。再次说明,我们所做的就是,对每个神经元(上述代码中的外层循环,遍历神经元的权重和偏差),取每个输入值与该输入相关联的权重相乘(上述代码中的内层循环,遍历输入和权重),将所有这些相加,然后在最后加上一个偏差。最后,将神经元的输出发送到层的输出列表中。

    作者:墨绿色的摆渡人

    物联沃分享整理
    物联沃-IOTWORD物联网 » 用 Python 从零开始创建神经网络(一):编码我们的第一个神经元

    发表回复