Coding according to TensorFlow 官方文档中文版
1 import tensorflow as tf 2 import numpy as np 3 4 5 ''' Intro. for this python file. 6 Objective: 7 Implement for generating some 3-dimensional phony data and fitting them with a plane. 8 Operating Environment: 9 python = 3.6.4 10 tensorflow = 1.5.0 11 numpy = 1.15.1 12 ''' 13 14 15 # Generate phony data using NumPy. There is totally 100 points. 16 ''' numpy.random.rand(d0, d1, ..., dn) 17 Explanation: 18 Random values in a given shape. 19 Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). 20 ''' 21 ''' numpy.dot(a, b, out=None) 22 Explanation: 23 Dot product of two arrays. 24 ''' 25 x_data = np.float32(np.random.rand(2, 100)) 26 y_data = np.dot([0.100, 0.200], x_data) + 0.300 27 28 29 # Generate a linear model. 30 ''' tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None) 31 Explanation: 32 shape: A 1-D integer Tensor or Python array. The shape of the output tensor. 33 minval: A 0-D Tensor or Python value of type dtype. The lower bound on the range of random values to generate. 34 Defaults to 0. 35 maxval: A 0-D Tensor or Python value of type dtype. The upper bound on the range of random values to generate. 36 Defaults to 1 if dtype is floating point. 37 dtype: The type of the output: float16, float32, float64, int32, or int64. 38 seed: A Python integer. Used to create a random seed for the distribution. See tf.set_random_seed for behavior. 39 name: A name for the operation (optional). 40 ''' 41 ''' tf.zeros(shape, dtype=tf.float32, name=None) 42 Explanation: 43 shape: A list of integers, a tuple of integers, or a 1-D Tensor of type int32. 44 dtype: The type of an element in the resulting Tensor. 45 name: A name for the operation (optional). 46 ''' 47 W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) 48 b = tf.Variable(tf.zeros([1])) 49 y = tf.matmul(W, x_data) + b 50 51 # Minimize variance 52 ''' tf.square(x, name=None) 53 Explanation: 54 Computes square of x element-wise. 55 ''' 56 ''' tf.reduce_mean(input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None) 57 Explanation: 58 input_tensor: The tensor to reduce. Should have numeric type. 59 axis: The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range 60 [-rank(input_tensor), rank(input_tensor)]. 61 keepdims: If true, retains reduced dimensions with length 1. 62 name: A name for the operation (optional). 63 reduction_indices: The old (deprecated) name for axis. 64 keep_dims: Deprecated alias for keepdims. 65 ''' 66 loss = tf.reduce_mean(tf.square(y - y_data)) 67 optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5) 68 train = optimizer.minimize(loss) 69 70 # Initialize variables 71 init = tf.initialize_all_variables() 72 73 # Launch the graph in a session. 74 sess = tf.Session() 75 sess.run(init) 76 77 # Fitting 78 for step in range(0, 201): 79 sess.run(train) 80 if step % 20 == 0: 81 print(step, sess.run(W), sess.run(b)) 82 83 # The best fitting result: W = [[0.10000069 0.20000069]], b = [0.29999927]