teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

追記質問

2017/06/07 06:00

投稿

trafalbad
trafalbad

スコア303

title CHANGED
File without changes
body CHANGED
@@ -8,20 +8,24 @@
8
8
 
9
9
 
10
10
  def trainable_inference(input):
11
- hidden1_weight = tf.get_variable([INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
11
+ hidden1_weight = tf.get_variable("d_hidden1_weight",[INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
12
- hidden1_bias = tf.get_variable([HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
12
+ hidden1_bias = tf.get_variable("d_hidden1_bias",[HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
13
- output_weight = tf.get_variable([HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
13
+ output_weight = tf.get_variable("d_output_bias",[HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
14
- output_bias = tf.get_variable([1],initializer = tf.constant_initializer(0.1))
14
+ output_bias = tf.get_variable("d_output_bias",[1],initializer = tf.constant_initializer(0.1))
15
15
  hidden1_output = tf.nn.relu(tf.matmul(input, hidden1_weight) + hidden1_bias)
16
16
  output = tf.sigmoid(tf.matmul(hidden1_output, output_weight) + output_bias)
17
17
  return output
18
+
18
19
  ```
19
20
  この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
21
+
22
+ 追記
23
+ 名前を定義したのですが下記エラーが出てしまいました。
20
24
  ```
21
25
  d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
22
26
 
23
27
  trainable_inference(d_given_data_placeholder)
24
28
 
25
- TypeError: unhashable type: 'list'
29
+ TypeError:Value passed to parameter 'shape' has DataType string not in list of allowed values: int32, int64
26
30
  ```
27
- 解決策を教えてくださ
31
+ この解決策もお願いたします

4

修正

2017/06/07 06:00

投稿

trafalbad
trafalbad

スコア303

title CHANGED
File without changes
body CHANGED
@@ -20,7 +20,7 @@
20
20
  ```
21
21
  d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
22
22
 
23
- trainable_inference(g_input_placeholder)
23
+ trainable_inference(d_given_data_placeholder)
24
24
 
25
25
  TypeError: unhashable type: 'list'
26
26
  ```

3

修正

2017/06/07 05:35

投稿

trafalbad
trafalbad

スコア303

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,7 @@
18
18
  ```
19
19
  この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
20
20
  ```
21
- given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
21
+ d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
22
22
 
23
23
  trainable_inference(g_input_placeholder)
24
24
 

2

修正

2017/06/07 05:34

投稿

trafalbad
trafalbad

スコア303

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,7 @@
18
18
  ```
19
19
  この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
20
20
  ```
21
- d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
21
+ given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
22
22
 
23
23
  trainable_inference(g_input_placeholder)
24
24
 

1

修正

2017/06/07 05:33

投稿

trafalbad
trafalbad

スコア303

title CHANGED
@@ -1,1 +1,1 @@
1
- cifar10のベルPlaceholderの形状について
1
+ ー「unhashable type: 'list'」解決方法
body CHANGED
@@ -1,141 +1,27 @@
1
- cifar10をCNNで学習させようていす。CNNの構造は畳み込み層1→プーリング層1→畳み込み層2→pooling層2→全結合層→出力層となっています。
1
+ CNNの一部次のような関数を定義しました
2
2
 
3
+ ```
4
+ INPUT_SIZE = 1
3
- ここで1点注意しなければいけないのが
5
+ HIDDEN_UNIT_SIZE = 64
4
- 1.損失関数のlogits=y_convとlabels=yは同じにしなければならないことです。
6
+ TRAIN_DATA_SIZE = 100
7
+ OUTPUT_SIZE = 1
5
8
 
9
+
10
+ def trainable_inference(input):
11
+ hidden1_weight = tf.get_variable([INPUT_SIZE, HIDDEN_UNIT_SIZE], initializer = tf.random_normal_initializer(0, 0.1))
12
+ hidden1_bias = tf.get_variable([HIDDEN_UNIT_SIZE], initializer = tf.constant_initializer(0.1))
13
+ output_weight = tf.get_variable([HIDDEN_UNIT_SIZE, 1],initializer = tf.random_normal_initializer(0, 0.1))
14
+ output_bias = tf.get_variable([1],initializer = tf.constant_initializer(0.1))
15
+ hidden1_output = tf.nn.relu(tf.matmul(input, hidden1_weight) + hidden1_bias)
16
+ output = tf.sigmoid(tf.matmul(hidden1_output, output_weight) + output_bias)
17
+ return output
6
18
  ```
7
- y.shape y_conv.shape
8
- TensorShape([Dimension(None), Dimension(10)])
19
+ この関数に引数として以下の値を入れたのですが、エラーが発生してしまいました。
9
20
  ```
10
- yはPlaceholderです。
11
- しかしこのまま学習を行おうとすると以下のエラーメッセージが出てしまいます。
12
- ```
13
- Cannot feed value of shape (50000,) for Tensor 'Placeholder_16:0', which has shape '(?, 10)
21
+ d_given_data_placeholder = tf.placeholder("float", [None, INPUT_SIZE])
14
- ```
15
- これはおそらくラベルの形に等しいのでラベルをPlaceholderに入れることができないことを表しています。しかしここでPlaceholderの形を[None]にしてしまうと損失関数のlogits=y_convとlabels=yが一致しなくなってしまいます。
16
- ・損失関数のlogits=y_convとlabels=yが一致させる
17
- ・ラベル(y_trainとy_test)とy(Placeholder)を一致させる
18
22
 
19
- この矛盾を解決するにはどのようにしたらいいのでしょうか?training時にfeed_dictで画像とラベルを渡しているので、Placeholderの形状はラベルに合わせなければならない気がします
23
+ trainable_inference(g_input_placeholder)
20
24
 
21
- コード全容
25
+ TypeError: unhashable type: 'list'
22
-
23
26
  ```
24
- #データセット作成
25
- import sys
26
- import pickle
27
- import numpy as np
28
-
29
-
30
- def unpickle(file):
31
- fp = open(file, 'rb')
32
- if sys.version_info.major == 2:
33
- data = pickle.load(fp)
34
- elif sys.version_info.major == 3:
35
- data = np.load(fp, encoding='latin1')
36
- fp.close()
37
-
38
- return data
39
-
40
- X_train = None
41
- y_train = []
27
+ 解決策を教えてください?
42
-
43
- for i in range(1,6):
44
- data_dic = unpickle("data_batch_1".format(i))
45
- if i == 1:
46
- X_train = data_dict['data']
47
- y_train = data_dict['labels']
48
- else:
49
- X_train = np.vstack((X_train, data_dict['data']))
50
- y_train = np.hstack((y_train, data_dict['labels']))
51
-
52
- test_data_dic = unpickle("test_batch")
53
- X_test = test_data_dic['data']
54
- y_test =np.array(test_data_dic['labels'])
55
-
56
- #CNN
57
- from __future__ import absolute_import
58
- from __future__ import division
59
- from __future__ import print_function
60
- import tensorflow as tf
61
- import os
62
- import time
63
- import numpy as np
64
- NUM_CLASSES = 10
65
-
66
- def weight_variable(shape):
67
- initial = tf.truncated_normal(shape, stddev=0.1)
68
- return tf.Variable(initial)
69
-
70
- # バイアス変数
71
- def bias_variable(shape):
72
- initial = tf.constant(0.1, shape=shape)
73
- return tf.Variable(initial)
74
-
75
- # 畳み込み
76
- def conv2d(x, W):
77
- return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
78
-
79
- # プーリング
80
- def max_pool_2x2(x):
81
- return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
82
- strides=[1, 2, 2, 1], padding='SAME')
83
-
84
- x = tf.placeholder(tf.float32, [None, 3*32*32])
85
- y = tf.placeholder(tf.float32, [None,10])
86
-
87
- # 畳み込み1層目(conv1)
88
- W_conv1 = weight_variable([5, 5, 3, 32])
89
- b_conv1 = bias_variable([32])
90
- x_image = tf.reshape(x, [-1,32,32,3])
91
- h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
92
- # pool1
93
- h_pool1 = max_pool_2x2(h_conv1)
94
-
95
-
96
- # 畳み込み2層目(conv2)
97
- W_conv2 = weight_variable([5, 5, 32, 64])
98
- b_conv2 = bias_variable([64])
99
- h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
100
- # pool2
101
- h_pool2 = max_pool_2x2(h_conv2)
102
-
103
-
104
- # 全結合層
105
- W_fc1 = weight_variable([8 * 8 * 64, 1024])
106
- b_fc1 = bias_variable([1024])
107
- h_pool2_flat = tf.reshape(h_pool2, [-1, 8 * 8 * 64])
108
- h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
109
-
110
-
111
- # ドロップアウト層
112
- keep_prob = tf.placeholder(tf.float32)
113
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
114
-
115
- # 出力層
116
- W_fc2 = weight_variable([1024, 10])
117
- b_fc2 = bias_variable([10])
118
- y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
119
-
120
- # 損失関数(交差エントロピー誤差)
121
- cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y))
122
-
123
- # 勾配
124
- train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
125
-
126
- # 精度
127
- correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
128
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
129
- # セッション
130
- sess = tf.InteractiveSession()
131
- sess.run(tf.global_variables_initializer())
132
- #学習
133
- i=0
134
- for i in range(100):
135
- i+=1
136
- sess.run(train_step, feed_dict={x: X_train, y: y_train, keep_prob: 0.5})
137
- if i % 10 == 0:
138
- # 途中経過(10件ごと)
139
- loss_val, acc_val = sess.run([loss, accuracy], feed_dict={x: X_test, y: y_test, keep_prob: 1.0})
140
- print("step: %d, Loss: %f, Accuracy: %f" % (i, loss_val, acc_val))
141
- ```