机器学习、深度学习和神经网络之间的区别和联系

  import numpy as np

  # 定义Sigmoid函数

  def sigmoid(x):

  return 1 / (1 + np.exp(-x))

  # 定义神经网络类

  class NeuralNetwork:

  def __init__(self, input_size, hidden_size, output_size):

  # 初始化权重

  self.W1 = np.random.randn(input_size, hidden_size)

  self.W2 = np.random.randn(hidden_size, output_size)

  def forward(self, X):

  # 前向传播

  self.z = np.dot(X, self.W1)

  self.z2 = sigmoid(self.z)

  self.z3 = np.dot(self.z2, self.W2)

  output = sigmoid(self.z3)

  return output

  def backward(self, X, y, output, learning_rate):

  # 反向传播

  self.output_error = y - output

  self.output_delta = self.output_error * sigmoid(output, derivative=True)

  self.z2_error = self.output_delta.dot(self.W2.T)

  self.z2_delta = self.z2_error * sigmoid(self.z2, derivative=True)

  self.W1 += X.T.dot(self.z2_delta) * learning_rate

  self.W2 += self.z2.T.dot(self.output_delta) * learning_rate

  def train(self, X, y, learning_rate=1, epochs=10000):

  for epoch in range(epochs):

  output = self.forward(X)

  self.backward(X, y, output, learning_rate)

  def predict(self, X):

  output = self.forward(X)

  return output

  # 创建一个神经网络实例

  input_size = 2

  hidden_size = 3

  output_size = 1

  nn = NeuralNetwork(input_size, hidden_size, output_size)

  # 准备训练数据

  X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

  y = np.array([[0], [1], [1], [0]])

  # 训练神经网络

  nn.train(X, y)

  # 预测新的数据

  new_data = np.array([0, 1])

  prediction = nn.predict(new_data)

  print("预测结果:", prediction)