[Tensorflow]激勵函數tf.nn.relu樣例

 

代碼:
import tensorflow as tf
import numpy as np
### 定義添加神經網絡層函數 START ###

def add_layer(inputs,in_size,out_size,activation_function=None): 
    """描述:  添加神經網絡層函數.
    :param inputs:  輸入神經層
    :param in_size: 輸入神經層的神經元個數
    :param out_size:    輸出神經層的神經元個數
    :param activation_function: 激勵函數
    """
    
    # 定義一個"in_size行,out_size列"的隨機矩陣變量
    Weights=tf.Variable(tf.random_normal([in_size,out_size]))

    # 定義一個"1行,out_size列"的0值矩陣基準變量
    biases=tf.Variable(tf.zeros([1,out_size])+0.1)

    # 定義一個矩陣乘法函數公式
    Wx_plus_b = tf.matmul(inputs,Weights)+biases

    # 判斷是否使用激勵函數
    if activation_function is None:
        outputs=Wx_plus_b
    else:
        outputs=activation_function(Wx_plus_b)
    return outputs
### 定義添加神經網絡層函數 END ###


### 定義變量結構 START###

# 定義起始輸入:在指定的-1到1的間隔內返回300個均勻間隔的1行300列的數組,再將數組轉化爲1列300行的矩陣
#   例如:
#   x1 = np.array([1, 2, 3, 4, 5])
#   #   the shape of x1 is (5,)
#   x1_new = x1[:, np.newaxis]
#   #   now, the shape of x1_new is (5, 1)
#   array([[1],
#          [2],
#          [3],
#          [4],
#          [5]])
#   x1_new = x1[np.newaxis,:]
#   #   now, the shape of x1_new is (1, 5)
#   array([[1, 2, 3, 4, 5]])

x_data=np.linspace(-1,1,300)[:,np.newaxis]

# 定義噪點 :使用高斯分佈的機率密度函數定義一個均值爲0,標準差爲0.05的高斯隨機數,個數爲x_data的矩陣元素數
noise =np.random.normal(0,0.05,x_data.shape)

# 定義起始輸出:x_data的平方減去0.5,再加上噪點
y_data=np.square(x_data)-0.5+noise


# 定義運行時參數變量
xs=tf.placeholder(tf.float32,[None,1])
ys=tf.placeholder(tf.float32,[None,1])

### 定義神經網絡結構    START###

# 定義隱藏層神經網絡層layer01
layer01=add_layer(xs,1,10,activation_function=tf.nn.relu)
# 定義隱藏層神經網絡層layer02
layer02=add_layer(layer01,10,10,activation_function=tf.nn.sigmoid)
# 定義預測輸出層 prediction
prediction =add_layer(layer02,10,1,activation_function=None)
# 計算損失
# 1.計算起始輸出與預測輸出的誤差的平方
loss_square=tf.square(y_data - prediction)
# 2.計算一個張量的各個維度上元素的總和.
reduce_sum_square=tf.reduce_sum(loss_square,reduction_indices=[1])
# 3.計算損失:張量的各個維度上的元素的平均值
loss=tf.reduce_mean(reduce_sum_square)

#使用梯度降低算法訓練全部樣本
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 定義初始化變量
init=tf.initialize_all_variables()
# 建立會話
sess=tf.Session()
# 運行初始化變量指針
sess.run(init)

### 定義神經網絡結構    END###

###定義變量結構  END###

for i in range(2000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if i%50==0:
        print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))

 

輸出結果:
> Executing task: python d:\Work\002_WorkSpace\VSCode\Tensorflow\cnn.py <

WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\framework
\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\ops
\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.cast instead. WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\util
\tf_should_use.py:193: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 2019-06-16 18:23:25.445771: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports
instructions that this TensorFlow binary was not compiled to use: AVX2
0.9150444 0.018474927 0.012227052 0.008430008 0.006330067 0.005174632 0.0045147026 0.004099458 0.0037936615 0.0035521714 0.0033668855 0.003235288 0.0031796119 0.003798308 0.011472862 0.011122204 0.0038715526 0.0029777498 0.00284954 0.0028072707 0.0027813027 0.0027617016 0.0027467846 0.0027342557 0.0027231644 0.0027126905 0.0027037202 0.0026956936 0.0026887206 0.0026827992 0.0026773391 0.0026706234 0.0026643125 0.0026575066 0.0026512532 0.00264405 0.0026368005 0.0026302505 0.0026243015 0.0026188325 Terminal will be reused by tasks, press any key to close it.
相關文章
相關標籤/搜索