Skip to content

Instantly share code, notes, and snippets.

@Jviejo
Last active October 6, 2018 09:09
Show Gist options
  • Save Jviejo/ee8327700e68c8fc35d8cb53b68a0d01 to your computer and use it in GitHub Desktop.
Save Jviejo/ee8327700e68c8fc35d8cb53b68a0d01 to your computer and use it in GitHub Desktop.
#https://github.com/delip/blog-stuff/blob/master/tensorflow_ufp.ipynb
#https://github.com/delip/blog-stuff/blob/master/tensorflow_ufp.ipynb
import tensorflow as tf
import numpy as np
import math, random
np.random.seed(100)
funtion_to_learn = lambda x: x * 2
NODOS = 10
EXAMPLES = 1000
TRAIN_SPLIT = .8
BATCH_SIZE = 1000
EPOCS = 1000
todos = np.random.uniform(-1000, 1000, 1000)
# todos = np.arange(1000)
trainSIZE = int(EXAMPLES * TRAIN_SPLIT)
a = np.split(todos, [trainSIZE])
trainX = a[0].reshape(trainSIZE, 1);
validX = a[1].reshape(EXAMPLES - trainSIZE, 1)
trainY = funtion_to_learn(trainX)
validY = funtion_to_learn(validX)
X = tf.placeholder(tf.float32, [None, 1], name = "X")
Y = tf.placeholder(tf.float32, [None, 1], name = "Y")
w = tf.Variable(tf.zeros([1, NODOS], dtype=tf.float32))
b = tf.Variable(tf.zeros([1, NODOS], dtype=tf.float32))
h = tf.nn.sigmoid(tf.matmul(X , w ) + b)
wo = tf.Variable(tf.zeros([NODOS, 1], dtype=tf.float32))
bo = tf.Variable(tf.zeros([1, 1], dtype=tf.float32))
y_ = tf.matmul(h , wo ) + bo
train_op = tf.train.AdamOptimizer().minimize(tf.nn.l2_loss(y_ - Y))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
errors = []
for i in range(EPOCS):
for start, end in zip(range(0, trainX.size, BATCH_SIZE),
range(BATCH_SIZE, trainX.size, BATCH_SIZE)):
print (start, end)
sess.run(train_op, feed_dict={X: trainX, Y: trainY})
mse = sess.run(tf.nn.l2_loss(y_ - validY), feed_dict={X: validX})
errors.append(mse)
if i%100 == 0: print ("epoch %d, validation MSE %s" % (i, mse))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment