msyim / tensorflowfun Goto Github PK
View Code? Open in Web Editor NEWMe playing with TF
Me playing with TF
Method 1 in the code works fine:
# Method 1
hypothesis = tf.matmul(X,W) + b
cost = tf.nn.sigmoid_cross_entropy_with_logits(hypothesis, Y)
optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
However, the second method (which I believe is essentially the same as the first) doesn't seem to work:
# Method 2
hypothesis2 = tf.sigmoid(tf.matmul(X,W) + b)
cost2 = -tf.reduce_mean(Y * tf.log(hypothesis2) + (1-Y)*tf.log(1-hypothesis2))
optimizer2 = tf.train.GradientDescentOptimizer(0.001).minimize(cost2)
predicted2 = tf.cast(hypothesis2 > 0.5, dtype=tf.float32)
accuracy2 = tf.reduce_mean(tf.cast(tf.equal(predicted2, Y), dtype=tf.float32))
I get cost: nan
Need to investigate further on this issue....
It seemed like the model doesn't work when the input data has dimension >= 2:
x_train = [[1.,2.],[3.,4.]]
y_train = [4.,8.]
# Build a graph
X = tf.placeholder(tf.float32, shape=[None,2])
Y = tf.placeholder(tf.float32, shape=[None,1])
W = tf.Variable(tf.random_normal([2,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = tf.matmul(X,W) + b
cost = tf.square(hypothesis - Y)
Clearly, the desired weight matrix is : [[1],[1]] and the bias : [1], but the model always converged to a point where the output is ~ [6,6] (and consequently, the loss ~ 4)
Studied the cost matrix whose dimension should be (2,1) but with the above code, the cost matrix had dimension: (2,2).
There was a problem in defining the y_train array. I had to change y_train to:
y_train = [[4.],[8.]]
Tensorflow does not seem to implicitly change the dimension as necessary (which is good)
For some reason, BN is not really helping ( in fact, NN using BN has worse performance than the ones not using BN). Will further investigate why.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.