In [1]:
import numpy as np
In [2]:
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
n_samples = 1000
In [4]:
x = np.zeros(n_samples)
In [5]:
x[1] = 1
c = np.cos(2*np.pi*0.005)
In [6]:
for i in range(2,n_samples):
x[i]= 2*c*x[i-1] - x[i-2]
In [7]:
plt.plot(x)
Out[7]:
In [8]:
X = np.array([[x[i-1],x[i-2]] for i in range(2,1000)])
Y = np.array([x[i] for i in range(2,1000)])
In [9]:
coeffs_true = np.array([2*c, -1]).reshape(-1,1)
In [10]:
coeffs_true
Out[10]:
In [11]:
import tensorflow as tf
In [12]:
X_tf = tf.placeholder(dtype=tf.float32, shape=[None,2], name="X_tf")
Y_tf = tf.placeholder(dtype=tf.float32, shape=[None,1], name="Y_tf")
In [13]:
c_tf = tf.Variable(tf.random_normal([2,1], stddev=1.0, dtype=tf.float32))
# c_tf = tf.Variable(initial_value=coeffs_true)
In [14]:
loss = tf.reduce_sum(
tf.square(
tf.subtract(
tf.matmul(X_tf,c_tf), Y_tf
)
)
)
In [15]:
train_op = tf.train.AdamOptimizer(learning_rate=0.01, epsilon=1E-12).minimize(loss)
In [18]:
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
In [19]:
loss_list = []
In [20]:
sess.run(c_tf, feed_dict={X_tf:X, Y_tf:Y.reshape(-1,1)})
Out[20]:
In [21]:
for i in range(10000):
sess.run(train_op, feed_dict={X_tf:X, Y_tf:Y.reshape(-1,1)})
loss_val = sess.run(loss, feed_dict={X_tf:X, Y_tf:Y.reshape(-1,1)})
loss_list.append(loss_val)
In [22]:
plt.plot(np.log10(loss_list))
Out[22]:
In [293]:
sess.run(c_tf, feed_dict={X_tf:X, Y_tf:Y.reshape(-1,1)})
Out[293]:
In [23]:
coeffs_true
Out[23]:
The obtained value is close to the target value ! Mission accomplished.
In [ ]: