1, 练习
[root@shenzhen ~]# cat /server/tensorflow/tensor1.py #!/usr/local/bin/python3 #coding:utf-8 import tensorflow as tf import numpy as np #create data x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1 + 0.3 ###create tensorflow structure start### Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0)) biases = tf.Variable(tf.zeros([1])) y = Weights*x_data + biases loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) #init = tf.initialize_all_variables() init = tf.global_variables_initializer() ###create tensorflow structure start### sess = tf.Session() sess.run(init) #very important for step in range(201): sess.run(train) if step % 20 == 0: print(step, sess.run(Weights), sess.run(biases)) [root@shenzhen tensorflow]# python3 tensor1.py 2018-08-20 20:58:14.585672: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 0 [0.5822645] [0.00799593] 20 [0.27060172] [0.19762385] 40 [0.16020724] [0.26387033] 60 [0.12124781] [0.28724945] 80 [0.10749861] [0.2955002] 100 [0.10264634] [0.29841197] 120 [0.10093395] [0.29943955] 140 [0.10032961] [0.2998022] 160 [0.10011631] [0.2999302] 180 [0.10004105] [0.2999754] 200 [0.10001447] [0.29999134]