Files
cs224n_2019/[finished]Assignment_2_word2vec/others/pytorch review1.ipynb
chongjiu.jin 90ef82b456 a2
2019-11-11 14:55:15 +08:00

462 lines
8.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 为什么要学习pytorch\n",
"\n",
"tensorflow的学习曲线陡峭\n",
"\n",
"pytorch出自facebook\n",
"\n",
"PyTorch 可以当做 NumPy 用\n",
"\n",
"静态图 vs 动态图\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"最新版本全部使用tensor就可以\n",
"\n",
"numpy能实现的东西在pytorch基本可以实现\n",
"\n",
"torch.randn()=numpy.random.randn()\n",
"\n",
"torch.max()=np.max()\n",
"\n",
"torch.zeros()=np.zeros()\n",
"\n",
"\n",
"如果不能实现,Tensor与numpy之间的可以相互转化\n",
"\n",
"tensor.numpy()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.1.0\n"
]
}
],
"source": [
"import numpy as np\n",
"import torch\n",
"import torch.nn as nn\n",
"print(torch.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"device(type='cpu')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"device"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([0., 1., 2., 3., 4., 5., 6.])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# python struct to torch tensor\n",
"temp=[0, 1, 2, 3, 4, 5, 6]\n",
"x = torch.tensor(temp, dtype=torch.float, device=device)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#gpu/cpu\n",
"x = torch.tensor(temp).cuda()\n",
"x = x.cpu()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x=x.long()\n",
"x=x.float()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## numpy reshape squeeze expand_dims"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 4 5 6 7 8 9]\n",
"(10,)\n"
]
}
],
"source": [
"a = np.arange(10)\n",
"print(a)\n",
"print(a.shape)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0]\n",
" [1]]\n",
"\n",
" [[2]\n",
" [3]]\n",
"\n",
" [[4]\n",
" [5]]\n",
"\n",
" [[6]\n",
" [7]]\n",
"\n",
" [[8]\n",
" [9]]]\n",
"(5, 2, 1)\n"
]
}
],
"source": [
"#a=a.reshape(1,-1)\n",
"#a.reshape(1,10)\n",
"a=a.reshape(5,2,1)\n",
"print(a)\n",
"print(a.shape)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0 1]\n",
" [2 3]\n",
" [4 5]\n",
" [6 7]\n",
" [8 9]]\n",
"(5, 2)\n"
]
}
],
"source": [
"b = np.squeeze(a)\n",
"print(b)\n",
"print(b.shape)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5, 1, 2)\n"
]
}
],
"source": [
"a = np.arange(10)\n",
"a=a.reshape(5,1,2)\n",
"print(a.shape)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0 1]\n",
" [2 3]\n",
" [4 5]\n",
" [6 7]\n",
" [8 9]]\n",
"(5, 2)\n"
]
}
],
"source": [
"b = np.squeeze(a)#b=a.reshape(5,2)\n",
"print(b)\n",
"print(b.shape)\n",
"#np.squeeze(e,axis = 0,1,2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a (10,)\n",
"b axis=0 (1, 10)\n",
"c axis=1 (10, 1)\n"
]
}
],
"source": [
"#expand_dims\n",
"a = np.arange(10)\n",
"print(\"a\",a.shape)\n",
"b = np.expand_dims(a, axis=0)\n",
"print(\"b axis=0\",b.shape)\n",
"c = np.expand_dims(a, axis=1)\n",
"print(\"c axis=1\",c.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## pytorch reshape squeeze unsqueeze"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"x torch.Size([10])\n",
"b axis=0 torch.Size([1, 10])\n",
"c axis=1 torch.Size([10, 1])\n"
]
}
],
"source": [
"x = torch.arange(0,10)\n",
"print(\"x\",x)\n",
"print(\"x\",x.shape)\n",
"b = x.unsqueeze(0)\n",
"print(\"b axis=0\",b.shape)\n",
"c = torch.unsqueeze(x,1)\n",
"print(\"c axis=1\",c.shape)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"d torch.Size([10])\n",
"d torch.Size([10])\n"
]
}
],
"source": [
"d=c.squeeze(1)\n",
"print(\"d\",d.shape)\n",
"d=c.squeeze()\n",
"print(\"d\",d.shape)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"d torch.Size([10])\n"
]
}
],
"source": [
"d=b.squeeze()\n",
"print(\"d\",d.shape)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([8])\n",
"torch.Size([8])\n",
"tensor(0.4091, grad_fn=<BinaryCrossEntropyWithLogitsBackward>)\n"
]
}
],
"source": [
"import torch.nn.functional as F\n",
"m = nn.Sigmoid()\n",
"\n",
"loss = nn.BCEWithLogitsLoss()\n",
"temp=[0, 1, 2,0, 1, 2,0, 1]\n",
"input =torch.tensor(temp, dtype=torch.float, requires_grad=True)\n",
"#input = torch.randn(3, requires_grad=True)\n",
"target = torch.tensor([0,1,1,0,1,1,0,1], dtype=torch.float)\n",
"lossinput = input\n",
"output = loss(lossinput, target)\n",
"\n",
"\n",
"print(lossinput.shape)\n",
"\n",
"print(target.shape)\n",
"\n",
"print(output)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## logist regression\n",
"\n",
"$$y(z) = \\frac{1}{1 + exp(-\\theta^T z)}$$\n",
"\n",
"nn.BCELoss\n",
"$$\n",
" \\ell(x, y) = L = \\{l_1,\\dots,l_N\\}^\\top, \\quad\n",
" l_n = - w_n \\left[ y_n \\cdot \\log x_n + (1 - y_n) \\cdot \\log (1 - x_n) \\right],\n",
" $$ \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch.nn.functional as F # 激励函数库\n",
"\n",
"feature_num=100\n",
"\n",
"class LogistRegression(torch.nn.Module):\n",
" def __init__(self):\n",
" super(LogistRegression, self).__init__()\n",
" self.linear = torch.nn.Linear(feature_num, 1)\n",
" def forward(self, x):\n",
" z=self.linear(x)\n",
" y_pred = F.sigmoid(z)\n",
" return y_pred\n",
" def loss(y_pred,label):#Binary Cross Entropy\n",
" criterion = torch.nn.BCELoss(size_average=True)\n",
" return criterion(y_pred,label)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"nn.CrossEntropyLoss() 自带sigmod不需要加"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}