머신러닝

x-2sin(x)=0 TensorFlow 수치해

coding art 2018. 11. 25. 16:30
728x90

# x2sinxeqn_regression_01.py

import tensorflow as tf

x_train = [-2.0, -0.7, 0.4, 2.0]
y_train = [-0.0059696,0.2037, 0-.05437, 0.0059696]

W1 = tf.Variable(tf.random_uniform([1],-2.0, -1.8, dtype = tf.float32, name='weight1'))
W2 = tf.Variable(tf.random_uniform([1],-0.5, +0.5, dtype = tf.float32, name='weight2'))
W3 = tf.Variable(tf.random_uniform([1], 1.8, +2.0, dtype = tf.float32, name='weight3'))

hypothesis = (x_train-2*tf.sin(x_train) - W1 +2*tf.sin(W1))*(x_train-2*tf.sin(x_train) - W2+2*tf.sin(W2))*(x_train-2*tf.sin(x_train) - W3 +2*tf.sin(W3))

cost = tf.reduce_mean(tf.square(hypothesis - y_train))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=4.0)
train = optimizer.minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())


for step in range(5001):
    sess.run(train)
    if step % 1000 == 0:
        print(step, sess.run(cost), sess.run(W1), sess.run(W2), sess.run(W3))