人工智能基础:从搜索到深度学习的完整知识体系 人工智能(AI)是计算机科学的一个分支,致力于创建能够执行通常需要人类智能的任务的系统。本文将系统性地介绍AI的核心概念、算法和实际应用。 AI发展历程 第一代AI(1956-1980):符号主义AI 第二代AI(1980-2010):统计机器学习 第三代AI(2010-至今):深度学习 核心AI技术 搜索算法 机器学习核心概念 深度学习架构 实际应用 计算机视觉 自然语言处理 AI伦理与安全 偏见检测 总结 人工智能从早期的符号推理发展到今天的深度学习,已经形成了完整的技术体系。
人工智能(AI)是计算机科学的一个分支,致力于创建能够执行通常需要人类智能的任务的系统。本文将系统性地介绍AI的核心概念、算法和实际应用。
# 早期AI示例:专家系统 class ExpertSystem: """基于规则的专家系统""" def __init__(self): self.rules = [] self.facts = {} def add_rule(self, condition, action): """添加规则""" self.rules.append({'condition': condition, 'action': action}) def add_fact(self, key, value): """添加事实""" self.facts[key] = value def infer(self, goal): """前向链推理""" # 检查是否已经是事实 if goal in self.facts: return self.facts[goal] # 尝试应用规则 for rule in self.rules: if rule['condition'](self.facts): result = rule['action'](self.facts) if goal in result: return result[goal] return None # 医疗诊断专家系统示例 medical_es = ExpertSystem() # 添加规则 medical_es.add_rule( condition=lambda facts: facts.get('temperature', 0) > 37.5, action=lambda facts: {'diagnosis': 'fever', 'severity': 'mild'} ) medical_es.add_rule( condition=lambda facts: facts.get('temperature', 0) > 39.0, action=lambda facts: {'diagnosis': 'high_fever', 'severity': 'severe'} ) # 添加事实 medical_es.add_fact('temperature', 38.5) # 推理 diagnosis = medical_es.infer('diagnosis') print(f"诊断结果: {diagnosis}") # 输出: fever
# 传统机器学习算法 import numpy as np from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.cluster import KMeans from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report class TraditionalML: """传统机器学习算法展示""" def __init__(self): self.models = { 'logistic_regression': LogisticRegression(max_iter=1000), 'decision_tree': DecisionTreeClassifier(), 'random_forest': RandomForestClassifier(n_estimators=100), 'svm': SVC(kernel='rbf'), 'kmeans': KMeans(n_clusters=3) } def train_and_evaluate(self, X, y, model_name='logistic_regression'): """训练和评估模型""" # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # 训练模型 model = self.models[model_name] model.fit(X_train, y_train) # 预测 y_pred = model.predict(X_test) # 评估 accuracy = accuracy_score(y_test, y_pred) report = classification_report(y_test, y_pred) return { 'model': model, 'accuracy': accuracy, 'report': report } def demonstrate_algorithms(self, X, y): """展示不同算法的效果""" results = {} for model_name in ['logistic_regression', 'decision_tree', 'random_forest']: if model_name != 'kmeans': # 跳过无监督学习 result = self.train_and_evaluate(X, y, model_name) results[model_name] = result['accuracy'] print(f"{model_name}: {result['accuracy']:.4f}") return results # 示例:鸢尾花分类 from sklearn.datasets import load_iris iris = load_iris() X, y = iris.data, iris.target ml_demo = TraditionalML() results = ml_demo.demonstrate_algorithms(X, y)
import torch import torch.nn as nn import torch.optim as optim class DeepLearning: """深度学习基础""" @staticmethod def create_simple_nn(input_size, hidden_size, output_size): """创建简单神经网络""" model = nn.Sequential( nn.Linear(input_size, hidden_size), nn.ReLU(), nn.Linear(hidden_size, hidden_size), nn.ReLU(), nn.Linear(hidden_size, output_size), nn.Softmax(dim=1) ) return model @staticmethod def train_model(model, X_train, y_train, epochs=100, lr=0.01): """训练模型""" criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=lr) losses = [] for epoch in range(epochs): # 前向传播 outputs = model(X_train) loss = criterion(outputs, y_train) # 反向传播 optimizer.zero_grad() loss.backward() optimizer.step() losses.append(loss.item()) if (epoch + 1) % 10 == 0: print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}') return model, losses @staticmethod def create_cnn(): """创建卷积神经网络""" class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(64 * 7 * 7, 128) self.fc2 = nn.Linear(128, 10) self.relu = nn.ReLU() def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = self.pool(self.relu(self.conv2(x))) x = x.view(-1, 64 * 7 * 7) x = self.relu(self.fc1(x)) x = self.fc2(x) return x return CNN() @staticmethod def create_rnn(): """创建循环神经网络""" class RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RNN, self).__init__() self.hidden_size = hidden_size self.rnn = nn.RNN(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x): out, _ = self.rnn(x) out = self.fc(out[:, -1, :]) # 取最后一个时间步 return out return RNN # 使用示例 # 创建模型 model = DeepLearning.create_simple_nn(input_size=4, hidden_size=10, output_size=3) # 模拟数据 X_train = torch.randn(100, 4) y_train = torch.randint(0, 3, (100,)) # 训练 trained_model, losses = DeepLearning.train_model(model, X_train, y_train)
from collections import deque import heapq class SearchAlgorithms: """经典搜索算法""" @staticmethod def bfs(graph, start, goal): """广度优先搜索""" queue = deque([[start]]) visited = set() while queue: path = queue.popleft() node = path[-1] if node == goal: return path if node not in visited: visited.add(node) for neighbor in graph.get(node, []): new_path = list(path) new_path.append(neighbor) queue.append(new_path) return None @staticmethod def dfs(graph, start, goal, visited=None): """深度优先搜索""" if visited is None: visited = set() if start == goal: return [start] if start in visited: return None visited.add(start) for neighbor in graph.get(start, []): path = SearchAlgorithms.dfs(graph, neighbor, goal, visited) if path: return [start] + path return None @staticmethod def a_star_search(graph, start, goal, heuristic): """A*搜索算法""" open_set = [] heapq.heappush(open_set, (0, start)) came_from = {} g_score = {start: 0} f_score = {start: heuristic(start, goal)} while open_set: current = heapq.heappop(open_set)[1] if current == goal: return SearchAlgorithms.reconstruct_path(came_from, current) for neighbor, cost in graph.get(current, {}).items(): tentative_g_score = g_score[current] + cost if neighbor not in g_score or tentative_g_score < g_score[neighbor]: came_from[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal) heapq.heappush(open_set, (f_score[neighbor], neighbor)) return None @staticmethod def reconstruct_path(came_from, current): """重建路径""" total_path = [current] while current in came_from: current = came_from[current] total_path.append(current) return total_path[::-1] # 示例:路径规划 graph = { 'A': {'B': 1, 'C': 4}, 'B': {'A': 1, 'C': 2, 'D': 5}, 'C': {'A': 4, 'B': 2, 'D': 1}, 'D': {'B': 5, 'C': 1} } def heuristic(node, goal): """简单的启发式函数(实际应用中需要更复杂的)""" return 0 # 这里简化为0,实际应使用距离估计 # A*搜索 path = SearchAlgorithms.a_star_search(graph, 'A', 'D', heuristic) print(f"最短路径: {path}") # 输出: ['A', 'B', 'C', 'D']
class MLConcepts: """机器学习核心概念""" @staticmethod def demonstrate_bias_variance(): """展示偏差-方差权衡""" import numpy as np import matplotlib.pyplot as plt np.random.seed(42) X = np.linspace(0, 10, 100) y_true = np.sin(X) y_noisy = y_true + np.random.normal(0, 0.3, 100) # 高偏差(欠拟合) # 简单线性回归无法拟合非线性关系 # 高方差(过拟合) # 高阶多项式可能会过拟合噪声 # 平衡点 # 适当的模型复杂度 return X, y_true, y_noisy @staticmethod def cross_validation(X, y, model, k=5): """K折交叉验证""" from sklearn.model_selection import KFold kf = KFold(n_splits=k, shuffle=True, random_state=42) scores = [] for train_index, test_index in kf.split(X): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] model.fit(X_train, y_train) score = model.score(X_test, y_test) scores.append(score) return np.mean(scores), np.std(scores) @staticmethod def feature_engineering_example(df): """特征工程示例""" import pandas as pd # 创建新特征 df['feature_ratio'] = df['feature1'] / df['feature2'] df['feature_sum'] = df['feature1'] + df['feature2'] # 特征选择 from sklearn.feature_selection import SelectKBest, f_classif selector = SelectKBest(score_func=f_classif, k=5) X_selected = selector.fit_transform(df.drop('target', axis=1), df['target']) return X_selected
class DeepLearningArchitectures: """深度学习架构""" @staticmethod def transformer_encoder_block(d_model, n_heads, d_ff, dropout=0.1): """Transformer编码器块""" class TransformerEncoderBlock(nn.Module): def __init__(self): super().__init__() self.self_attn = nn.MultiheadAttention(d_model, n_heads) self.feed_forward = nn.Sequential( nn.Linear(d_model, d_ff), nn.ReLU(), nn.Linear(d_ff, d_model) ) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.dropout = nn.Dropout(dropout) def forward(self, x, mask=None): # 自注意力 attn_output, _ = self.self_attn(x, x, x, attn_mask=mask) x = self.norm1(x + self.dropout(attn_output)) # 前馈网络 ff_output = self.feed_forward(x) x = self.norm2(x + self.dropout(ff_output)) return x return TransformerEncoderBlock() @staticmethod def residual_block(in_channels, out_channels, stride=1): """残差网络块""" class ResidualBlock(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1) self.bn1 = nn.BatchNorm2d(out_channels) self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) self.bn2 = nn.BatchNorm2d(out_channels) self.shortcut = nn.Sequential() if stride != 1 or in_channels != out_channels: self.shortcut = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride), nn.BatchNorm2d(out_channels) ) def forward(self, x): out = torch.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out += self.shortcut(x) out = torch.relu(out) return out return ResidualBlock()
class ComputerVisionApplications: """计算机视觉应用""" @staticmethod def image_classification_demo(): """图像分类演示""" from torchvision import models, transforms from PIL import Image # 加载预训练模型 model = models.resnet18(pretrained=True) model.eval() # 图像预处理 preprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) ]) # 分类图像 def classify_image(image_path): image = Image.open(image_path) input_tensor = preprocess(image) input_batch = input_tensor.unsqueeze(0) with torch.no_grad(): output = model(input_batch) # 获取预测结果 probabilities = torch.nn.functional.softmax(output[0], dim=0) return probabilities return classify_image @staticmethod def object_detection_demo(): """目标检测演示""" import cv2 import numpy as np def detect_objects(image_path): # 加载图像 image = cv2.imread(image_path) # 这里使用OpenCV的Haar级联作为简单示例 # 实际应用中可以使用YOLO、Faster R-CNN等 face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' ) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5 ) # 绘制检测框 for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) return image, faces return detect_objects
class NLPApplications: """自然语言处理应用""" @staticmethod def sentiment_analysis(text): """情感分析""" from textblob import TextBlob blob = TextBlob(text) sentiment = blob.sentiment return { 'polarity': sentiment.polarity, # -1到1,负面到正面 'subjectivity': sentiment.subjectivity # 0到1,客观到主观 } @staticmethod def named_entity_recognition(text): """命名实体识别""" import spacy nlp = spacy.load('en_core_web_sm') doc = nlp(text) entities = [(ent.text, ent.label_) for ent in doc.ents] return entities @staticmethod def text_similarity(text1, text2): """文本相似度""" from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity vectorizer = TfidfVectorizer() tfidf = vectorizer.fit_transform([text1, text2]) similarity = cosine_similarity(tfidf[0:1], tfidf[1:2]) return similarity[0][0]
class AIEthics: """AI伦理考虑""" @staticmethod def check_fairness(model, X, sensitive_attribute): """检查模型公平性""" from sklearn.metrics import accuracy_score, confusion_matrix # 按敏感属性分组 groups = X[sensitive_attribute].unique() results = {} for group in groups: group_mask = X[sensitive_attribute] == group X_group = X[group_mask] if len(X_group) > 0: y_pred = model.predict(X_group) # 这里应该有真实的标签来计算准确率 # results[group] = accuracy_score(y_true[group_mask], y_pred) return results @staticmethod def detect_bias_in_text(text): """检测文本中的偏见""" # 简化的偏见检测 bias_keywords = [ 'always', 'never', 'all', 'every', # 绝对化语言 'obviously', 'clearly', # 假设性语言 'should', 'must', 'ought to' # 规范性语言 ] found_biases = [] for keyword in bias_keywords: if keyword in text.lower(): found_biases.append(keyword) return found_biases
人工智能从早期的符号推理发展到今天的深度学习,已经形成了完整的技术体系。掌握AI基础知识需要:
AI技术正在快速演进,为各行各业带来变革。理解AI的基础原理,将帮助你更好地应用和开发AI系统。