构建简单的网络

1、低级别的API *

1. # a rank 0 tensor; a scalar with shape [],

[1., 2., 3.] # a rank 1 tensor; a vector with shape [3]

[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]

[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

  x = tf.constant([[37.0, -23.0], [1.0, 4.0]])
  w = tf.Variable(tf.random_uniform([2, 2]))
  y = tf.matmul(x, w)
  output = tf.nn.softmax(y)
  init_op = w.initializer

  with tf.Session() as sess:
    # Run the initializer on `w`.
    sess.run(init_op)

    # Evaluate `output`. `sess.run(output)` will return a NumPy array containing
    # the result of the computation.
    print(sess.run(output))

    # Evaluate `y` and `output`. Note that `y` will only be computed once, and its
    # result used both to return `y_val` and as an input to the `tf.nn.softmax()`
    # op. Both `y_val` and `output_val` will be NumPy arrays.
    y_val, output_val = sess.run([y, output])


2、构建一个简单的神经网络

  import tensorflow as tf

  #在 Tensorflow 中需要定义 placeholder 的 type ,一般为 float32 形式
  input1 = tf.placeholder(tf.float32)
  input2 = tf.placeholder(tf.float32)

  # mul = multiply 是将input1和input2 做乘法运算,并输出为 output 
  ouput = tf.multiply(input1, input2)    

  with tf.Session() as sess:
      print(sess.run(ouput, feed_dict={input1: [7.], input2: [2.]}))
  # [ 14.]
  def add_layer(inputs, in_size, out_size, activation_function=None):
      #1 参数的初始化
      Weights = tf.Variable(tf.random_normal([in_size, out_size]))
      #生成初始参数时,最好用一个随机变量,这里的权值为一个in_size行,out_size列的随机变量矩阵
      Biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
      #机器学习中,偏置的推荐值不为0,因此这里是在0向量的基础上增加了0.1
      
      #2 神经层的输出
      Wx_plus_b = tf.matmul(inputs, Weights) + Biases
      #定义神经网络的默认值,matmul为矩阵乘法函数
      if activation_function is None:
          outputs = Wx_plus_b
      else:
          outputs = activation_function(Wx_plus_b)
      #如果激活函数为None,那么outputs = Wx_plus_b,否则outputs为激活函数对Wx_plus_b的响应值
      return outputs
  train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
  #定义tf要如何学习,这里采用了梯度下降法,选取学习效率为0.1,目标为最小化误差loss

  init = tf.global_variables_initializer()
  #使用变量时,需要对它们进行初始化,这一函数可以一次性初始化所有变量

  sess = tf.Session()
  sess.run(init)
  #定义Session,并进行变量的初始化工作