質問編集履歴

5

追記質問

2017/06/07 06:00

投稿

trafalbad
trafalbad

スコア303

test CHANGED
File without changes
test CHANGED
@@ -18,13 +18,13 @@
18
18
 
19
19
  def trainable_inference(input):
20
20
 
21
- hidden1_weight = tf.get_variable([INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
21
+ hidden1_weight = tf.get_variable("d_hidden1_weight",[INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
22
22
 
23
- hidden1_bias = tf.get_variable([HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
23
+ hidden1_bias = tf.get_variable("d_hidden1_bias",[HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
24
24
 
25
- output_weight = tf.get_variable([HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
25
+ output_weight = tf.get_variable("d_output_bias",[HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
26
26
 
27
- output_bias = tf.get_variable([1],initializer = tf.constant_initializer(0.1))
27
+ output_bias = tf.get_variable("d_output_bias",[1],initializer = tf.constant_initializer(0.1))
28
28
 
29
29
  hidden1_output = tf.nn.relu(tf.matmul(input, hidden1_weight) + hidden1_bias)
30
30
 
@@ -32,9 +32,17 @@
32
32
 
33
33
  return output
34
34
 
35
+
36
+
35
37
  ```
36
38
 
37
39
  この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
40
+
41
+
42
+
43
+ 追記
44
+
45
+ 名前を定義したのですが下記エラーが出てしまいました。
38
46
 
39
47
  ```
40
48
 
@@ -46,8 +54,8 @@
46
54
 
47
55
 
48
56
 
49
- TypeError: unhashable type: 'list'
57
+ TypeError:Value passed to parameter 'shape' has DataType string not in list of allowed values: int32, int64
50
58
 
51
59
  ```
52
60
 
53
- 解決策を教えてくださ
61
+ この解決策もお願いたします

4

修正

2017/06/07 06:00

投稿

trafalbad
trafalbad

スコア303

test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
 
44
44
 
45
- trainable_inference(g_input_placeholder)
45
+ trainable_inference(d_given_data_placeholder)
46
46
 
47
47
 
48
48
 

3

修正

2017/06/07 05:35

投稿

trafalbad
trafalbad

スコア303

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  ```
40
40
 
41
- given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
41
+ d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
42
42
 
43
43
 
44
44
 

2

修正

2017/06/07 05:34

投稿

trafalbad
trafalbad

スコア303

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  ```
40
40
 
41
- d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
41
+ given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
42
42
 
43
43
 
44
44
 

1

修正

2017/06/07 05:33

投稿

trafalbad
trafalbad

スコア303

test CHANGED
@@ -1 +1 @@
1
- cifar10のベルのPlaceholder形状について
1
+ ー「unhashable type: 'list'」解決方法
test CHANGED
@@ -1,281 +1,53 @@
1
- cifar10をCNNで学習させようとしています。CNNの構造は畳み込み層1→プーリング層1→畳み込み層2→pooling層2→全結合層→出力層となっています。
2
-
3
-
4
-
5
- ここ1点注意なければいけないのが
1
+ CNNの一部次のような関数を定義ました
6
-
7
- 1.損失関数のlogits=y_convとlabels=yは同じにしなければならないことです。
8
2
 
9
3
 
10
4
 
11
5
  ```
12
6
 
13
- y.shape y_conv.shape
7
+ INPUT_SIZE = 1
14
8
 
15
- TensorShape([Dimension(None), Dimension(10)])
9
+ HIDDEN_UNIT_SIZE = 64
16
10
 
17
- ```
11
+ TRAIN_DATA_SIZE = 100
18
12
 
19
- yはPlaceholderです。
20
-
21
- しかしこのまま学習を行おうとすると以下のエラーメッセージが出てしまいます。
22
-
23
- ```
24
-
25
- Cannot feed value of shape (50000,) for Tensor 'Placeholder_16:0', which has shape '(?, 10)’
26
-
27
- ```
28
-
29
- これはおそらくラベルの形に等しいのでラベルをPlaceholderに入れることができないことを表しています。しかしここでPlaceholderの形を[None]にしてしまうと損失関数のlogits=y_convとlabels=yが一致しなくなってしまいます。
30
-
31
- ・損失関数のlogits=y_convとlabels=yが一致させる
32
-
33
- ・ラベル(y_trainとy_test)とy(Placeholder)を一致させる
34
-
35
-
36
-
37
- この矛盾を解決するにはどのようにしたらいいのでしょうか?training時にfeed_dictで画像とラベルを渡しているので、Placeholderの形状はラベルに合わせなければならない気がします
38
-
39
-
40
-
41
- コード全容
42
-
43
-
44
-
45
- ```
46
-
47
- #データセット作成
48
-
49
- import sys
50
-
51
- import pickle
52
-
53
- import numpy as np
13
+ OUTPUT_SIZE = 1
54
14
 
55
15
 
56
16
 
57
17
 
58
18
 
59
- def unpickle(file):
19
+ def trainable_inference(input):
60
20
 
61
- fp = open(file, 'rb')
21
+ hidden1_weight = tf.get_variable([INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
62
22
 
63
- if sys.version_info.major == 2:
23
+ hidden1_bias = tf.get_variable([HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
64
24
 
65
- data = pickle.load(fp)
25
+ output_weight = tf.get_variable([HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
66
26
 
67
- elif sys.version_info.major == 3:
27
+ output_bias = tf.get_variable([1],initializer = tf.constant_initializer(0.1))
68
28
 
69
- data = np.load(fp, encoding='latin1')
29
+ hidden1_output = tf.nn.relu(tf.matmul(input, hidden1_weight) + hidden1_bias)
70
30
 
31
+ output = tf.sigmoid(tf.matmul(hidden1_output, output_weight) + output_bias)
32
+
71
- fp.close()
33
+ return output
34
+
35
+ ```
36
+
37
+ この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
38
+
39
+ ```
40
+
41
+ d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
72
42
 
73
43
 
74
44
 
75
- return data
45
+ trainable_inference(g_input_placeholder)
76
46
 
77
47
 
78
48
 
79
- X_train = None
80
-
81
- y_train = []
82
-
83
-
84
-
85
- for i in range(1,6):
86
-
87
- data_dic = unpickle("data_batch_1".format(i))
88
-
89
- if i == 1:
90
-
91
- X_train = data_dict['data']
92
-
93
- y_train = data_dict['labels']
49
+ TypeError: unhashable type: 'list'
94
-
95
- else:
96
-
97
- X_train = np.vstack((X_train, data_dict['data']))
98
-
99
- y_train = np.hstack((y_train, data_dict['labels']))
100
-
101
-
102
-
103
- test_data_dic = unpickle("test_batch")
104
-
105
- X_test = test_data_dic['data']
106
-
107
- y_test =np.array(test_data_dic['labels'])
108
-
109
-
110
-
111
- #CNN
112
-
113
- from __future__ import absolute_import
114
-
115
- from __future__ import division
116
-
117
- from __future__ import print_function
118
-
119
- import tensorflow as tf
120
-
121
- import os
122
-
123
- import time
124
-
125
- import numpy as np
126
-
127
- NUM_CLASSES = 10
128
-
129
-
130
-
131
- def weight_variable(shape):
132
-
133
- initial = tf.truncated_normal(shape, stddev=0.1)
134
-
135
- return tf.Variable(initial)
136
-
137
-
138
-
139
- # バイアス変数
140
-
141
- def bias_variable(shape):
142
-
143
- initial = tf.constant(0.1, shape=shape)
144
-
145
- return tf.Variable(initial)
146
-
147
-
148
-
149
- # 畳み込み
150
-
151
- def conv2d(x, W):
152
-
153
- return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
154
-
155
-
156
-
157
- # プーリング
158
-
159
- def max_pool_2x2(x):
160
-
161
- return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
162
-
163
- strides=[1, 2, 2, 1], padding='SAME')
164
-
165
-
166
-
167
- x = tf.placeholder(tf.float32, [None, 3*32*32])
168
-
169
- y = tf.placeholder(tf.float32, [None,10])
170
-
171
-
172
-
173
- # 畳み込み1層目(conv1)
174
-
175
- W_conv1 = weight_variable([5, 5, 3, 32])
176
-
177
- b_conv1 = bias_variable([32])
178
-
179
- x_image = tf.reshape(x, [-1,32,32,3])
180
-
181
- h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
182
-
183
- # pool1
184
-
185
- h_pool1 = max_pool_2x2(h_conv1)
186
-
187
-
188
-
189
-
190
-
191
- # 畳み込み2層目(conv2)
192
-
193
- W_conv2 = weight_variable([5, 5, 32, 64])
194
-
195
- b_conv2 = bias_variable([64])
196
-
197
- h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
198
-
199
- # pool2
200
-
201
- h_pool2 = max_pool_2x2(h_conv2)
202
-
203
-
204
-
205
-
206
-
207
- # 全結合層
208
-
209
- W_fc1 = weight_variable([8 * 8 * 64, 1024])
210
-
211
- b_fc1 = bias_variable([1024])
212
-
213
- h_pool2_flat = tf.reshape(h_pool2, [-1, 8 * 8 * 64])
214
-
215
- h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
216
-
217
-
218
-
219
-
220
-
221
- # ドロップアウト層
222
-
223
- keep_prob = tf.placeholder(tf.float32)
224
-
225
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
226
-
227
-
228
-
229
- # 出力層
230
-
231
- W_fc2 = weight_variable([1024, 10])
232
-
233
- b_fc2 = bias_variable([10])
234
-
235
- y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
236
-
237
-
238
-
239
- # 損失関数(交差エントロピー誤差)
240
-
241
- cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y))
242
-
243
-
244
-
245
- # 勾配
246
-
247
- train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
248
-
249
-
250
-
251
- # 精度
252
-
253
- correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
254
-
255
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
256
-
257
- # セッション
258
-
259
- sess = tf.InteractiveSession()
260
-
261
- sess.run(tf.global_variables_initializer())
262
-
263
- #学習
264
-
265
- i=0
266
-
267
- for i in range(100):
268
-
269
- i+=1
270
-
271
- sess.run(train_step, feed_dict={x: X_train, y: y_train, keep_prob: 0.5})
272
-
273
- if i % 10 == 0:
274
-
275
- # 途中経過(10件ごと)
276
-
277
- loss_val, acc_val = sess.run([loss, accuracy], feed_dict={x: X_test, y: y_test, keep_prob: 1.0})
278
-
279
- print("step: %d, Loss: %f, Accuracy: %f" % (i, loss_val, acc_val))
280
50
 
281
51
  ```
52
+
53
+ 解決策を教えてください?