Last active
May 8, 2018 14:37
-
-
Save maple3142/9bc7299c942250ebc09b93f6e78c19d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://blog.gtwang.org/statistics/tensorflow-google-machine-learning-software-library-tutorial/ | |
| # 移除警告 | |
| import os | |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | |
| # y=2x+1 | |
| x_data=[1,2,3,4,5] | |
| y_data=[3,5,7,9,11] | |
| import tensorflow as tf | |
| # 先給它假的 a,b | |
| a=tf.Variable(0.1,dtype=tf.float32) | |
| b=tf.Variable(-0.1,dtype=tf.float32) | |
| # x,y | |
| x=tf.placeholder(tf.float32) | |
| y=tf.placeholder(tf.float32) | |
| # 定義計算的方程式 | |
| line=a*x+b | |
| # loss 是差值平方 | |
| loss=tf.reduce_sum(tf.square(line-y)) | |
| # 最小化 loss | |
| optimizer = tf.train.GradientDescentOptimizer(0.01) | |
| train = optimizer.minimize(loss) | |
| # 初始化 | |
| init = tf.global_variables_initializer() | |
| with tf.Session() as sess: | |
| sess.run(init) | |
| for i in range(1000): | |
| sess.run(train,{x:x_data,y:y_data}) | |
| # a,b 應該會趨近於 2,1 | |
| print(sess.run([a,b])) | |
| # 趨近於 2*6+1=13 | |
| print(sess.run(line,{x:6})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://kakack.github.io/2017/06/TensorFlow%E8%A7%A3%E5%86%B3XOR%E9%97%AE%E9%A2%98/ | |
| # 移除警告 | |
| import os | |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | |
| # y=2x+1 | |
| x_data=[[0,0],[0,1],[1,0],[1,1]] | |
| y_data=[[0],[1],[1],[0]] | |
| import tensorflow as tf | |
| x_=tf.placeholder(tf.float32,shape=(4,2)) | |
| y_=tf.placeholder(tf.float32,shape=(4,1)) | |
| HU=3 | |
| w1=tf.Variable(tf.random_uniform([2, HU], -1.0, 1.0)) | |
| b1=tf.Variable(tf.zeros([HU])) | |
| O=tf.nn.sigmoid(tf.matmul(x_, w1) + b1) | |
| w2=tf.Variable(tf.random_uniform([HU, 1], -1.0, 1.0)) | |
| b2=tf.Variable(tf.zeros([1])) | |
| y=tf.nn.sigmoid(tf.matmul(O, w2) + b2) | |
| # 最小化 loss | |
| optimizer=tf.train.GradientDescentOptimizer(1) | |
| loss=tf.reduce_sum(tf.square(y_ - y), reduction_indices=[0]) | |
| train=optimizer.minimize(loss) | |
| with tf.Session() as sess: | |
| # 初始化 | |
| sess.run(tf.global_variables_initializer()) | |
| for i in range(2000): | |
| sess.run(train,{x_:x_data,y_:y_data}) | |
| print(sess.run(y,{x_:x_data})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment