Skip to content

Instantly share code, notes, and snippets.

@panchishin
Last active May 21, 2021 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panchishin/a0a6bd3b9119aba6818e95908c54aa49 to your computer and use it in GitHub Desktop.
Save panchishin/a0a6bd3b9119aba6818e95908c54aa49 to your computer and use it in GitHub Desktop.
experiment_for_msmits.py
# -- imports --
import numpy as np
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# np.set_printoptions(precision=2) reduces np precision output to 2 digit
np.set_printoptions(precision=2, suppress=True)
# -- constant data --
x = [[0., 0.], [1., 1.], [1., 0.], [0., 1.], [-1., -1.], [-1., 0.], [0., -1.]]
y_ = [[0.], [0.], [1.], [1.], [0.], [1.], [1.]]
# -- induction --
# 1x2 input -> 2x5 hidden sigmoid -> 5x1 sigmoid output
SIZE = 5
# Layer 0 = the x2 inputs
x0 = tf.constant(x, dtype=tf.float32)
y0 = tf.constant(y_, dtype=tf.float32)
# Layer 1 = the 2x5 hidden sigmoid
m1 = tf.Variable(tf.random_uniform([2, SIZE], minval=-0.5, maxval=0.5, dtype=tf.float32))
b1 = tf.Variable(tf.random_uniform([SIZE], minval=-0.5, maxval=0.5, dtype=tf.float32))
h1 = tf.sigmoid(tf.matmul(x0, m1) + b1)
# Layer 2 = the 5x1 sigmoid output
m2 = tf.Variable(tf.random_uniform([SIZE, 1], minval=-0.5, maxval=0.5, dtype=tf.float32))
b2 = tf.Variable(tf.random_uniform([1], minval=-0.5, maxval=0.5, dtype=tf.float32))
y_out = tf.sigmoid(tf.matmul(h1, m2) + b2)
# -- loss --
# loss : sum of the squares of y0 - y_out
loss = tf.reduce_sum(tf.square(y0 - y_out))
# training step : gradient descent (1.0) to minimize loss
train = tf.train.GradientDescentOptimizer(1.0).minimize(loss)
# -- training --
# run 2500 times using all the X and Y
# print out the loss and any other interesting info
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print("\nloss")
for step in range(2500):
sess.run(train)
if (step + 1) % 100 == 0:
print(sess.run(loss))
results = sess.run([m1, b1, m2, b2, y_out, loss])
labels = "m1,b1,m2,b2,y_out,loss".split(",")
for label, result in zip(*(labels, results)):
print("")
print(label)
print(result)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment