#libs import tensorflow as tf; import matplotlib.pyplot as pyplot; #data X = [[0,0],[0,1],[1,0],[1,1]]; Y = [[0], [1], [1], [0] ]; Batch_Size = 4; #model Input = tf.placeholder(dtype=tf.float32, shape=[Batch_Size,2]); Expected = tf.placeholder(dtype=tf.float32, shape=[Batch_Size,1]); Weight1 = tf.Variable(tf.random_uniform(shape=[2,20], minval=-1, maxval=1)); Bias1 = tf.Variable(tf.random_uniform(shape=[ 20], minval=-1, maxval=1)); Hidden1 = tf.nn.relu(tf.matmul(Input,Weight1) + Bias1); Weight2 = tf.Variable(tf.random_uniform(shape=[20,1], minval=-1, maxval=1)); Bias2 = tf.Variable(tf.random_uniform(shape=[ 1], minval=-1, maxval=1)); Output = tf.sigmoid(tf.matmul(Hidden1,Weight2) + Bias2); Loss = tf.reduce_sum(tf.square(Expected-Output)); Optimiser = tf.train.GradientDescentOptimizer(1e-1); Training = Optimiser.minimize(Loss); #train Sess = tf.Session(); Init = tf.global_variables_initializer(); Sess.run(Init); Losses = []; for I in range(1000): if (I%100==0): Lossvalue = Sess.run(Loss, feed_dict={Input:X, Expected:Y}); Losses += [Lossvalue]; print("Loss:",Lossvalue); #end if Sess.run(Training, feed_dict={Input:X, Expected:Y}); #end for Lastloss = Sess.run(Loss, feed_dict={Input:X, Expected:Y}); Losses += [Lastloss]; print("Loss:",Lastloss,"(Last)"); pyplot.plot(Losses); #eof
Colab link:
https://colab.research.google.com/drive/1pWz4kN_ZP92LW1csQDIUHb8SY3b7UFqa
No comments:
Post a Comment