質問編集履歴
1
コードを間違えたため
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,93 +6,7 @@
|
|
6
6
|
|
7
7
|
### 記述コード
|
8
8
|
```ここに言語を入力
|
9
|
-
import numpy as np
|
10
|
-
import tensorflow as tf
|
11
|
-
import keras
|
12
9
|
|
13
|
-
##重み(特殊な正規分布から発生する値)
|
14
|
-
def weight(shape = []):
|
15
|
-
initial = tf.truncated_normal(shape, stddev = 0.01)
|
16
|
-
return tf.Variable(initial)
|
17
|
-
|
18
|
-
##バイアス
|
19
|
-
def bias(dtype = tf.float32, shape = []):
|
20
|
-
initial = tf.zeros(shape, dtype = dtype)
|
21
|
-
return tf.Variable(initial)
|
22
|
-
|
23
|
-
##損失関数(交叉エントロピー)
|
24
|
-
def loss(t, f):
|
25
|
-
cross_entropy = tf.reduce_mean(-tf.reduce_sum(t * tf.log(f)))
|
26
|
-
return cross_entropy
|
27
|
-
|
28
|
-
##正確性の尺度
|
29
|
-
def accuracy(t, f):
|
30
|
-
correct_prediction = tf.equal(tf.argmax(t, 1), tf.argmax(f, 1))
|
31
|
-
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
32
|
-
return accuracy
|
33
|
-
|
34
|
-
##層の数
|
35
|
-
Q = 60
|
36
|
-
P = 60
|
37
|
-
R = 1
|
38
|
-
|
39
|
-
sess = tf.InteractiveSession()
|
40
|
-
|
41
|
-
X = tf.placeholder(dtype = tf.float32, shape = [None, Q])
|
42
|
-
t = tf.placeholder(dtype = tf.float32, shape = [None, R])
|
43
|
-
|
44
|
-
##隠れ層
|
45
|
-
##活性化関数はシグモイド関数
|
46
|
-
W1 = weight(shape = [Q, P])
|
47
|
-
b1 = bias(shape = [P])
|
48
|
-
f1 = tf.matmul(X, W1) + b1
|
49
|
-
sigm = tf.nn.sigmoid(f1)
|
50
|
-
|
51
|
-
##出力層
|
52
|
-
##fはソフトマックス関数(出力を0~1に制限)
|
53
|
-
W2 = weight(shape = [P, R])
|
54
|
-
b2 = bias(shape = [R])
|
55
|
-
f2 = tf.matmul(sigm, W2) + b2
|
56
|
-
f = tf.nn.softmax(f2)
|
57
|
-
|
58
|
-
loss = loss(t, f)
|
59
|
-
acc = accuracy(t, f)
|
60
|
-
|
61
|
-
##BP法
|
62
|
-
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.95)
|
63
|
-
train_step = optimizer.minimize(loss)
|
64
|
-
|
65
|
-
##学習を実行
|
66
|
-
with tf.Session() as sess:
|
67
|
-
init_op = tf.global_variables_initializer()
|
68
|
-
sess.run(init_op)
|
69
|
-
|
70
|
-
#ここからインデント調整
|
71
|
-
|
72
|
-
##学習データを習得
|
73
|
-
from sklearn.model_selection import train_test_split
|
74
|
-
##RMSE用
|
75
|
-
from sklearn.metrics import mean_squared_error
|
76
|
-
from math import sqrt
|
77
|
-
##説明変数(入力特徴量)
|
78
|
-
x = DataFrame(input_data)
|
79
|
-
x2 = DataFrame(input_test_data)
|
80
|
-
##目的変数(評価データ)
|
81
|
-
y = DataFrame(learning_output_data)
|
82
|
-
y2 = DataFrame(learning_test_data)
|
83
|
-
##説明変数・目的変数をそれぞれ訓練データ・テストデータに分割
|
84
|
-
train_x = x
|
85
|
-
test_x = x2
|
86
|
-
train_t = y
|
87
|
-
test_t = y2
|
88
|
-
|
89
|
-
#データの整形(tは0.0~1.0の値に変換)
|
90
|
-
train_x = train_x.astype(np.float)
|
91
|
-
test_x = test_x.astype(np.float)
|
92
|
-
|
93
|
-
train_t = train_t.T.astype(np.float)/7.0
|
94
|
-
test_t = test_t.astype(np.float)/7.0
|
95
|
-
|
96
10
|
##ミニバッチ学習
|
97
11
|
num_epoch = 10000
|
98
12
|
num_data = train_x.shape[0]
|