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

質問編集履歴

1

内容を追加しました

2021/01/25 07:18

投稿

GAKU_SAY
GAKU_SAY

スコア23

title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,9 @@
2
2
  コードに問題があるのでしょうか?
3
3
  お教えいただけるとありがたいです.
4
4
 
5
+ 内容は![イメージ説明](60c124e29c52a614993dd0e5bf972c63.png)
6
+ の極値を求めるプログラムです.
7
+
5
8
  pythonは初心者です.
6
9
 
7
10
  ```python
@@ -23,6 +26,7 @@
23
26
  #学習係数
24
27
  eta = 0.01
25
28
 
29
+ #関数
26
30
  def function(x):
27
31
  if len(x)!=size:
28
32
  print('ERROR in function',len(x))
@@ -36,17 +40,20 @@
36
40
  i += 1
37
41
  return ans
38
42
 
39
-
43
+ #ベクトルの足し算
40
44
  def tashizann(x,y):
41
45
  wrk = np.array(x) + np.array(y)
42
46
  return wrk.tolist()
43
47
 
48
+ #ベクトルとスカラーの掛け算
44
49
  def kakezann(x,y):#x:vector, y:scalor
45
50
  ans = []
46
51
  for xi in x:
47
52
  ans.append(y*xi)
48
53
 
49
54
  return ans
55
+
56
+ #1階微分を数値で行う関数
50
57
  def get_gradient_vector(x):
51
58
  #1階微分
52
59
  ans = []
@@ -66,12 +73,14 @@
66
73
 
67
74
  return ans
68
75
 
76
+ #ベクトルが0のみになったか確認
69
- def check_sa(x):
77
+ def check(x):
70
78
  for xi in x:
71
79
  if xi != 0:
72
80
  return False
73
81
  return True
74
82
 
83
+ #処理
75
84
  def process_of_GD(number):
76
85
  #最急降下法
77
86
  i = 0
@@ -83,8 +92,8 @@
83
92
  global dx
84
93
  global eta
85
94
 
86
- point = [1,1,1,1,1,1]
95
+ #初期値
87
- #[random(),random(),random(),random(),random(),random()]
96
+ point = [random(),random(),random(),random(),random(),random()]
88
97
 
89
98
  #print("initial value is ",point)
90
99
 
@@ -98,15 +107,18 @@
98
107
 
99
108
 
100
109
  while True:
110
+ #プロット用
101
111
  print((point))
102
112
  n.append(i)
103
113
  fn.append(function(point))
104
114
 
115
+ #勾配ベクトルの確認
105
116
  gv = get_gradient_vector(point)
106
117
  if check_sa(gv) == True:
107
118
  print(gv)
108
119
  break
120
+
109
-
121
+ #更新
110
122
  point = tashizann(point, kakezann(gv, -1*eta))
111
123
  i += 1
112
124
  if i>2000000: