深度学习入门:简单神经网络的构建与实现

163 阅读1分钟

深度学习中,神经网络是核心模型。今天我们用 Python 和 NumPy 构建一个简单的神经网络。

神经网络由多个神经元组成,神经元之间通过权重连接。我们构建一个包含输入层、隐藏层和输出层的简单神经网络。

首先,导入必要的库:

收起

python

import numpy as np

定义激活函数 Sigmoid:

收起

python

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

定义神经网络的结构和参数初始化:

收起

python

# 输入层节点数
input_size = 2
# 隐藏层节点数
hidden_size = 3
# 输出层节点数
output_size = 1

# 初始化权重,使用随机数
weights1 = np.random.randn(input_size, hidden_size)
weights2 = np.random.randn(hidden_size, output_size)

前向传播函数:

收起

python

def forward_propagation(inputs):
    hidden_layer = sigmoid(np.dot(inputs, weights1))
    output_layer = sigmoid(np.dot(hidden_layer, weights2))
    return output_layer

假设我们有一个输入数据:

收起

python

# 示例输入
inputs = np.array([0.5, 0.3])
output = forward_propagation(inputs)
print(f"神经网络的输出: {output}")

在这个简单的神经网络中,输入数据通过权重矩阵与隐藏层和输出层进行计算,经过激活函数处理后得到最终输出。虽然这只是一个简单的示例,但理解其原理是深入学习深度学习的基础。