Skip to content

Commit 6659a09

Browse files
committed
new
1 parent d404dce commit 6659a09

17 files changed

+7362
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from torch.utils.data import Dataset, DataLoader\n",
10+
"import pandas as pd"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 21,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"class file(Dataset):\n",
20+
" def __init__(self,files):\n",
21+
" super(file,self).__init__()\n",
22+
" with open(files) as f: \n",
23+
" self.file=f.readlines()\n",
24+
" def __len__(self):\n",
25+
" return len(self.file)\n",
26+
" def __getitem__(self,idx):\n",
27+
" return self.file[idx]"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 22,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"f=file('backward.py')"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 23,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"['# -*- coding: utf-8 -*-\\n',\n",
48+
" '\"\"\"\\n',\n",
49+
" 'Created on Sun Apr 29 16:23:43 2018\\n',\n",
50+
" '\\n',\n",
51+
" '@author: omf\\n',\n",
52+
" '\"\"\"\\n',\n",
53+
" 'import torch as t\\n',\n",
54+
" 'from torch.autograd import Variable as v\\n',\n",
55+
" '# compute jacobian matrix\\n',\n",
56+
" 'x = t.FloatTensor([2, 1]).view(1, 2)\\n',\n",
57+
" 'x = v(x, requires_grad=True)\\n',\n",
58+
" 'y = v(t.FloatTensor([[1, 2], [3, 4]]))t\\n',\n",
59+
" '\\n',\n",
60+
" 'z = t.mm(x, y)\\n',\n",
61+
" 'jacobian = t.zeros((2, 2))\\n',\n",
62+
" 'z.backward(t.FloatTensor([[1, 0]]), retain_graph=True) # dz1/dx1, dz1/dx2\\n',\n",
63+
" 'jacobian[:, 0] = x.grad.data\\n',\n",
64+
" 'x.grad.data.zero_()\\n',\n",
65+
" 'z.backward(t.FloatTensor([[0, 1]])) # dz2/dx1, dz2/dx2\\n',\n",
66+
" 'jacobian[:, 1] = x.grad.data\\n',\n",
67+
" \"print('=========jacobian========')\\n\",\n",
68+
" \"print('x')\\n\",\n",
69+
" 'print(x.data)\\n',\n",
70+
" \"print('y')\\n\",\n",
71+
" 'print(y.data)\\n',\n",
72+
" \"print('compute result')\\n\",\n",
73+
" 'print(z.data)\\n',\n",
74+
" \"print('jacobian matrix is')\\n\",\n",
75+
" 'print(jacobian)']"
76+
]
77+
},
78+
"execution_count": 23,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"f.file"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 30,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"'from torch.autograd import Variable as v\\n'"
96+
]
97+
},
98+
"execution_count": 30,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"f[7]"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 31,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"dataloader = DataLoader(f, batch_size=4)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 36,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"4\n",
126+
"4\n",
127+
"4\n",
128+
"4\n",
129+
"4\n",
130+
"4\n",
131+
"4\n",
132+
"1\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"for i,j in enumerate(dataloader):\n",
138+
" print(len(j))"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": []
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"metadata": {},
152+
"outputs": [],
153+
"source": []
154+
}
155+
],
156+
"metadata": {
157+
"kernelspec": {
158+
"display_name": "Python 3",
159+
"language": "python",
160+
"name": "python3"
161+
},
162+
"language_info": {
163+
"codemirror_mode": {
164+
"name": "ipython",
165+
"version": 3
166+
},
167+
"file_extension": ".py",
168+
"mimetype": "text/x-python",
169+
"name": "python",
170+
"nbconvert_exporter": "python",
171+
"pygments_lexer": "ipython3",
172+
"version": "3.6.4"
173+
}
174+
},
175+
"nbformat": 4,
176+
"nbformat_minor": 2
177+
}

0 commit comments

Comments
 (0)