質問編集履歴

1

コードを間違えたため

2019/07/09 02:15

投稿

Bonziri
Bonziri

スコア16

test CHANGED
File without changes
test CHANGED
@@ -13,178 +13,6 @@
13
13
  ### 記述コード
14
14
 
15
15
  ```ここに言語を入力
16
-
17
- import numpy as np
18
-
19
- import tensorflow as tf
20
-
21
- import keras
22
-
23
-
24
-
25
- ##重み(特殊な正規分布から発生する値)
26
-
27
- def weight(shape = []):
28
-
29
- initial = tf.truncated_normal(shape, stddev = 0.01)
30
-
31
- return tf.Variable(initial)
32
-
33
-
34
-
35
- ##バイアス
36
-
37
- def bias(dtype = tf.float32, shape = []):
38
-
39
- initial = tf.zeros(shape, dtype = dtype)
40
-
41
- return tf.Variable(initial)
42
-
43
-
44
-
45
- ##損失関数(交叉エントロピー)
46
-
47
- def loss(t, f):
48
-
49
- cross_entropy = tf.reduce_mean(-tf.reduce_sum(t * tf.log(f)))
50
-
51
- return cross_entropy
52
-
53
-
54
-
55
- ##正確性の尺度
56
-
57
- def accuracy(t, f):
58
-
59
- correct_prediction = tf.equal(tf.argmax(t, 1), tf.argmax(f, 1))
60
-
61
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
62
-
63
- return accuracy
64
-
65
-
66
-
67
- ##層の数
68
-
69
- Q = 60
70
-
71
- P = 60
72
-
73
- R = 1
74
-
75
-
76
-
77
- sess = tf.InteractiveSession()
78
-
79
-
80
-
81
- X = tf.placeholder(dtype = tf.float32, shape = [None, Q])
82
-
83
- t = tf.placeholder(dtype = tf.float32, shape = [None, R])
84
-
85
-
86
-
87
- ##隠れ層
88
-
89
- ##活性化関数はシグモイド関数
90
-
91
- W1 = weight(shape = [Q, P])
92
-
93
- b1 = bias(shape = [P])
94
-
95
- f1 = tf.matmul(X, W1) + b1
96
-
97
- sigm = tf.nn.sigmoid(f1)
98
-
99
-
100
-
101
- ##出力層
102
-
103
- ##fはソフトマックス関数(出力を0~1に制限)
104
-
105
- W2 = weight(shape = [P, R])
106
-
107
- b2 = bias(shape = [R])
108
-
109
- f2 = tf.matmul(sigm, W2) + b2
110
-
111
- f = tf.nn.softmax(f2)
112
-
113
-
114
-
115
- loss = loss(t, f)
116
-
117
- acc = accuracy(t, f)
118
-
119
-
120
-
121
- ##BP法
122
-
123
- optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.95)
124
-
125
- train_step = optimizer.minimize(loss)
126
-
127
-
128
-
129
- ##学習を実行
130
-
131
- with tf.Session() as sess:
132
-
133
- init_op = tf.global_variables_initializer()
134
-
135
- sess.run(init_op)
136
-
137
-
138
-
139
- #ここからインデント調整
140
-
141
-
142
-
143
- ##学習データを習得
144
-
145
- from sklearn.model_selection import train_test_split
146
-
147
- ##RMSE用
148
-
149
- from sklearn.metrics import mean_squared_error
150
-
151
- from math import sqrt
152
-
153
- ##説明変数(入力特徴量)
154
-
155
- x = DataFrame(input_data)
156
-
157
- x2 = DataFrame(input_test_data)
158
-
159
- ##目的変数(評価データ)
160
-
161
- y = DataFrame(learning_output_data)
162
-
163
- y2 = DataFrame(learning_test_data)
164
-
165
- ##説明変数・目的変数をそれぞれ訓練データ・テストデータに分割
166
-
167
- train_x = x
168
-
169
- test_x = x2
170
-
171
- train_t = y
172
-
173
- test_t = y2
174
-
175
-
176
-
177
- #データの整形(tは0.0~1.0の値に変換)
178
-
179
- train_x = train_x.astype(np.float)
180
-
181
- test_x = test_x.astype(np.float)
182
-
183
-
184
-
185
- train_t = train_t.T.astype(np.float)/7.0
186
-
187
- test_t = test_t.astype(np.float)/7.0
188
16
 
189
17
 
190
18