Skip to content

Commit 917d67f

Browse files
committed
Init Commit
0 parents  commit 917d67f

File tree

3 files changed

+726
-0
lines changed

3 files changed

+726
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.ipynb_checkpoints/

卷积.ipynb

+345
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import tensorflow as tf\n",
10+
"import numpy as np"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"data": {
20+
"text/plain": [
21+
"'1.9.0'"
22+
]
23+
},
24+
"execution_count": 2,
25+
"metadata": {},
26+
"output_type": "execute_result"
27+
}
28+
],
29+
"source": [
30+
"tf.__version__"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"### 核,输入"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"kernel = np.arange(1, 10).reshape((3, 3))"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 4,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/plain": [
57+
"array([[1, 2, 3],\n",
58+
" [4, 5, 6],\n",
59+
" [7, 8, 9]])"
60+
]
61+
},
62+
"execution_count": 4,
63+
"metadata": {},
64+
"output_type": "execute_result"
65+
}
66+
],
67+
"source": [
68+
"kernel"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 5,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"C = np.array([[1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, 0, 0, 0, 0],\n",
78+
" [0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, 0, 0, 0],\n",
79+
" [0, 0, 0, 0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0],\n",
80+
" [0, 0, 0, 0, 0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]])"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 6,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"input_ = np.arange(1, 17).reshape((4, 4))"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 7,
95+
"metadata": {
96+
"scrolled": true
97+
},
98+
"outputs": [
99+
{
100+
"data": {
101+
"text/plain": [
102+
"array([[ 1, 2, 3, 4],\n",
103+
" [ 5, 6, 7, 8],\n",
104+
" [ 9, 10, 11, 12],\n",
105+
" [13, 14, 15, 16]])"
106+
]
107+
},
108+
"execution_count": 7,
109+
"metadata": {},
110+
"output_type": "execute_result"
111+
}
112+
],
113+
"source": [
114+
"input_"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"### 卷积 in Numpy"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 8,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"x = input_.reshape((-1, 1))"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 9,
136+
"metadata": {},
137+
"outputs": [
138+
{
139+
"data": {
140+
"text/plain": [
141+
"array([[ 1],\n",
142+
" [ 2],\n",
143+
" [ 3],\n",
144+
" [ 4],\n",
145+
" [ 5],\n",
146+
" [ 6],\n",
147+
" [ 7],\n",
148+
" [ 8],\n",
149+
" [ 9],\n",
150+
" [10],\n",
151+
" [11],\n",
152+
" [12],\n",
153+
" [13],\n",
154+
" [14],\n",
155+
" [15],\n",
156+
" [16]])"
157+
]
158+
},
159+
"execution_count": 9,
160+
"metadata": {},
161+
"output_type": "execute_result"
162+
}
163+
],
164+
"source": [
165+
"x"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 10,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": [
174+
"y = C.dot(x)"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": 11,
180+
"metadata": {},
181+
"outputs": [
182+
{
183+
"data": {
184+
"text/plain": [
185+
"array([[348],\n",
186+
" [393],\n",
187+
" [528],\n",
188+
" [573]])"
189+
]
190+
},
191+
"execution_count": 11,
192+
"metadata": {},
193+
"output_type": "execute_result"
194+
}
195+
],
196+
"source": [
197+
"y"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 12,
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"y = y.reshape((2, 2))"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": 13,
212+
"metadata": {},
213+
"outputs": [
214+
{
215+
"data": {
216+
"text/plain": [
217+
"array([[348, 393],\n",
218+
" [528, 573]])"
219+
]
220+
},
221+
"execution_count": 13,
222+
"metadata": {},
223+
"output_type": "execute_result"
224+
}
225+
],
226+
"source": [
227+
"y"
228+
]
229+
},
230+
{
231+
"cell_type": "markdown",
232+
"metadata": {},
233+
"source": [
234+
"### 卷积 in TensorFlow"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 14,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": [
243+
"x = tf.expand_dims(tf.expand_dims(tf.constant(input_, dtype=tf.float32), axis=-1), axis=0)"
244+
]
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": 15,
249+
"metadata": {},
250+
"outputs": [
251+
{
252+
"data": {
253+
"text/plain": [
254+
"[1, 4, 4, 1]"
255+
]
256+
},
257+
"execution_count": 15,
258+
"metadata": {},
259+
"output_type": "execute_result"
260+
}
261+
],
262+
"source": [
263+
"x.get_shape().as_list()"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 16,
269+
"metadata": {},
270+
"outputs": [],
271+
"source": [
272+
"kernel_conv = tf.expand_dims(tf.expand_dims(tf.constant(kernel, dtype=tf.float32), axis=-1), axis=-1)"
273+
]
274+
},
275+
{
276+
"cell_type": "code",
277+
"execution_count": 17,
278+
"metadata": {},
279+
"outputs": [
280+
{
281+
"data": {
282+
"text/plain": [
283+
"[3, 3, 1, 1]"
284+
]
285+
},
286+
"execution_count": 17,
287+
"metadata": {},
288+
"output_type": "execute_result"
289+
}
290+
],
291+
"source": [
292+
"kernel_conv.get_shape().as_list()"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": 18,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": [
301+
"y = tf.nn.conv2d(x, kernel_conv, strides=(1, 1, 1, 1), padding='VALID')"
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": 19,
307+
"metadata": {},
308+
"outputs": [
309+
{
310+
"name": "stdout",
311+
"output_type": "stream",
312+
"text": [
313+
"[[348. 393.]\n",
314+
" [528. 573.]]\n"
315+
]
316+
}
317+
],
318+
"source": [
319+
"with tf.Session() as sess:\n",
320+
" print(y.eval()[0, :, :, 0])"
321+
]
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python 3",
327+
"language": "python",
328+
"name": "python3"
329+
},
330+
"language_info": {
331+
"codemirror_mode": {
332+
"name": "ipython",
333+
"version": 3
334+
},
335+
"file_extension": ".py",
336+
"mimetype": "text/x-python",
337+
"name": "python",
338+
"nbconvert_exporter": "python",
339+
"pygments_lexer": "ipython3",
340+
"version": "3.6.6"
341+
}
342+
},
343+
"nbformat": 4,
344+
"nbformat_minor": 2
345+
}

0 commit comments

Comments
 (0)