Skip to content

Instantly share code, notes, and snippets.

@seungwonpark
Last active September 8, 2018 05:43
Show Gist options
  • Save seungwonpark/0efdd0cf15a38936f1d2294fe0a81a78 to your computer and use it in GitHub Desktop.
Save seungwonpark/0efdd0cf15a38936f1d2294fe0a81a78 to your computer and use it in GitHub Desktop.
Sin wave regression using TensorFlow
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
xs = np.arange(-20,21).reshape(41, 1)
ys = np.sin(xs).reshape(41, 1)
W1 = tf.Variable(tf.random_normal([1, 20]))
W2 = tf.Variable(tf.random_normal([20, 20]))
W3 = tf.Variable(tf.random_normal([20, 20]))
W4 = tf.Variable(tf.random_normal([20, 20]))
W5 = tf.Variable(tf.random_normal([20, 1]))
b1 = tf.Variable(tf.random_normal([20]))
b2 = tf.Variable(tf.random_normal([20]))
b3 = tf.Variable(tf.random_normal([20]))
b4 = tf.Variable(tf.random_normal([20]))
b5 = tf.Variable(tf.random_normal([1]))
L1 = tf.matmul(X, W1) + b1
L1 = tf.nn.sigmoid(L1)
L2 = tf.matmul(L1, W2) + b2
L2 = tf.nn.sigmoid(L2)
L3 = tf.matmul(L2, W3) + b3
L3 = tf.nn.sigmoid(L3)
L4 = tf.matmul(L3, W4) + b4
L4 = tf.nn.sigmoid(L4)
L5 = tf.matmul(L4, W5) + b5
with tf.name_scope('Loss'):
cost = tf.reduce_mean(tf.square(Y - L5))
with tf.name_scope('Optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
test_x = np.linspace(-40, 40, 1000).reshape(1000, 1)
real_y = np.sin(test_x)
test_y = []
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(1, 100000):
cost_val, _ = sess.run([cost, train], feed_dict={X: xs, Y: ys})
if step % 20 == 0:
# tf.logging.info('Training step %04d / loss=%.8lf' % (step, cost_val))
print('Training step %05d / loss=%.8lf' % (step, cost_val))
test_y = sess.run(L5, feed_dict={X: test_x})
plt.title('Estimating sinusoidal wave with 4-layer CNN')
plt.ylim((-2, 2))
plt.plot(xs, ys, 'o', label='training data')
plt.plot(test_x, real_y, label='target: y=sin(x)')
plt.plot(test_x, test_y, label='regression')
plt.legend(loc=4)
plt.text(-41, 1.7, 'training step %05d' % step)
plt.text(-41, 1.5, 'cost=%.6f' % cost_val)
plt.savefig('frames/%05d.png' % step)
plt.clf()
if(cost_val < 5e-4):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment