More actions
imported>rabierre No edit summary |
(Repair batch-0005 pages from live compare) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 2: | Line 2: | ||
[[머신러닝스터디/2016/목차]] | [[머신러닝스터디/2016/목차]] | ||
== 내용 == | == 내용 == | ||
* Basic | * Basic Logic Gate만들어보자! | ||
** AND, OR, NXOR, XOR | ** AND, OR, NXOR, XOR | ||
=== 코드 === | === 코드 === | ||
| Line 12: | Line 12: | ||
# (1, 1) => 1 (1, 1) => 1 (1, 1) => 1 (1, 1) => 0 | # (1, 1) => 1 (1, 1) => 1 (1, 1) => 1 (1, 1) => 0 | ||
W1 = tf.Variable(tf.random_uniform( | W1 = tf.Variable(tf.random_uniform([2, 2])) | ||
b1 = tf.Variable(tf.random_uniform( | b1 = tf.Variable(tf.random_uniform([2])) | ||
W2 = tf.Variable(tf.random_uniform( | W2 = tf.Variable(tf.random_uniform([2, 1])) | ||
b2 = tf.Variable(tf.random_uniform( | b2 = tf.Variable(tf.random_uniform([1])) | ||
def logic_gate(x): | def logic_gate(x): | ||
| Line 22: | Line 22: | ||
return tf.sigmoid(tf.matmul(hidden, W2) + b2) | return tf.sigmoid(tf.matmul(hidden, W2) + b2) | ||
x = tf.placeholder("float", | x = tf.placeholder("float", [None, 2]) | ||
y = tf.placeholder("float", | y = tf.placeholder("float", [None, 1]) | ||
value = logic_gate(x) | value = logic_gate(x) | ||
| Line 35: | Line 35: | ||
sess.run(init) | sess.run(init) | ||
for i in range(30001): | for i in range(30001): | ||
result = sess.run(optimize, feed_dict={x: | result = sess.run(optimize, feed_dict={x: [[0, 0], [0, 1], [1, 0], [1, 1]], y: [[1], [0], [0], [1]]}) | ||
if (i % 1000 == 0): | if (i % 1000 == 0): | ||
print("Epoch: ", i) | print("Epoch: ", i) | ||
print(sess.run( | print(sess.run([value, loss], feed_dict={x: [[0, 0], [0, 1], [1, 0], [1, 1]], y: [[1], [0], [0], [1]]})) | ||
== 후기 == | == 후기 == | ||
== 다음 시간에는 == | == 다음 시간에는 == | ||
* ML Week 5 Back Propagation 실습 | * ML Week 5 Back Propagation 실습 | ||
== 더 보기 == | == 더 보기 == | ||
Latest revision as of 00:44, 27 March 2026
내용
- Basic Logic Gate만들어보자!
- AND, OR, NXOR, XOR
코드
import tensorflow as tf
# AND OR NXOR XOR
# (0, 0) => 0 (0, 0) => 0 (0, 0) => 1 (0, 0) => 0
# (0, 1) => 0 (0, 1) => 1 (0, 1) => 0 (0, 1) => 1
# (1, 0) => 0 (1, 0) => 1 (1, 0) => 0 (1, 0) => 1
# (1, 1) => 1 (1, 1) => 1 (1, 1) => 1 (1, 1) => 0
W1 = tf.Variable(tf.random_uniform([2, 2]))
b1 = tf.Variable(tf.random_uniform([2]))
W2 = tf.Variable(tf.random_uniform([2, 1]))
b2 = tf.Variable(tf.random_uniform([1]))
def logic_gate(x):
hidden = tf.sigmoid(tf.matmul(x, W1) + b1)
return tf.sigmoid(tf.matmul(hidden, W2) + b2)
x = tf.placeholder("float", [None, 2])
y = tf.placeholder("float", [None, 1])
value = logic_gate(x)
// loss = tf.reduce_sum(tf.pow(y-value, 2))
loss = - tf.reduce_mean(y*tf.log(value) + (1-y)*tf.log(1-value))
optimize = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(30001):
result = sess.run(optimize, feed_dict={x: [[0, 0], [0, 1], [1, 0], [1, 1]], y: [[1], [0], [0], [1]]})
if (i % 1000 == 0):
print("Epoch: ", i)
print(sess.run([value, loss], feed_dict={x: [[0, 0], [0, 1], [1, 0], [1, 1]], y: [[1], [0], [0], [1]]}))
후기
다음 시간에는
- ML Week 5 Back Propagation 실습