モデル
予測値Y’ 真の予測値Y
損失関数
損失率
オプティマイザー
①モデルは何か?
②損失関数は何を使っているか?
③オプティマイザーは何を使っているか?
15num_coeffs = 6
16trY_coeffs = [1, 2, 3, 4, 5, 6]
17trY = 0
18for i in range(num_coeffs):
19 #print(i)
20 trY += trY_coeffs[i] * np.power(trX, i)
21 #print(trX)
22 #print(trY)
23
24trY += np.random.randn(*trX.shape) * 1.5
25
26#print(trX)
27#print(trY)
28plt.scatter(trX, trY)
29plt.show()
F(x) = Wn*X^n + … + W1*x + W0
https://github.com/RuoAndo/seminar/blob/master/jt/7-nonlinear.py
41w = tf.Variable([0.] * num_coeffs, name="parameters")
42y_model = model(X, w)
43
44cost = tf.reduce_sum(tf.square(Y-y_model))
45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
46
47sess = tf.Session()
48init = tf.global_variables_initializer()
49sess.run(init)
50
51for epoch in range(training_epochs):
52 for (x, y) in zip(trX, trY):
53 sess.run(train_op, feed_dict={X: x, Y: y})
54
55w_val = sess.run(w)
56print(w_val)
[1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995]
session.runの引数
train_opを探す。
41w = tf.Variable([0.] * num_coeffs, name="parameters")
42y_model = model(X, w)
43
44cost = tf.reduce_sum(tf.square(Y-y_model))
45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
46
47sess = tf.Session()
48init = tf.global_variables_initializer()
49sess.run(init)
50
51for epoch in range(training_epochs):
52 for (x, y) in zip(trX, trY):
53 sess.run(train_op, feed_dict={X: x, Y: y})
54
55w_val = sess.run(w)
56print(w_val)
[1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995]
損失関数は
何か?
41w = tf.Variable([0.] * num_coeffs, name="parameters")
42y_model = model(X, w)
43
44cost = tf.reduce_sum(tf.square(Y-y_model))
45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
46
47sess = tf.Session()
48init = tf.global_variables_initializer()
49sess.run(init)
50
51for epoch in range(training_epochs):
52 for (x, y) in zip(trX, trY):
53 sess.run(train_op, feed_dict={X: x, Y: y})
54
55w_val = sess.run(w)
56print(w_val)
[1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995]
モデルを探す。
41w = tf.Variable([0.] * num_coeffs, name="parameters")
42y_model = model(X, w)
43
44cost = tf.reduce_sum(tf.square(Y-y_model))
45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
46
47sess = tf.Session()
48init = tf.global_variables_initializer()
49sess.run(init)
50
51for epoch in range(training_epochs):
52 for (x, y) in zip(trX, trY):
53 sess.run(train_op, feed_dict={X: x, Y: y})
54
55w_val = sess.run(w)
56print(w_val)
[1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995]
34def model(X, w):
35 terms = []
36 for i in range(num_coeffs):
37 term = tf.multiply(w[i], tf.pow(X, i))
38 terms.append(term)
39 return tf.add_n(terms)
モデル
34def model(X, w):
35 terms = []
36 for i in range(num_coeffs):
37 term = tf.multiply(w[i], tf.pow(X, i))
38 terms.append(term)
39 return tf.add_n(terms)
モデル
15num_coeffs = 6
16trY_coeffs = [1, 2, 3, 4, 5, 6]
17trY = 0
18for i in range(num_coeffs):
19 #print(i)
20 trY += trY_coeffs[i] * np.power(trX, i)
生成器
60plt.scatter(trX, trY)
61trY2 = 0
62for i in range(num_coeffs):
63 trY2 += w_val[i] * np.power(trX, i)
64plt.plot(trX, trY2, 'r')
65plt.show()
15num_coeffs = 6
16trY_coeffs = [1, 2, 3, 4, 5, 6]
17trY = 0
18for i in range(num_coeffs):
19 #print(i)
20 trY += trY_coeffs[i] * np.power(trX, i)
生成器

