回答編集履歴

1

d

2019/01/11 03:44

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -1 +1,67 @@
1
1
  回帰問題の場合、loss には binary_crossentropy ではなく mean_squared_error を指定するべきではないでしょうか
2
+
3
+
4
+
5
+ ## 追記
6
+
7
+
8
+
9
+ XOR を学習するサンプル
10
+
11
+
12
+
13
+ ```python
14
+
15
+ # -*- coding: utf-8 -*-
16
+
17
+ import numpy as np
18
+
19
+ from keras.layers.core import Dense
20
+
21
+ from keras.models import Sequential
22
+
23
+
24
+
25
+ X_train = np.array([[0, 0], [1, 0], [0, 1], [1, 1]], dtype=float)
26
+
27
+ Y_train = np.array([0, 1, 1, 0], dtype=float)
28
+
29
+
30
+
31
+ # モデルを作成する。
32
+
33
+ model = Sequential()
34
+
35
+ model.add(Dense(2, activation='sigmoid', input_shape=(2,)))
36
+
37
+ model.add(Dense(1, activation='linear'))
38
+
39
+ model.compile(loss='mean_squared_error',
40
+
41
+ optimizer='rmsprop', metrics=['accuracy'])
42
+
43
+
44
+
45
+ # 学習する。
46
+
47
+ history = model.fit(X_train, Y_train, batch_size=4, epochs=3000, verbose=0)
48
+
49
+
50
+
51
+ # 推論する。
52
+
53
+ Y_pred = model.predict(X_train)
54
+
55
+ result = np.round(Y_pred)
56
+
57
+ print(result)
58
+
59
+ # [[0.]
60
+
61
+ # [1.]
62
+
63
+ # [1.]
64
+
65
+ # [0.]]
66
+
67
+ ```