tf.reset_default_graph()
import tensorflow as tf
import numpy as np
import tensorboard as tb
import tensorflow as tf
import numpy as np
import tensorboard as tb
#placeholder宣言
x = tf.placeholder(tf.float32, name='x')
t = tf.placeholder(tf.float32, name='t')
x = tf.placeholder(tf.float32, name='x')
t = tf.placeholder(tf.float32, name='t')
# 変数宣言
with tf.name_scope('variables'):
W = tf.Variable(np.random.uniform(low=-0.07, high=0.07, size=(2, 1)).astype('float32'), name='W') # 2行1列
b = tf.Variable(np.zeros(1).astype('float32'), name='b')
with tf.name_scope('variables'):
W = tf.Variable(np.random.uniform(low=-0.07, high=0.07, size=(2, 1)).astype('float32'), name='W') # 2行1列
b = tf.Variable(np.zeros(1).astype('float32'), name='b')
# モデルの設定
with tf.name_scope('model'):
y = tf.nn.sigmoid(tf.matmul(x, W) + b, name='y')
with tf.name_scope('model'):
y = tf.nn.sigmoid(tf.matmul(x, W) + b, name='y')
# E = 1/N・Σn=0->N-1{t_n・logy_n + (1 -t_n)log(1 -y_n)}
with tf.name_scope('train'):
cost = -tf.reduce_mean(t*tf.log(tf.clip_by_value(y, 1e-10, 1.0)) + (1 - t)*tf.log(tf.clip_by_value(1 - y, 1e-10, 1.0)))
with tf.name_scope('train'):
cost = -tf.reduce_mean(t*tf.log(tf.clip_by_value(y, 1e-10, 1.0)) + (1 - t)*tf.log(tf.clip_by_value(1 - y, 1e-10, 1.0)))
with tf.name_scope('gradients'):
dW, db = tf.gradients(cost, [W, b])
updates = [
W.assign_add(-0.01*dW),
b.assign_add(-0.01*db)
]
dW, db = tf.gradients(cost, [W, b])
updates = [
W.assign_add(-0.01*dW),
b.assign_add(-0.01*db)
]
train = tf.group(*updates)
# OR(どちらかが1なら1、どちらも0のときのみ0)
train_X = np.array([[0, 1], [1, 0], [0, 0], [1, 1]])
train_y = np.array([[1], [1], [0], [1]])
# OR(どちらかが1なら1、どちらも0のときのみ0)
train_X = np.array([[0, 1], [1, 0], [0, 0], [1, 1]])
train_y = np.array([[1], [1], [0], [1]])
# 学習(10000回の繰り返し、1000回ごとに誤差を表示)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10000):
_cost, _ = sess.run([cost, train], feed_dict={x: train_X, t: train_y})
if (i+1)%1000==0:
print(_cost)
tf.summary.FileWriter('./log/', sess.graph) # Tensorboad用に logフォルダにログを作成
tb.show_graph(tf.get_default_graph().as_graph_def()) # グラフの表示
