-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv1d.py
204 lines (168 loc) · 8.75 KB
/
conv1d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-)
import tensorflow as tf
import numpy as np
print('----------example 1----------------')
# 定义一个矩阵a,表示需要被卷积的矩阵。
a = np.array(np.arange(1, 1 + 20).reshape([1, 10, 2]), dtype=np.float32)
print('a',a)
print('---------------------------')
# 卷积核,此处卷积核的数目为1
kernel = np.array(np.arange(1, 1 + 4), dtype=np.float32).reshape([2, 2, 1])
print('kernel',kernel)
# 进行conv1d卷积
#conv1d = tf.nn.conv1d(a, kernel, 1, 'VALID')
conv1d = tf.nn.conv1d(value = a, filters = kernel, stride = 1, padding = 'SAME')
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
print(sess.run(conv1d))
print('----------example 2----------------')
time_step = 144
input_size = 14
b = np.array(np.arange(1, 1 + 2016).reshape([1, time_step, input_size]), dtype=np.float32)
# 定义一个序列。
# 进行conv1d卷积
print('b',b)
#conv1d = tf.nn.conv1d(a, kernel, 1, 'VALID')
input_b = tf.placeholder(tf.float32, [None, time_step, input_size])
conv1d = tf.layers.conv1d(inputs = input_b, filters = 2, kernel_size = 3, strides = 2, padding = 'same', activation = tf.nn.relu)
conv2d = tf.layers.conv1d(inputs = conv1d, filters = 4, kernel_size = 3, strides = 2, padding = 'same', activation = tf.nn.relu)
conv4d = tf.layers.conv1d(inputs = conv2d, filters = 8, kernel_size = 3, strides = 2, padding = 'same', activation = tf.nn.relu)
conv8d = tf.layers.conv1d(inputs = conv4d, filters = 16, kernel_size = 3, strides = 2, padding = 'same', activation = tf.nn.relu)
#conv1d = tf.layers.conv1d(inputs = input_b, filters = 1, kernel_size = 3, strides = 2, padding = 'same')
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
print(sess.run(conv8d, feed_dict = {input_b:b}))
print('----------example 3----------------')
# (batch, 128, 9) -> (batch, 32, 18)
conv1 = tf.layers.conv1d(inputs=input_b, filters=18, kernel_size=2, strides=1,
padding='same', activation = tf.nn.relu)
# (batch, 32, 18) -> (batch, 8, 36)
conv2 = tf.layers.conv1d(inputs=conv1, filters=36, kernel_size=2, strides=1,
padding='same', activation = tf.nn.relu)
# (batch, 8, 36) -> (batch, 2, 72)
conv3 = tf.layers.conv1d(inputs=conv2, filters=72, kernel_size=2, strides=1,
padding='same', activation = tf.nn.relu)
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
print(sess.run(conv3, feed_dict = {input_b:b}))
print('---------- example 4 max pool ----------------')
time_step = 2016
time_step = 576
input_size = 1
b = np.array(np.arange(1, 1 + time_step).reshape([1, time_step, input_size]), dtype=np.float32)
input_b = tf.placeholder(tf.float32, [None, time_step, input_size])
def conv3_pool(filters=36, kernel_size=2, strides=1):
# (batch, 128, 9) -> (batch, 32, 36)
conv1 = tf.layers.conv1d(inputs=input_b, filters=36, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=4, strides=4, padding='same')
# (batch, 32, 18) -> (batch, 8, 72)
conv2 = tf.layers.conv1d(inputs=max_pool_1, filters=72, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=4, strides=4, padding='same')
# (batch, 8, 36) -> (batch, 2, 144)
conv3 = tf.layers.conv1d(inputs=max_pool_2, filters=144, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_3 = tf.layers.max_pooling1d(inputs=conv3, pool_size=4, strides=4, padding='same')
print 'max_pool_3',max_pool_3
lstm = tf.reshape(max_pool_3, [-1, 1])
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
conv = sess.run(max_pool_3, feed_dict = {input_b:b})
#conv = sess.run(lstm, feed_dict = {input_b:b})
#print(conv)
print('len(conv):', np.size(conv))
conv3_pool(filters=36, kernel_size=3, strides=2)
print('---------- ----------------')
conv3_pool(filters=36, kernel_size=4, strides=2)
print('---------- ----------------')
conv3_pool(filters=8, kernel_size=3, strides=2)
print('---------- example 5 max pool ----------------')
def conv4_pool(filters=18, kernel_size=2, strides=1):
# (batch, 128, 9) --> (batch, 64, 18)
conv1 = tf.layers.conv1d(inputs=input_b, filters=filters, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=2, strides=2, padding='same')
# (batch, 64, 18) --> (batch, 32, 36)
conv2 = tf.layers.conv1d(inputs=max_pool_1, filters=filters*2, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=2, strides=2, padding='same')
# (batch, 32, 36) --> (batch, 16, 72)
conv3 = tf.layers.conv1d(inputs=max_pool_2, filters=filters*2*2, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_3 = tf.layers.max_pooling1d(inputs=conv3, pool_size=2, strides=2, padding='same')
# (batch, 16, 72) --> (batch, 8, 144)
conv4 = tf.layers.conv1d(inputs=max_pool_3, filters=filters*2*2*2, kernel_size=kernel_size, strides=strides,
padding='same', activation = tf.nn.relu)
max_pool_4 = tf.layers.max_pooling1d(inputs=conv4, pool_size=2, strides=2, padding='same')
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
conv = sess.run(max_pool_4, feed_dict = {input_b:b})
#print(conv)
print('len(conv):', np.size(conv))
conv4_pool(filters=4, kernel_size=3, strides=2)
print('---------- example 6 max pool ----------------')
def conv4(filters=2, kernel_size=3, strides=2):
# (batch, 128, 9) --> (batch, 64, 18)
conv1 = tf.layers.conv1d(inputs=input_b, filters = filters, kernel_size = kernel_size, strides = strides,
padding='same', activation = tf.nn.relu)
# (batch, 64, 18) --> (batch, 32, 36)
conv2 = tf.layers.conv1d(inputs=conv1, filters=4, kernel_size = kernel_size, strides = strides,
padding='same', activation = tf.nn.relu)
# (batch, 32, 36) --> (batch, 16, 72)
conv3 = tf.layers.conv1d(inputs=conv2, filters=8, kernel_size = kernel_size, strides = strides,
padding='same', activation = tf.nn.relu)
# (batch, 16, 72) --> (batch, 8, 144)
conv4 = tf.layers.conv1d(inputs=conv3, filters=16, kernel_size = kernel_size, strides = strides,
padding='same', activation = tf.nn.relu)
print conv4
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
conv = sess.run(conv3, feed_dict = {input_b:b})
#print(conv)
print('len(conv3):', np.size(conv))
# 输出卷积值
conv = sess.run(conv4, feed_dict = {input_b:b})
#print(conv)
print('len(conv4):', np.size(conv))
conv4(filters=2, kernel_size=3, strides=2)
print('---------- example 6 max pool ----------------')
conv4(filters=2, kernel_size=3, strides=4)
print('---------- example 7 max pool ----------------')
def conv4(filters=2, kernel_size=3, strides=2):
# (batch, 128, 9) --> (batch, 64, 18)
conv1 = tf.layers.conv1d(inputs=input_b, filters=2, kernel_size=3, strides=2,
padding='same', activation = tf.nn.relu)
max_pool_1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=2, strides=2, padding='same')
# (batch, 64, 18) --> (batch, 32, 36)
conv2 = tf.layers.conv1d(inputs=conv1, filters=4, kernel_size=3, strides=2,
padding='same', activation = tf.nn.relu)
# (batch, 32, 36) --> (batch, 16, 72)
conv3 = tf.layers.conv1d(inputs=conv2, filters=8, kernel_size=3, strides=2,
padding='same', activation = tf.nn.relu)
# (batch, 16, 72) --> (batch, 8, 144)
conv4 = tf.layers.conv1d(inputs=conv3, filters=16, kernel_size=3, strides=2,
padding='same', activation = tf.nn.relu)
print conv4
with tf.Session() as sess:
# 初始化
tf.global_variables_initializer().run()
# 输出卷积值
conv = sess.run(conv3, feed_dict = {input_b:b})
#print(conv)
print('len(conv3):', np.size(conv))
conv = sess.run(conv4, feed_dict = {input_b:b})
#print(conv)
print('len(conv4):', np.size(conv))
conv4(filters=2, kernel_size=3, strides=2)