【Tensorflow試料励起関数tf.nn.relu

 

コード:
インポートAS tensorflowのTF
 インポートNPのAS numpyの
 ##定義されたニューラルネットワーク層の機能は、### START追加

DEFの add_layerを(入力、IN_SIZE、OUT_SIZE、activation_function = なし): 
     "" " 説明:機能ニューラルネットワークレイヤを追加します。
    :のparam入力:求心性神経層
    :PARAMのIN_SIZE:神経の入力層のニューロンの数
    :PARAM OUT_SIZE:出力層の神経におけるニューロンの数
    :PARAM activation_function:活性化関数
    「」「
    
    #はIN_SIZE行定義」、OUT_SIZE列「確率変数行列 
    =重みtf.Variable(tf.random_normal([IN_SIZE、OUT_SIZE])) 

    "行1、OUT_SIZE列" 0基準変数値行列の定義 
    バイアス= tf.Variable(tf.zeros([1 、OUT_SIZE])+を0.1 

    #は、行列乗算関数式を定義します
    = Tf.matmul Wx_plus_b(入力、重み)+ バイアスの

    励起関数か否かを判断する
    IF activation_functionはしないなし:
        出力 = Wx_plus_b
     
        出力 = activation_function(Wx_plus_b)が
     返す出力
 ##定義されたニューラルネットワーク層###の機能ENDの追加


##スタート###の可変構造を定義

#は開始入力を定義する:300行指定-1 1に等間隔の300の配列を返し、その後、マトリクスの300行のアレイに変換   例えば:   X1 = np.array([1、2 ,. 3 ,. 4 ,. 5。])    #X1の形状は、IS(5)    x1_new = X1の[:、np.newaxis]    今#、x1_newの形状が(5 ,. 1)
#1    アレイ([1]、           [2]、
#1           [3]、           [4]、
#1           [5])    x1_new = X1 [np.newaxis 、:】
#1    #は現在、x1_newの形状は(1 ,. 5)
#1    アレイ([[1、2 ,. 3 ,. 4 ,. 5。]])

x_data = np.linspace(-1,1,300 ):、NP。 newaxis] 

ノイズを定義する:確率密度関数のガウス分布を用いてガウス乱数0.05、0の平均および標準偏差が定義され、数がx_data行列要素の数であり、 
ノイズnp.random.normal =(0、0.05 、x_data。形状)

#は、起動出力を定義:x_data二乗マイナス0.5、プラス雑音 
y_data = np.square(x_data)-0.5+ ノイズ


#は、実行時パラメータ変数を定義します
tf.placeholder = XS(tf.float32、[なし,. 1 ])
YS = tf.placeholder(tf.float32、[なし,. 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)
出力層予測を定義します予測 
予測= add_layer(layer02,10,1、activation_function = なし)
 算出損失1予測出力と開始正方形の出力の偏差を算出 
loss_square = tf.square(y_data - 予測)
 シートの計算量2.各次元の要素の和。
= tf.reduce_sum reduce_sum_square(loss_square、reduction_indices = [1 ])
3計算値損失:各次元の要素テンソルの平均値 
損失= tf.reduce_mean(reduce_sum_square) 

全ての試料について勾配降下学習アルゴリズムを使用して 
train_step = TF .train.GradientDescentOptimizer(0.1 ).minimize(損失)
 変数の初期化を定義 
INIT = tf.initialize_all_variables()
作成セッション 
のSES = tf.Session()
初期化変数ポインタ実行
sess.run(INIT)を

#1 ##は、ニューラルネットワークを定義されています###構造のEND 

## ###定義された変数構造END 

 I におけるレンジ(2000 ):
    sess.run(train_step、feed_dict = {XS:x_data、YS:y_data})
    もし私%50 == 0:
         プリント(sess.run(損失、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.

 

おすすめ

転載: www.cnblogs.com/Areas/p/11032577.html