In [2]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline
plt.style.use('ggplot')
In [3]:
x_max = 0
x_min = 10*np.pi
sample_size = 1000
inputs = np.linspace(x_min, x_max, sample_size)
def test_function(x):
return np.sin(x)
outputs = test_function(inputs)
plt.plot(inputs, outputs)
Out[3]:
In [4]:
train_ratio = 0.8
train_size = int(sample_size* train_ratio)
test_size = sample_size - train_size
train_in = inputs[:train_size].reshape(train_size,1)
train_out = outputs[:train_size].reshape(train_size,1)
test_in = inputs[train_size:].reshape(test_size,1)
test_out = outputs[train_size:].reshape(test_size,1)
In [5]:
def fc_layer(ins, channels_in, channels_out, act=None):
W = tf.Variable(tf.truncated_normal(shape=[channels_in, channels_out]), dtype=tf.float32)
b = tf.Variable(tf.constant(0.1))
if(act):
return act(tf.matmul(ins, W) + b)
else:
return tf.matmul(ins, W) + b
In [6]:
n_hidden = 640
tf.reset_default_graph()
tf_in = tf.placeholder(shape=[None, 1], dtype=tf.float32)
tf_label = tf.placeholder(shape=[None,1], dtype=tf.float32)
h1 = fc_layer(tf_in, 1, n_hidden, tf.nn.relu)
h2 = fc_layer(h1, n_hidden, n_hidden, tf.nn.relu)
h3 = fc_layer(h2, n_hidden, n_hidden, tf.nn.relu)
h4 = fc_layer(h3, n_hidden, n_hidden, tf.nn.relu)
h5 = fc_layer(h4, n_hidden, n_hidden, tf.nn.relu)
ho = fc_layer(h5, n_hidden, 1)
error = tf.nn.l2_loss(ho-tf_label)
train = tf.train.AdamOptimizer(1e-4).minimize(error)
In [7]:
%%time
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
epoch = 10
epoch_size = 1000
n_iter = epoch * epoch_size
batch_size = 50
for it in range(0, n_iter+1):
train.run(feed_dict={tf_in: train_in, tf_label:train_out})
if it % epoch_size == 0:
print('Epoch: {}, L2 Loss: {}' .format(int(it/epoch_size), error.eval(feed_dict={tf_in:train_in, tf_label:train_out})))
In [18]:
ans_train = ho.eval(feed_dict={tf_in:train_in, tf_label:train_out})
print('Train L2 Loss: {}' .format(error.eval(feed_dict={tf_in:train_in, tf_label:train_out})))
plt.plot(train_in, train_out, label='Actual')
plt.plot(train_in, ans_train, label='Prediction')
plt.legend()
Out[18]:
In [10]:
ans_test = ho.eval(feed_dict={tf_in:test_in, tf_label:test_out})
print('Test L2 Loss: {}' .format(error.eval(feed_dict={tf_in:test_in, tf_label:test_out})))
plt.plot(test_in, test_out)
plt.plot(test_in, ans_test)
Out[10]:
In [ ]: