Commit 908c6812 by 靓靓

upload files

parent 2ba4b0fa
-- "a/Python\345\237\272\347\241\200\344\273\243\347\240\201\350\203\275\345\212\233\346\217\220\345\215\207\350\257\276\347\250\213/.gitkeep"
++ /dev/null
{
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 程序异常示例"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T02:50:19.239752100Z",
"start_time": "2024-10-30T02:50:18.911490900Z"
}
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[28], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m----> 3\u001b[0m c \u001b[38;5;241m=\u001b[39m \u001b[43ma\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43mb\u001b[49m\n",
"\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"a = 1\n",
"b = 0\n",
"c = a/b"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T02:50:35.247367300Z",
"start_time": "2024-10-30T02:50:35.216259500Z"
}
},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate list (not \"str\") to list",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[29], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m a \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m]\n\u001b[0;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpython\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m----> 3\u001b[0m c \u001b[38;5;241m=\u001b[39m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m \n",
"\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"str\") to list"
]
}
],
"source": [
"a = [1, 2]\n",
"b = 'python'\n",
"c = a + b "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# try-except 捕获不同异常"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T03:08:25.842129400Z",
"start_time": "2024-10-30T03:08:25.831488200Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n",
"2.5\n",
"捕获到除数为0的异常: division by zero\n"
]
}
],
"source": [
"a = [1, 2, 0, 'python']\n",
"b = 5\n",
"try:\n",
" for i in a:\n",
" result = b/i\n",
" print(result)\n",
"except ZeroDivisionError as e:\n",
" print(\"捕获到除数为0的异常:\", e)\n",
"except TypeError as e:\n",
" print(\"捕获到类型错误的异常:\", e)\n",
"\n",
"# Q:为什么没有捕获到 TypeError,程序就结束了?"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T03:06:37.482380400Z",
"start_time": "2024-10-30T03:06:37.469325300Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n",
"2.5\n",
"捕获到除数为0的异常: division by zero\n",
"捕获到类型错误的异常: unsupported operand type(s) for /: 'int' and 'str'\n"
]
}
],
"source": [
"# try-except 程序流程\n",
"\n",
"a = [1, 2, 0, 'python']\n",
"b = 5\n",
"\n",
"for i in a:\n",
" try:\n",
" result = b/i\n",
" print(result)\n",
" except ZeroDivisionError as e:\n",
" print(\"捕获到除数为0的异常:\", e)\n",
" except TypeError as e:\n",
" print(\"捕获到类型错误的异常:\", e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# try-except-else-finally"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T03:11:31.811591100Z",
"start_time": "2024-10-30T03:11:31.789033400Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"进入else\n",
"进入finally\n"
]
}
],
"source": [
"a = 1\n",
"b = 5\n",
"# b = 0\n",
"try:\n",
" c = a/b\n",
"except Exception as e:\n",
" # 处理异常的代码块\n",
" print('进入 except')\n",
"else:\n",
" print('进入else')\n",
"finally:\n",
" print('进入finally')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 自定义异常类"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-30T03:40:10.135463300Z",
"start_time": "2024-10-30T03:40:10.117941100Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"进入 MyCustomException\n",
"变量值是:红色,期望是绿色,请注意!!!\n"
]
}
],
"source": [
"# 自定义异常类\n",
"class MyCustomException(Exception):\n",
" def __init__(self, message):\n",
" super().__init__(message)\n",
"\n",
"try:\n",
" a = '红色'\n",
" if a == '红色':\n",
" raise MyCustomException(f'变量值是:{a},期望是绿色,请注意!!!')\n",
" elif a == '黄色':\n",
" raise MyCustomException(f'变量值是:{a},期望是绿色,请注意!')\n",
" \n",
"except MyCustomException as e:\n",
" print(\"进入 MyCustomException\")\n",
" print(e)\n",
"except Exception as e:\n",
" print(\"进入 Exception\")\n",
" print(e)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n"
]
}
],
"source": [
"print(123)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 程序异常示例"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-25T09:02:07.740662900Z",
"start_time": "2024-11-25T09:02:07.718846800Z"
}
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mZeroDivisionError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[11], line 3\u001B[0m\n\u001B[0;32m 1\u001B[0m a \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m1\u001B[39m\n\u001B[0;32m 2\u001B[0m b \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m0\u001B[39m\n\u001B[1;32m----> 3\u001B[0m c \u001B[38;5;241m=\u001B[39m \u001B[43ma\u001B[49m\u001B[38;5;241;43m/\u001B[39;49m\u001B[43mb\u001B[49m\n",
"\u001B[1;31mZeroDivisionError\u001B[0m: division by zero"
]
}
],
"source": [
"a = 1\n",
"b = 0\n",
"c = a/b"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-25T09:02:08.190160700Z",
"start_time": "2024-11-25T09:02:08.163608200Z"
}
},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate list (not \"str\") to list",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mTypeError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[12], line 3\u001B[0m\n\u001B[0;32m 1\u001B[0m a \u001B[38;5;241m=\u001B[39m [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m]\n\u001B[0;32m 2\u001B[0m b \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpython\u001B[39m\u001B[38;5;124m'\u001B[39m\n\u001B[1;32m----> 3\u001B[0m c \u001B[38;5;241m=\u001B[39m \u001B[43ma\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mb\u001B[49m \n",
"\u001B[1;31mTypeError\u001B[0m: can only concatenate list (not \"str\") to list"
]
}
],
"source": [
"a = [1, 2]\n",
"b = 'python'\n",
"c = a + b "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"source": [
"# 基础语法 try-except-else-finally"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"进入 except\n",
"进入finally\n"
]
}
],
"source": [
"a = 1\n",
"# b = 5\n",
"b = 0\n",
"try:\n",
" c = a/b\n",
"except Exception as e:\n",
" # 处理异常的代码块\n",
" print('进入 except')\n",
"else:\n",
" print('进入else')\n",
"finally:\n",
" print('进入finally')"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-25T09:09:15.199075400Z",
"start_time": "2024-11-25T09:09:15.185041100Z"
}
},
"execution_count": 14
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-25T08:30:35.423637500Z",
"start_time": "2024-11-25T08:30:35.399093600Z"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"source": [
"# try-except 捕获不同异常"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n",
"2.5\n",
"捕获到除数为0的异常: division by zero\n"
]
}
],
"source": [
"a = [1, 2, 0, 'python']\n",
"b = 5\n",
"try:\n",
" for i in a:\n",
" result = b/i\n",
" print(result)\n",
"except ZeroDivisionError as e:\n",
" print(\"捕获到除数为0的异常:\", e)\n",
"except TypeError as e:\n",
" print(\"捕获到类型错误的异常:\", e)\n",
"\n",
"# Q:为什么没有捕获到 TypeError,程序就结束了?"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-25T09:13:28.984438Z",
"start_time": "2024-11-25T09:13:28.964401400Z"
}
},
"execution_count": 15
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-25T08:32:00.215150Z",
"start_time": "2024-11-25T08:32:00.198129800Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n",
"2.5\n",
"捕获到除数为0的异常: division by zero\n",
"捕获到类型错误的异常: unsupported operand type(s) for /: 'int' and 'str'\n"
]
}
],
"source": [
"# try-except 程序流程\n",
"\n",
"a = [1, 2, 0, 'python']\n",
"b = 5\n",
"\n",
"for i in a:\n",
" try:\n",
" result = b/i\n",
" print(result)\n",
" except ZeroDivisionError as e:\n",
" print(\"捕获到除数为0的异常:\", e)\n",
" except TypeError as e:\n",
" print(\"捕获到类型错误的异常:\", e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 自定义异常类"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-25T08:36:30.518114600Z",
"start_time": "2024-11-25T08:36:30.506597200Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"进入 MyCustomException\n",
"变量值是:红色,期望是绿色,请注意!!!\n"
]
}
],
"source": [
"# 自定义异常类\n",
"class MyCustomException(Exception):\n",
" def __init__(self, message: str):\n",
" super().__init__(message)\n",
"\n",
"def raise_something(color_):\n",
" if color_ == '红色':\n",
" raise MyCustomException(f'变量值是:{color_},期望是绿色,请注意!!!')\n",
" elif color_ == '黄色':\n",
" raise MyCustomException(f'变量值是:{color_},期望是绿色,请注意!')\n",
"\n",
"try:\n",
" color = '红色'\n",
" raise_something(color)\n",
"\n",
"except MyCustomException as e:\n",
" print(\"进入 MyCustomException\")\n",
" print(e)\n",
"except Exception as e:\n",
" print(\"进入 Exception\")\n",
" print(e)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# 一、基础类型\n",
"基础类型是最常见的类型提示,包括整型(int)、浮点型(float)、字符串(str)和布尔型(bool)等。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"def add_numbers(a: int, b: int) -> int:\n",
" return a + b\n",
"\n",
"def greet(name: str) -> str:\n",
" return f\"Hello, {name}!\"\n",
"\n",
"def is_active(is_logged_in: bool) -> bool:\n",
" return is_logged_in\n",
"\n",
"def calculate_area(length: float, width: float) -> float:\n",
" return length * width"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:02.587370Z",
"start_time": "2024-10-21T15:01:02.569015300Z"
}
},
"execution_count": 1
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"ab\n"
]
}
],
"source": [
"print(add_numbers(1, 2))\n",
"\n",
"print(add_numbers('a', 'b')) # 类型注解是静态的,主要给人看,解释器不会断变量类型进行判断\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:23.432111100Z",
"start_time": "2024-10-21T15:01:23.426109100Z"
}
},
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# 二、容器类型\n",
"容器类型包括列表(List)、字典(Dict)、集合(Set)和元组(Tuple)。需要从 typing 模块导入。\n",
"typing 模块为python3.5后引入的内置库:https://docs.python.org/3/library/typing.html\n",
"\n",
"注意:\n",
"Tuple: 需要将每一个元素都标记出来\n",
"Dict: 第一个代表key,第二个代表value"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from typing import List, Dict, Set, Tuple\n",
"\n",
"def get_items() -> List[str]:\n",
" # 思考:List中元素有多个类型时,如何注解?\n",
" return [\"item1\", \"item2\"]\n",
"\n",
"def parse_data(data: Dict[str, int]) -> int:\n",
" \n",
" print(data.values())\n",
" total = sum(data.values())\n",
" return total\n",
"\n",
"def unique_elements(elements: List[int]) -> Set[int]:\n",
" return set(elements)\n",
"\n",
"def point(p: Tuple[int, int]) -> Tuple[int, int]:\n",
" return p"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:25.147708700Z",
"start_time": "2024-10-21T15:01:25.119136800Z"
}
},
"execution_count": 3
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_values([15000, 20000, 11000])\n",
"46000\n",
"dict_values([1000, 1000, 1000])\n",
"3000\n"
]
}
],
"source": [
"dict_str = {'北京': 15000, '上海': 20000, '广州': 11000}\n",
"print(parse_data(dict_str))\n",
"\n",
"dict_int = {0: 1000, 1: 1000, 2: 1000} # 错误的key类型,代码仍旧正常\n",
"print(parse_data(dict_int))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:25.280438400Z",
"start_time": "2024-10-21T15:01:25.266119400Z"
}
},
"execution_count": 4
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, '2')\n",
"(1, '2')\n"
]
}
],
"source": [
"tuple_str = (1, '2')\n",
"\n",
"print(point((1, '2')))\n",
"print(point(tuple_str)) # 赋值给变量时,IDE未进行提示"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:25.411784200Z",
"start_time": "2024-10-21T15:01:25.392749200Z"
}
},
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"# 三、None 类型\n",
"当一个函数可能不会返回任何值时, 用 None 指示"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"def log(message: str) -> None:\n",
" print(message)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:25.655149900Z",
"start_time": "2024-10-21T15:01:25.642764Z"
}
},
"execution_count": 6
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, world!\n",
"None\n"
]
}
],
"source": [
"result = log('Hello, world!')\n",
"print(result)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:25.800368400Z",
"start_time": "2024-10-21T15:01:25.774801600Z"
}
},
"execution_count": 7
},
{
"cell_type": "markdown",
"source": [
"# 四、Any 类型\n",
"无法确定或不想指定一个类型时用 Any"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from typing import Any\n",
"\n",
"def process_data(data: Any) -> Any:\n",
" return data"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:26.027786800Z",
"start_time": "2024-10-21T15:01:26.022781Z"
}
},
"execution_count": 8
},
{
"cell_type": "markdown",
"source": [
"# 五、Union 类型\n",
"接受多种类型时,使用 Union。 类似枚举的概念。\n",
"例如一个List中的元素有int, 有str,可以 List[Union[str, int]]"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from typing import Union\n",
"\n",
"def describe_value(value: Union[int, str, None]) -> str:\n",
" if value is None:\n",
" return \"Value is None\"\n",
" elif isinstance(value, str):\n",
" return f\"Value is a string: {value}\"\n",
" else:\n",
" return f\"Value is an integer: {value}\""
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:26.309758Z",
"start_time": "2024-10-21T15:01:26.296717400Z"
}
},
"execution_count": 9
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value is an integer: 1234\n",
"Value is a string: 1234\n",
"Value is an integer: [1, 2, 3]\n"
]
}
],
"source": [
"print(describe_value(1234))\n",
"print(describe_value('1234'))\n",
"print(describe_value([1, 2, 3])) # 不是 int, str, None的任意一种,因此IDE 进行提示"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:26.472955100Z",
"start_time": "2024-10-21T15:01:26.449405800Z"
}
},
"execution_count": 10
},
{
"cell_type": "markdown",
"source": [
"# 六、开源项目赏析\n",
"\n",
"https://github.com/huggingface/transformers/blob/main/src/transformers/models/auto/tokenization_auto.py#L586"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"import os\n",
"from typing import Optional\n",
"\n",
"def get_tokenizer_config(\n",
" pretrained_model_name_or_path: Union[str, os.PathLike], # 二选一\n",
" cache_dir: Optional[Union[str, os.PathLike]] = None, # 可选,并且是str或者是os.PathLike\n",
" force_download: bool = False,\n",
" resume_download: bool = False,\n",
" proxies: Optional[Dict[str, str]] = None, # 可选, 一个字典,key和value都是str\n",
" use_auth_token: Optional[Union[bool, str]] = None, # 可选, bool或者str\n",
" revision: Optional[str] = None, # 可选, str\n",
" local_files_only: bool = False,\n",
" **kwargs, # 可变关键字变量\n",
"):\n",
" pass"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-21T15:01:27.031965400Z",
"start_time": "2024-10-21T15:01:27.015735900Z"
}
},
"execution_count": 11
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# 1. 全局作用域 (Global Scope)\n",
"指在函数外部定义的变量,它们可以在整个模块中被访问"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"x = 20 # x 是全局变量\n",
"\n",
"def func():\n",
" print(x) # 由于x是全局变量,虽然在func内部没有定义,仍旧可以访问\n",
"\n",
"func()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:31:40.724129200Z",
"start_time": "2024-10-28T14:31:40.715131300Z"
}
},
"execution_count": 1
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"# 2. 局部作用域 (Local Scope)\n",
"在函数内部定义的变量,只能在函数内部访问,并且当函数调用结束时,它们就会被销毁\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
},
{
"ename": "NameError",
"evalue": "name 'y' is not defined",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mNameError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[2], line 6\u001B[0m\n\u001B[0;32m 3\u001B[0m \u001B[38;5;28mprint\u001B[39m(y)\n\u001B[0;32m 5\u001B[0m func() \u001B[38;5;66;03m# 输出: 10\u001B[39;00m\n\u001B[1;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43my\u001B[49m) \u001B[38;5;66;03m# 抛出 NameError,因为 y 没有定义,在func中定义,但是func调用结束后销毁了局部变量y\u001B[39;00m\n",
"\u001B[1;31mNameError\u001B[0m: name 'y' is not defined"
]
}
],
"source": [
"def func():\n",
" y = 10 # y 是局部变量, 在func函数内部有效\n",
" print(y)\n",
"\n",
"func() # 输出: 10\n",
"print(y) # 抛出 NameError,因为 y 没有定义,在func中定义,但是func调用结束后销毁了局部变量y"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:32:02.354422800Z",
"start_time": "2024-10-28T14:32:02.034066400Z"
}
},
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# 3. 封闭作用域 (Enclosing Scope)\n",
"变量在外层函数但不是全局变量,通常发生在嵌套函数中\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30\n"
]
}
],
"source": [
"def outer():\n",
" z = 30 \n",
" \n",
" def inner():\n",
" print(z) # 访问外层函数中的变量,但又不是全局变量,因此z的作用于是一个封闭作用域\n",
" \n",
" inner()\n",
"\n",
"outer()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:32:26.283433500Z",
"start_time": "2024-10-28T14:32:26.264910300Z"
}
},
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"# global\n",
" 如果需要从函数内部修改全局变量,可以使用 global 关键字来声明这个变量是全局的 \n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50 40\n"
]
}
],
"source": [
"x, y = 40, 40\n",
"\n",
"def func():\n",
" global x # 声明 x 是全局变量\n",
" x = 50 # 修改全局变量 x\n",
" y = 50\n",
" \n",
"func()\n",
"print(x, y) # 输出: 50"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:32:34.489791200Z",
"start_time": "2024-10-28T14:32:34.478471800Z"
}
},
"execution_count": 4
},
{
"cell_type": "markdown",
"source": [
"# nonlocal\n",
"内层函数中修改外层函数的变量,需要用nonlocal声明"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"70\n"
]
}
],
"source": [
"def outer():\n",
" x = 60\n",
" \n",
" def inner():\n",
" nonlocal x # 声明 x 是外层函数中的变量,但不是全局\n",
" x = 70 # 修改的 x 已经是outer函数中的变量\n",
" \n",
" inner()\n",
" print(x) # x已经在inner函数中被修改了\n",
"\n",
"outer()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:32:43.727258400Z",
"start_time": "2024-10-28T14:32:43.712259200Z"
}
},
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"# 综合案例"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After local assignment: test spam\n",
"After nonlocal assignment: nonlocal spam\n",
"After global assignment: nonlocal spam\n",
"In global scope: global spam\n"
]
}
],
"source": [
"def scope_test():\n",
" def do_local():\n",
" spam = \"local spam\"\n",
"\n",
" def do_nonlocal():\n",
" nonlocal spam\n",
" spam = \"nonlocal spam\"\n",
"\n",
" def do_global():\n",
" global spam\n",
" spam = \"global spam\"\n",
"\n",
" spam = \"test spam\"\n",
" do_local()\n",
" print(\"After local assignment:\", spam) # 采用的是第13行定义的局部变量\n",
" do_nonlocal()\n",
" print(\"After nonlocal assignment:\", spam) # 在do_nonlocal()中,已经被修改为 \"nonlocal spam\"\n",
" do_global()\n",
" print(\"After global assignment:\", spam) # do_global()中并没有修改局部变量spam,因此还是 \"nonlocal spam\"\n",
"\n",
"scope_test()\n",
"print(\"In global scope:\", spam) # 此处spam是全局变量,全局变量在 do_global()中被修改为 \"global spam\""
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-28T14:32:45.042736200Z",
"start_time": "2024-10-28T14:32:45.026736700Z"
}
},
"execution_count": 6
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# 装饰器\n",
"“装饰器是一个函数,接收函数,返回函数”\n",
"装饰器是一个可被调用对象,接收可被调用对象,返回可被调用对象"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example_function took 0.3155 seconds to run.\n"
]
}
],
"source": [
"import time\n",
"\n",
"def timer_decorator(func):\n",
" def wrapper(*args, **kwargs):\n",
" start_time = time.time()\n",
" result = func(*args, **kwargs)\n",
" end_time = time.time()\n",
" print(f\"{func.__name__} took {end_time - start_time:.4f} seconds to run.\")\n",
" return result\n",
" return wrapper\n",
"\n",
"@timer_decorator\n",
"def example_function():\n",
" counter = 0\n",
" for i in range(1000_0000):\n",
" counter += 1\n",
"\n",
"example_function() # 函数被“装饰”了,得到额外的功能(打印函数执行的时间)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-30T13:26:17.443048600Z",
"start_time": "2024-10-30T13:26:17.106520500Z"
}
},
"execution_count": 1
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"example_function2 took 0.3099 seconds to run.\n"
]
}
],
"source": [
"# 此时example_function它不再是example_function,而是wrapper,有一个更通俗易懂的等价形式:\n",
"def example_function2():\n",
" counter = 0\n",
" for i in range(1000_0000):\n",
" counter += 1\n",
" \n",
"example_function2 = timer_decorator(example_function2) # 装饰器是一个函数,接受函数,返回函数。\n",
"example_function2()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-09-24T07:23:49.214435700Z",
"start_time": "2024-09-24T07:23:48.890520500Z"
}
},
"execution_count": 26
},
{
"cell_type": "markdown",
"source": [
"# 闭包\n",
"闭包指某种特殊的函数,一个内部函数引用了外部函数的局部变量,并且外部函数返回了这个内部函数,此时这个内部函数就是一个闭包"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi\n",
"Hello\n"
]
}
],
"source": [
"def outer_function(text):\n",
" def inner_function():\n",
" print(text) # 使用了外部函数的局部变量\n",
" return inner_function # 返回了内部函数\n",
"\n",
"hi_func = outer_function('Hi') # 创建闭包\n",
"hello_func = outer_function('Hello') # 创建另一个闭包\n",
"\n",
"hi_func() # 输出: Hi\n",
"hello_func() # 输出: Hello\n",
"\n",
"### 形式是否非常熟悉?装饰器中的内部函数也是一个闭包\n",
"### 形式是否非常熟悉?装饰器中的内部函数也是一个闭包\n",
"### 形式是否非常熟悉?装饰器中的内部函数也是一个闭包"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-09-24T03:36:29.418740900Z",
"start_time": "2024-09-24T03:36:29.412201900Z"
}
},
"execution_count": 13
},
{
"cell_type": "markdown",
"source": [
"## 装饰器内部使用局部变量"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"call foo func, total call count:1\n",
"call foo func, total call count:2\n",
"call foo func, total call count:3\n"
]
}
],
"source": [
"# 装饰器功能:统计被装饰函数的调用次数\n",
"def outer_func(func):\n",
" call_count = 0\n",
" def inner_func(*arg, **kwargs):\n",
" # 内部函数需要修改外部变量时,若不是全局变量则需要定义为nonlocal\n",
" nonlocal call_count # 使用外部函数的局部变量,该变量常驻内存中,不会释放,可用于全局的统计\n",
" call_count += 1\n",
" print(f'call {func.__name__} func, total call count:{call_count }')\n",
" func(*arg, **kwargs)\n",
" return inner_func\n",
"\n",
"@outer_func\n",
"def foo():\n",
" pass\n",
"\n",
"for i in range(3):\n",
" foo()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-30T13:37:36.888845Z",
"start_time": "2024-10-30T13:37:36.877309800Z"
}
},
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# 有参数装饰器\n",
"有参数装饰器,可用于定制化地控制装饰器的行为,例如限制函数总共被调用的次数"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Function executed.\n",
"Function executed.\n",
"Function executed.\n",
"Function example_function_with_limit has reached its maximum call limit of 3.\n",
"Function example_function_with_limit has reached its maximum call limit of 3.\n"
]
}
],
"source": [
"def limit_calls(max_calls):\n",
" def decorator(func):\n",
" call_count = 0 # 对于闭包(wrapper)而言的外部函数的局部变量\n",
" def wrapper(*args, **kwargs):\n",
" nonlocal call_count\n",
" if call_count < max_calls: # 数据1:由limit_calls传入\n",
" call_count += 1\n",
" return func(*args, **kwargs) # 数据2;由decorator传入; 数据3:由wrapper传入。\n",
" else:\n",
" print(f\"Function {func.__name__} has reached its maximum call limit of {max_calls}.\")\n",
" return wrapper\n",
" return decorator\n",
"\n",
"@limit_calls(3) # 设置最大调用次数为3次\n",
"def example_function_with_limit():\n",
" time.sleep(0.1)\n",
" print(\"Function executed.\")\n",
"\n",
"for i in range(5):\n",
" example_function_with_limit() # 函数最多会被成功调用3次"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-09-24T08:12:56.479640800Z",
"start_time": "2024-09-24T08:12:56.153315900Z"
}
},
"execution_count": 29
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-09-24T08:12:58.013559100Z",
"start_time": "2024-09-24T08:12:58.001529300Z"
}
},
"execution_count": 29
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# 1. 迭代器\n",
"一个对象实现了 \\_\\_iter_\\_() 和 \\_\\_next\\_\\_() 方法,则是第一个迭代器,可被for循环使用。"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [],
"source": [
"class MyIterator:\n",
" def __init__(self, max_value):\n",
" self.max_value = max_value\n",
" self.current = 0\n",
"\n",
" def __iter__(self):\n",
" return self\n",
"\n",
" def __next__(self):\n",
" if self.current < self.max_value:\n",
" value = self.current\n",
" self.current += 1\n",
" return value\n",
" else:\n",
" raise StopIteration"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-07T09:49:11.940483400Z",
"start_time": "2024-11-07T09:49:11.934448800Z"
}
},
"execution_count": 1
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"\n",
"# 使用迭代器\n",
"for i in MyIterator(5):\n",
" print(i)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-05T03:06:15.004966700Z",
"start_time": "2024-11-05T03:06:14.974890700Z"
}
},
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"# 2. 内置函数 iter(), next()"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
},
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mStopIteration\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[7], line 7\u001B[0m\n\u001B[0;32m 5\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mnext\u001B[39m(my_iterator)) \u001B[38;5;66;03m# 输出 2\u001B[39;00m\n\u001B[0;32m 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mnext\u001B[39m(my_iterator)) \u001B[38;5;66;03m# 输出 3\u001B[39;00m\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28;43mnext\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mmy_iterator\u001B[49m\u001B[43m)\u001B[49m) \u001B[38;5;66;03m# 抛出 StopIteration 异常\u001B[39;00m\n",
"\u001B[1;31mStopIteration\u001B[0m: "
]
}
],
"source": [
"my_list = [1, 2, 3]\n",
"my_iterator = iter(my_list)\n",
"\n",
"print(next(my_iterator)) # 输出 1\n",
"print(next(my_iterator)) # 输出 2\n",
"print(next(my_iterator)) # 输出 3\n",
"print(next(my_iterator)) # 抛出 StopIteration 异常"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-05T03:10:13.185253600Z",
"start_time": "2024-11-05T03:10:13.163694700Z"
}
},
"execution_count": 7
},
{
"cell_type": "markdown",
"source": [
"# 3. 生成器(函数)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"yield之前...\n",
"1\n",
"yield之后...\n",
"yield之前...\n",
"2\n",
"yield之后...\n",
"yield之前...\n",
"3\n",
"yield之后...\n"
]
}
],
"source": [
"def count_up_to(n):\n",
" count = 1\n",
" while count <= n:\n",
" print('yield之前...')\n",
" yield count\n",
" print('yield之后...')\n",
" count += 1\n",
"\n",
"# 使用生成器函数\n",
"for number in count_up_to(3):\n",
" print(number)\n",
" \n",
" "
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-05T03:16:20.859753500Z",
"start_time": "2024-11-05T03:16:20.838532Z"
}
},
"execution_count": 9
},
{
"cell_type": "markdown",
"source": [
"采用生成器与列表对象的内存效率对比"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using generator:\n",
"Time taken: 4.08 seconds\n",
"Memory used: 115.10 MB\n",
"\n",
"Not using generator:\n",
"Time taken: 1.40 seconds\n",
"Memory used: 187.52 MB\n"
]
}
],
"source": [
"import time\n",
"from memory_profiler import memory_usage\n",
"\n",
"# 生成大量数据\n",
"data_size = 1000000\n",
"data = list(range(data_size))\n",
"\n",
"# 生成器函数\n",
"def process_data_with_generator(data):\n",
" for item in data:\n",
" yield item * 2\n",
"\n",
"# 普通函数\n",
"def process_data_without_generator(data):\n",
" result = []\n",
" for item in data:\n",
" result.append(item * 2)\n",
" return result\n",
"\n",
"# 测量生成器处理数据的耗时和内存使用\n",
"def measure_generator_performance(data):\n",
" start_time = time.time()\n",
" mem_usage = memory_usage((process_data_with_generator, (data,)), interval=0.1)\n",
" end_time = time.time()\n",
" return end_time - start_time, max(mem_usage)\n",
"\n",
"# 测量普通函数处理数据的耗时和内存使用\n",
"def measure_non_generator_performance(data):\n",
" start_time = time.time()\n",
" mem_usage = memory_usage((process_data_without_generator, (data,)), interval=0.1)\n",
" end_time = time.time()\n",
" return end_time - start_time, max(mem_usage)\n",
"\n",
"# 运行实验\n",
"gen_time, gen_memory = measure_generator_performance(data)\n",
"non_gen_time, non_gen_memory = measure_non_generator_performance(data)\n",
"\n",
"# 输出结果\n",
"print(f\"Using generator:\")\n",
"print(f\"Time taken: {gen_time:.2f} seconds\")\n",
"print(f\"Memory used: {gen_memory:.2f} MB\")\n",
"\n",
"print(f\"\\nNot using generator:\")\n",
"print(f\"Time taken: {non_gen_time:.2f} seconds\")\n",
"print(f\"Memory used: {non_gen_memory:.2f} MB\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-05T03:44:20.511125700Z",
"start_time": "2024-11-05T03:44:14.993327100Z"
}
},
"execution_count": 48
},
{
"cell_type": "markdown",
"source": [
"# 4. 生成器(表达式)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"4\n",
"9\n",
"16\n",
"25\n",
"36\n",
"49\n",
"64\n",
"81\n"
]
}
],
"source": [
"# 生成器表达式\n",
"squares = (x**2 for x in range(10))\n",
"\n",
"# 使用生成器表达式\n",
"for square in squares:\n",
" print(square)\n",
"\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-11-05T03:26:37.412921700Z",
"start_time": "2024-11-05T03:26:37.177932900Z"
}
},
"execution_count": 46
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
import random
import random
def perform_division(a, b):
c = a/b
return c
for _ in range(10000000):
num_random = random.randint(-100, 100)
# print(f'变量num_random是:{num_random}')
# results = 100/num_random
# print(results)
# ==================================== 修改num_random变量 =====================================
for _ in range(10000000):
num_random = random.randint(-100, 100)
try:
results = perform_division(100, num_random)
except Exception as e:
print(num_random) # 此处打断点,可获取出现bug时,具体数据情况
print(results)
# data_loader.py
# data_loader.py
import torch
import numpy as np
class RandomDataset(torch.utils.data.Dataset):
def __init__(self, size, length):
self.size = size
self.length = length
self.data = np.random.rand(length, 3, 32, 32).astype(np.float32)
self.labels = np.random.randint(0, 10, size=length)
def __len__(self):
return self.length
def __getitem__(self, idx):
# 引入错误:返回错误的标签类型
return self.data[idx], float(self.labels[idx])
# return self.data[idx], int(self.labels[idx])
def get_data_loaders(batch_size=64, dataset_size=1000):
train_dataset = RandomDataset(32, dataset_size)
test_dataset = RandomDataset(32, int(dataset_size * 0.2))
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
return train_loader, test_loader
\ No newline at end of file
# main.py
# main.py
import torch
import torch.optim as optim
import torch.nn.functional as F
from model import SimpleCNN
from data_loader import get_data_loaders
from myutils import train, test
def main():
# 设备配置
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 加载数据
train_loader, test_loader = get_data_loaders()
# 初始化模型
model = SimpleCNN().to(device)
# 定义损失函数和优化器
criterion = F.cross_entropy
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练和测试
num_epochs = 100
for epoch in range(1, num_epochs + 1):
train(model, device, train_loader, optimizer, criterion, epoch)
test(model, device, test_loader, criterion)
if __name__ == "__main__":
main()
# model.py
# model.py
import torch
import torch.nn as nn
import torch.nn.functional as F
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
self.fc1 = nn.Linear(32 * 8 * 8, 64)
self.fc2 = nn.Linear(128, 10) # 128
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 32 * 8 * 8)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
\ No newline at end of file
# utils.py
# utils.py
import torch
def train(model, device, train_loader, optimizer, criterion, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
# loss = criterion(output, target.long())
loss.backward()
optimizer.step()
if batch_idx % 100 == 0:
print(f'Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)} ({100. * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}')
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
# data, target = data.to(device), target.to(device)
data, target = data.to(device), target.to(device).long()
output = model(data)
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print(f'\nTest set: Average loss: {test_loss:.4f}, Accuracy: {correct}/{len(test_loader.dataset)} ({100. * correct / len(test_loader.dataset):.0f}%)\n')
\ No newline at end of file
import random
import random
def perform_division(a, b):
c = a/b
return c
for _ in range(10000000):
num_random = random.randint(-100, 100)
# print(f'变量num_random是:{num_random}')
# results = 100/num_random
# print(results)
# ==================================== 修改num_random变量 =====================================
for _ in range(10000000):
num_random = random.randint(-100, 100)
try:
results = perform_division(100, num_random)
except Exception as e:
print(num_random) # 此处打断点,可获取出现bug时,具体数据情况
print(results)
# data_loader.py
# data_loader.py
import torch
import numpy as np
class RandomDataset(torch.utils.data.Dataset):
def __init__(self, size, length):
self.size = size
self.length = length
self.data = np.random.rand(length, 3, 32, 32).astype(np.float32)
self.labels = np.random.randint(0, 10, size=length)
def __len__(self):
return self.length
def __getitem__(self, idx):
# 引入错误:返回错误的标签类型
return self.data[idx], float(self.labels[idx])
# return self.data[idx], int(self.labels[idx])
def get_data_loaders(batch_size=64, dataset_size=1000):
train_dataset = RandomDataset(32, dataset_size)
test_dataset = RandomDataset(32, int(dataset_size * 0.2))
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
return train_loader, test_loader
\ No newline at end of file
# main.py
# main.py
import torch
import torch.optim as optim
import torch.nn.functional as F
from model import SimpleCNN
from data_loader import get_data_loaders
from myutils import train, test
def main():
# 设备配置
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 加载数据
train_loader, test_loader = get_data_loaders()
# 初始化模型
model = SimpleCNN().to(device)
# 定义损失函数和优化器
criterion = F.cross_entropy
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练和测试
num_epochs = 100
for epoch in range(1, num_epochs + 1):
train(model, device, train_loader, optimizer, criterion, epoch)
test(model, device, test_loader, criterion)
if __name__ == "__main__":
main()
# model.py
# model.py
import torch
import torch.nn as nn
import torch.nn.functional as F
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
self.fc1 = nn.Linear(32 * 8 * 8, 64)
self.fc2 = nn.Linear(128, 10) # 128
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 32 * 8 * 8)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
\ No newline at end of file
# utils.py
# utils.py
import torch
def train(model, device, train_loader, optimizer, criterion, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
# loss = criterion(output, target.long())
loss.backward()
optimizer.step()
if batch_idx % 100 == 0:
print(f'Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)} ({100. * batch_idx / len(train_loader):.0f}%)]\tLoss: {loss.item():.6f}')
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
# data, target = data.to(device), target.to(device)
data, target = data.to(device), target.to(device).long()
output = model(data)
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print(f'\nTest set: Average loss: {test_loss:.4f}, Accuracy: {correct}/{len(test_loader.dataset)} ({100. * correct / len(test_loader.dataset):.0f}%)\n')
\ No newline at end of file
# 视频资料链接:
# 视频资料链接:
ultralytics项目所用视频文件
链接: https://pan.baidu.com/s/1iaYuguOFJsS6HeNk4N8ebw?pwd=z3c4
提取码: z3c4
# 课程资料链接:
# 课程资料链接:
ultralytics_241225.zip
链接: https://pan.baidu.com/s/1xf5HJKo8FgM0SzoPv12bKA?pwd=yi6y
提取码: yi6y
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment