{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1.模型保存与加载\n", "\n", "Q:pytorch中的序列化与反序列化\n", "- `torch.save(obj, f, pickle_module=, pickle_protocol=2, _use_new_zipfile_serialization=True)`\n", "- obj:对象,f:输出路径\n", "- `torch.load(f, map_location=None, pickle_module=, **pickle_load_args)`\n", "- f:文件路径,map_location:指定存放位置,cpu or gpu\n", "\n", "Q:模型保存的两种方式是什么?\n", "- 1.保存整个Module:torch.save(net, path)\n", "- 2.保存模型参数:torch.save(net.state_dict(), path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2.模型finetune\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.GPU的使用\n", "\n", "Q:to函数如何转换数据类型和设备?\n", "- ```python\n", "x = torch.ones((3, 3))\n", "x = x.to(torch.float64)\n", "\n", "x = torch.ones((3, 3))\n", "x = x.to(\"cuda\")\n", "\n", "linear = nn.Linear(2, 2)\n", "linear.to(torch.double)\n", "\n", "gpu1 = torch.device(\"cuda\")\n", "linear.to(gpu1)\n", "```\n", "- 注意:张量不执行inplace,模型执行inplace" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }