Skip to content

Commit 5b07301

Browse files
committed
new tensorflow examples
1 parent 2a3801d commit 5b07301

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

machinelearning.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
</hook>
8484
<hook NAME="AutomaticEdgeColor" COUNTER="10" RULE="ON_BRANCH_CREATION"/>
8585
<hook NAME="accessories/plugins/AutomaticLayout.properties" VALUE="ALL"/>
86-
<node TEXT="model" POSITION="right" ID="ID_1486859491" CREATED="1489148242353" MODIFIED="1489149116450" HGAP_QUANTITY="14.749999977648258 pt" VSHIFT_QUANTITY="-58.49999825656419 pt">
86+
<node TEXT="model" POSITION="right" ID="ID_1486859491" CREATED="1489148242353" MODIFIED="1490358639221" HGAP_QUANTITY="14.749999977648258 pt" VSHIFT_QUANTITY="-58.49999825656419 pt">
8787
<edge COLOR="#00ffff"/>
88-
<node TEXT="regression (fit Y = M(X))" ID="ID_848707408" CREATED="1489148691975" MODIFIED="1489150550042">
88+
<node TEXT="regression (fit Y = M(X))" ID="ID_848707408" CREATED="1489148691975" MODIFIED="1490358639217" HGAP_QUANTITY="14.749999977648258 pt" VSHIFT_QUANTITY="-26.99999919533732 pt">
8989
<node TEXT="nearest neighbor" ID="ID_192120717" CREATED="1489150509095" MODIFIED="1489150523538"/>
9090
<node TEXT="decision tree" ID="ID_1981317805" CREATED="1489150524625" MODIFIED="1489150527641"/>
9191
<node TEXT="Lasso regression" ID="ID_503979546" CREATED="1489150558163" MODIFIED="1489150564677"/>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf8 -*-
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
import tensorflow as tf
6+
7+
8+
def main():
9+
# ---- dataset ----
10+
npoints = 400
11+
x0 = np.linspace(0., 10 * np.pi, npoints)
12+
noise = np.random.normal(scale=0.1, size=npoints)
13+
y0 = np.sin(x0) + noise
14+
# y0 = noise
15+
16+
# ---- definitions ----
17+
# Model
18+
x = tf.placeholder(tf.float32, [None], name='x')
19+
y_label = tf.placeholder(tf.float32, [None], name='y')
20+
21+
m = tf.Variable(tf.ones([1]))
22+
b = tf.Variable(tf.ones([1]))
23+
24+
y = m * x + b
25+
26+
# cost function
27+
cost = tf.reduce_mean(tf.square(y_label - y))
28+
29+
# optimizer
30+
stepsize = 0.001
31+
optimizer = tf.train.GradientDescentOptimizer(stepsize).minimize(cost)
32+
33+
# ---- run tensorflow ----
34+
init = tf.initialize_all_variables()
35+
36+
with tf.Session() as sess:
37+
sess.run(init)
38+
39+
for isample in range(10000):
40+
optimizer.run(feed_dict={x: x0, y_label: y0})
41+
42+
y_predicted = sess.run(y, feed_dict={x: x0})
43+
m_optimized = sess.run(m)
44+
b_optimized = sess.run(b)
45+
46+
# write graph file
47+
writer = tf.train.SummaryWriter('./', sess.graph)
48+
writer.close()
49+
50+
print(m_optimized, b_optimized)
51+
# ---- plotting ----
52+
fig, axes = plt.subplots()
53+
axes.plot(x0, y0)
54+
axes.plot(x0, y_predicted)
55+
56+
plt.show()
57+
58+
59+
if __name__ == "__main__":
60+
main()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf8 -*-
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
import tensorflow as tf
6+
7+
8+
def main():
9+
# ---- dataset ----
10+
npoints = 400
11+
x0 = np.linspace(0., 10 * np.pi, npoints)
12+
noise = np.random.normal(scale=0.1, size=npoints)
13+
y0 = (np.sin(x0) + noise)
14+
# y0 = noise
15+
16+
# ---- definitions ----
17+
# Model
18+
nnetwork = 400
19+
20+
x = tf.placeholder(tf.float32, [None, 1], name='x')
21+
y_label = tf.placeholder(tf.float32, [None, 1], name='y')
22+
23+
W1 = tf.Variable(tf.random_uniform([1, nnetwork], -1.0, 1.0))
24+
b1 = tf.Variable(tf.zeros([nnetwork]))
25+
26+
h1 = tf.nn.tanh(tf.matmul(x, W1) + b1)
27+
28+
29+
W2 = tf.Variable(tf.random_uniform([nnetwork, 1], -nnetwork, nnetwork))
30+
b2 = tf.Variable(tf.zeros([1]))
31+
32+
y = tf.matmul(h1, W2) + b2
33+
34+
# cost function
35+
cost = tf.reduce_mean(tf.square(y_label - y))
36+
37+
# optimizer
38+
stepsize = 0.001
39+
optimizer = tf.train.GradientDescentOptimizer(stepsize).minimize(cost)
40+
41+
# ---- run tensorflow ----
42+
init = tf.initialize_all_variables()
43+
44+
with tf.Session() as sess:
45+
sess.run(init)
46+
47+
for isample in range(10000):
48+
optimizer.run(feed_dict={x: x0[:, None], y_label: y0[:, None]})
49+
50+
y_predicted = sess.run(y, feed_dict={x: x0[:, None]})
51+
52+
# write graph file
53+
writer = tf.train.SummaryWriter('./', sess.graph)
54+
writer.close()
55+
56+
# ---- plotting ----
57+
fig, axes = plt.subplots()
58+
axes.plot(x0, y0, 'o')
59+
axes.plot(x0, y_predicted)
60+
61+
plt.show()
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)