Created
July 19, 2017 18:59
-
-
Save anonymous/c17d45b4e997bfccb5275dffa44512d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
import math | |
# input | |
X = tf.placeholder(tf.float32, [None, 2], name="X") | |
Y = tf.placeholder(tf.float32, [None, 1], name="Y") | |
# hidden | |
beta = tf.get_variable("beta", shape=[2], initializer=tf.contrib.layers.xavier_initializer()) | |
powered = tf.pow(X,beta) | |
productLayer = tf.contrib.keras.layers.multiply(powered) | |
# output | |
w_o = tf.get_variable("w_o", shape=[2], initializer=tf.contrib.layers.xavier_initializer()) | |
b_o = tf.get_variable("bias", shape=[1], initializer=tf.zeros([1])) | |
output = tf.add(tf.matmul(productLayer,w_o), b_o) | |
loss = tf.reduce_sum(tf.square(output - Y)) # tf.nn.l2_loss(yhat - Y) | |
training_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss) | |
## TRAINING PROCEDURE | |
h2o = lambda h,o: h**2 * o | |
trainIn = np.random.rand(10000, 2) | |
trainOut = h2o(trainIn) | |
validIn = np.random.rand(4000, 2) | |
validOut = h2o(validIn) | |
trainOut = np.reshape(trainOut, (-1, 1)) | |
validOut = np.reshape(validOut, (-1, 1)) | |
mse = math.inf | |
sess = tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
for i in range(1000): | |
if i % 100 == 0: | |
print("epoch {}, validation MSE {}".format((i + 1), mse)) | |
_, mse = sess.run([training_op, loss], feed_dict={X: trainIn, Y: trainOut}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment