Skip to content

Instantly share code, notes, and snippets.

View hackintoshrao's full-sized avatar
📚
Exploring AI agents on code search and understanding

Karthic Rao hackintoshrao

📚
Exploring AI agents on code search and understanding
View GitHub Profile
@hackintoshrao
hackintoshrao / power_recurse.py
Last active May 23, 2018 12:30
find power by using recursion
def power(x, n):
"""
Compute the value x n for integer n
"""
if n == 0:
return 1
else:
partial = power(x, n // 2)
@hackintoshrao
hackintoshrao / tf_math.py
Created May 7, 2018 03:47
Example for using math functions in tensorflow
import tensorflow as tf
x = tf.constant(10)
y = tf.constant(2)
z = tf.subtract(tf.divide(x,y),tf.cast(tf.constant(1), tf.float64))
with tf.Session() as sess:
output = sess.run(z)
print(output)
@hackintoshrao
hackintoshrao / multiple_feed_dict.py
Created May 7, 2018 03:45
Tensorflow session run with more than one input through feed_dict
import tensorflow as tf
x = tf.placeholder(tf.string)
y = tf.placeholder(tf.int32)
z = tf.placeholder(tf.float32)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Test String', y: 123, z: 45.67})
@hackintoshrao
hackintoshrao / tf_session.py
Created May 7, 2018 03:44
Tensorflow session basics
import tensorflow as tf
x = tf.placeholder(tf.string)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Hello World'})
@hackintoshrao
hackintoshrao / tf_basics_1.py
Created May 7, 2018 03:40
Basics of tensorflow
import tensorflow as tf
# You can create constants in TF to hold specific values
a = tf.constant(1)
b = tf.constant(2)
A = tf.constant(1234)
# B is a 1-dimensional int32 tensor
B = tf.constant([123,456,789])
# C is a 2-dimensional int32 tensor
C = tf.constant([ [123,456,789], [222,333,444] ])
# Adding and multiplying the constants
@hackintoshrao
hackintoshrao / optimizer.py
Created May 6, 2018 12:15
Optimization function evaluation
with tf.Session() as sess:
sess.run(init)
display_step = 20
# Fit all training data
for iteration in range(training_iteration):
# Run the gradient descent optimizer
sess.run(optimizer, feed_dict={X: X_train, Y: Y_train})
@hackintoshrao
hackintoshrao / cost_optimizer.py
Created May 6, 2018 02:13
Linear regression cost function and optimizer in tensorflow
def get_cost_optimizer_tensor(Y, model, size, learning_rate):
"""
Tensor for calculating the cost function and the the optimizer for
minimizing the cost.
"""
# Cost function tensor.
cost_function = tf.reduce_sum(tf.pow(model - Y, 2))/(2 * size)
# gradient descent tensor.
gradient_descent = tf.train.GradientDescentOptimizer(learning_rate)
# optimization tensor.
@hackintoshrao
hackintoshrao / get_model.py
Last active May 6, 2018 02:24
tensorflow model for linear regression
def get_model_tensors():
"""
function obtain model tensor
"""
# X is the placeholder for size of the house from the dataset.
# Y is the placeholder for size of the house from the dataset.
X = tf.placeholder("float")
Y = tf.placeholder("float")
# The parameters theta0 and theta1
@hackintoshrao
hackintoshrao / tf_housing_load_plot.py
Last active May 6, 2018 02:21
tf_housing_load_plot.py
import pandas as pd
# import library to split the data
from sklearn.model_selection import train_test_split
%matplotlib inline
import matplotlib.pyplot as plt
def read_data():
"""
@hackintoshrao
hackintoshrao / Dockerfile
Last active March 11, 2018 04:46
Simple flask server
FROM python:2.7
MAINTAINER Karthic Rao "[email protected]"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]