#!/usr/bin/env python # coding: utf-8 # In[1]: import warnings warnings.simplefilter(action='ignore') # In[2]: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # ### 1. 以矩阵运算仿真神经网络 # #### 1.1 y = relu((X * W) + b) # In[3]: X = tf.Variable([[0.4, 0.2, 0.4]]) W = tf.Variable([ [-0.5, -0.2], [-0.3, 0.4], [-0.5, 0.2] ]) b = tf.Variable([[0.1, 0.2]]) result = tf.matmul(X, W) + b y = tf.nn.relu(result) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('result:', sess.run(result)) print('y:', sess.run(y)) # #### 1.2 y = sigmoid((X * W) + b) # In[4]: X = tf.Variable([[0.4, 0.2, 0.4]]) W = tf.Variable([ [-0.5, -0.2], [-0.3, 0.4], [-0.5, 0.2] ]) b = tf.Variable([[0.1, 0.2]]) result = tf.matmul(X, W) + b y = tf.nn.sigmoid(result) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('result:', sess.run(result)) print('y:', sess.run(y)) # #### 1.3 以正态分布的随机数生成权重与偏差的初始值 # In[5]: X = tf.Variable([[0.4, 0.2, 0.4]]) W = tf.Variable(tf.random_normal([3, 2])) b = tf.Variable(tf.random_normal([1, 2])) result = tf.matmul(X, W) + b y = tf.nn.relu(result) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) w_, b_, y_ = sess.run((W, b, y)) print('W:', w_) print('b:', b_) print('y:', y_) # #### 1.4 正态分布的随机数 tf.random_normal # In[6]: normal_array = tf.random_normal([1000]) with tf.Session() as sess: normal_data = sess.run(normal_array) print('normal data length:', len(normal_data)) print(normal_data[:30]) # In[7]: plt.hist(normal_data) plt.show() # ### 2. 以 placeholder 传入 X 值 # #### 2.1 以 placeholder 传入 1*3 的二维数组 # In[8]: X = tf.placeholder('float', [None, 3]) W = tf.Variable(tf.random_normal([3, 2])) b = tf.Variable(tf.random_normal([1, 2])) result = tf.matmul(X, W) + b y = tf.nn.relu(result) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x_array = np.array([[0.4, 0.2, 0.4]]) w_, b_, y_ = sess.run((W, b, y), feed_dict={X: x_array}) print('W:', w_) print('b:', b_) print('y:', y_) # #### 2.2 以 placeholder 传入 3*3 的二维数组 # In[9]: X = tf.placeholder('float', [None, 3]) W = tf.Variable(tf.random_normal([3, 2])) b = tf.Variable(tf.random_normal([1, 2])) result = tf.matmul(X, W) + b y = tf.nn.sigmoid(result) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x_array = np.array([[0.4, 0.2, 0.4], [0.3, 0.4, 0.5], [0.3, -0.4, 0.5]]) w_, b_, y_ = sess.run((W, b, y), feed_dict={X: x_array}) print('W:', w_) print('b:', b_) print('y:', y_) # ### 3. 创建 layer 函数以矩阵运算仿真神经网络 # #### 3.1 定义 layer 函数 # In[10]: def layer(output_dim, input_dim, inputs, activation=None): W = tf.Variable(tf.random_normal([input_dim, output_dim])) b = tf.Variable(tf.random_normal([1, output_dim])) result = tf.matmul(inputs, W) + b if activation is None: outputs = result else: outputs = activation(result) return outputs # In[11]: X = tf.placeholder('float', [None, 4]) y = layer(output_dim=3, input_dim=4, inputs=X, activation=tf.nn.relu) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x_array = np.array([[0.4, 0.2, 0.4, 0.1], [0.3, 0.4, 0.5, 0.3], [0.3, -0.4, 0.5, 0.2]]) x_, y_ = sess.run((X, y), feed_dict={X: x_array}) print('X:', x_) print('y:', y_) # #### 3.2 使用 layer 函数建立3层神经网络 # In[12]: X = tf.placeholder('float', [None, 4]) h = layer(output_dim=3, input_dim=4, inputs=X, activation=tf.nn.relu) y = layer(output_dim=2, input_dim=3, inputs=h) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x_array = np.array([[0.4, 0.2, 0.4, 0.5]]) x_, h_, y_ = sess.run((X, h, y), feed_dict={X: x_array}) print('X:', x_) print('h:', h_) print('y:', y_) # ### 4. 创建 layer_debug 函数显示权重与偏差 # In[13]: def layer_debug(output_dim, input_dim, inputs, activation=None): W = tf.Variable(tf.random_normal([input_dim, output_dim])) b = tf.Variable(tf.random_normal([1, output_dim])) result = tf.matmul(inputs, W) + b if activation is None: outputs = result else: outputs = activation(result) return outputs, W, b # In[14]: X = tf.placeholder('float', [None, 4]) h, W1, b1 = layer_debug(output_dim=3, input_dim=4, inputs=X, activation=tf.nn.relu) y, W2, b2 = layer_debug(output_dim=2, input_dim=3, inputs=h) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x_array = np.array([[0.4, 0.2, 0.4, 0.5]]) x_, h_, y_, w1_, b1_, w2_, b2_ = sess.run((X, h, y, W1, b1, W2, b2), feed_dict={X: x_array}) print('X:', x_) print('h:', h_) print('W1:', w1_) print('b1:', b1_) print('y:', y_) print('W2:', w2_) print('b2:', b2_)