【AI実装4】TensorFlowのプログラムを読む2 非線形回帰

  • 1.
  • 2.
    15num_coeffs = 6 16trY_coeffs= [1, 2, 3, 4, 5, 6] 17trY = 0 18for i in range(num_coeffs): 19 #print(i) 20 trY += trY_coeffs[i] * np.power(trX, i) 21 #print(trX) 22 #print(trY) 23 24trY += np.random.randn(*trX.shape) * 1.5 25 26#print(trX) 27#print(trY) 28plt.scatter(trX, trY) 29plt.show() F(x) = Wn*X^n + … + W1*x + W0 https://github.com/RuoAndo/seminar/blob/master/jt/7-nonlinear.py
  • 3.
    41w = tf.Variable([0.]* num_coeffs, name="parameters") 42y_model = model(X, w) 43 44cost = tf.reduce_sum(tf.square(Y-y_model)) 45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 46 47sess = tf.Session() 48init = tf.global_variables_initializer() 49sess.run(init) 50 51for epoch in range(training_epochs): 52 for (x, y) in zip(trX, trY): 53 sess.run(train_op, feed_dict={X: x, Y: y}) 54 55w_val = sess.run(w) 56print(w_val) [1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995] session.runの引数 train_opを探す。
  • 4.
    41w = tf.Variable([0.]* num_coeffs, name="parameters") 42y_model = model(X, w) 43 44cost = tf.reduce_sum(tf.square(Y-y_model)) 45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 46 47sess = tf.Session() 48init = tf.global_variables_initializer() 49sess.run(init) 50 51for epoch in range(training_epochs): 52 for (x, y) in zip(trX, trY): 53 sess.run(train_op, feed_dict={X: x, Y: y}) 54 55w_val = sess.run(w) 56print(w_val) [1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995] 損失関数は 何か?
  • 5.
    41w = tf.Variable([0.]* num_coeffs, name="parameters") 42y_model = model(X, w) 43 44cost = tf.reduce_sum(tf.square(Y-y_model)) 45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 46 47sess = tf.Session() 48init = tf.global_variables_initializer() 49sess.run(init) 50 51for epoch in range(training_epochs): 52 for (x, y) in zip(trX, trY): 53 sess.run(train_op, feed_dict={X: x, Y: y}) 54 55w_val = sess.run(w) 56print(w_val) [1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995] モデルを探す。
  • 6.
    41w = tf.Variable([0.]* num_coeffs, name="parameters") 42y_model = model(X, w) 43 44cost = tf.reduce_sum(tf.square(Y-y_model)) 45train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 46 47sess = tf.Session() 48init = tf.global_variables_initializer() 49sess.run(init) 50 51for epoch in range(training_epochs): 52 for (x, y) in zip(trX, trY): 53 sess.run(train_op, feed_dict={X: x, Y: y}) 54 55w_val = sess.run(w) 56print(w_val) [1.0518903 1.7557341 4.2570124 5.756541 3.2840345 5.1628995] 34def model(X, w): 35 terms = [] 36 for i in range(num_coeffs): 37 term = tf.multiply(w[i], tf.pow(X, i)) 38 terms.append(term) 39 return tf.add_n(terms) モデル
  • 7.
    34def model(X, w): 35terms = [] 36 for i in range(num_coeffs): 37 term = tf.multiply(w[i], tf.pow(X, i)) 38 terms.append(term) 39 return tf.add_n(terms) モデル 15num_coeffs = 6 16trY_coeffs = [1, 2, 3, 4, 5, 6] 17trY = 0 18for i in range(num_coeffs): 19 #print(i) 20 trY += trY_coeffs[i] * np.power(trX, i) 生成器
  • 8.
    60plt.scatter(trX, trY) 61trY2 =0 62for i in range(num_coeffs): 63 trY2 += w_val[i] * np.power(trX, i) 64plt.plot(trX, trY2, 'r') 65plt.show() 15num_coeffs = 6 16trY_coeffs = [1, 2, 3, 4, 5, 6] 17trY = 0 18for i in range(num_coeffs): 19 #print(i) 20 trY += trY_coeffs[i] * np.power(trX, i) 生成器