[ARCHIVE] Learning High School Physics with Tensorflow
Task
What we want to do is learn the mapping between the angular rotation (complete circle) of a circular object given its diameter to the distance covered on a linear path
So we want to find the length of the underlying line that the circle covers while completing a full rotation.
What we already know
From highschool, we already know the formula to calculate this function.
which yields to
So we want our Neural network to learn the parameter a = 6.283185
Dataset
Since we already have the mapping from input to output, then we can generate the dataset ourselves.
import numpy as np
import math
dataset_size = 200
def generate_dataset():
radii = np.random.uniform(-20, 20, size=dataset_size)
np.random.shuffle(radii)
distance_covered_by_one_lap = 2.0 * math.pi * radii
return radii , distance_covered_by_one_lap
We are going to consider the distance covered by 1 full rotation of circles with different radii
Since the function is linear, then the curve will be a straight line.
Model
We are going to use a very simple neural network. Actually it’s not going to be a network, just 1 neuron.
Code
X = tf.placeholder(dtype=tf.float32,shape=(dataset_size,),name = 'Input_X')
Y = tf.placeholder(dtype=tf.float32,shape=(dataset_size,),name = 'Y')
def step(X, weight):
h = tf.multiply(X,weight)
a = tf.nn.relu(h)
return a
yhat = step(X,w)
cost = tf.reduce_sum(tf.pow(tf.subtract(yhat,Y), 2))/(dataset_size)
train = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess :
sess.run(init)
print(w.eval())
for i in range(100):
sess.run(train,feed_dict = {
X : radii,
Y : distance
})
print(w.eval())
While training, after just a few iterations, we will see that the network converged and the scalar w we are training is about 6.2831855
which is the desired value