Tensorflow简单实例2

 目标:曲线拟合

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 生成200个随机点以及噪音
x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis]
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise

x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])

# 定义神经网络中间层
Weights_L1 = tf.Variable(tf.random.normal([1, 10]))
biases_L1 = tf.Variable(tf.zeros([1, 10]))
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)

# 输出层
Weights_L2 = tf.Variable(tf.random_normal([10, 1]))
biases_L2 = tf.Variable(tf.zeros([1, 1]))
Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)

# 二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))
# 梯度下降算法
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(2000):
        sess.run(train_step, feed_dict={x: x_data, y: y_data})

    prediction_value = sess.run(prediction, feed_dict={x: x_data})

    plt.figure()
    plt.scatter(x_data, y_data)
    plt.plot(x_data, prediction_value, "r-", lw=5)
    plt.show()

结果:

Tensorflow简单实例2_第1张图片

x_data   prediction

[-0.5] [0.19294329]
[-0.49497487] [0.19175923]
[-0.48994975] [0.19051863]
[-0.48492462] [0.18922187]
[-0.4798995] [0.1878693]
[-0.47487437] [0.18646146]
[-0.46984925] [0.18499872]
[-0.46482412] [0.18348157]
[-0.45979899] [0.1819106]
[-0.45477387] [0.18028645]
[-0.44974874] [0.17860968]
[-0.44472362] [0.17688093]
[-0.43969849] [0.17510127]
[-0.43467337] [0.173271]
[-0.42964824] [0.17139119]
[-0.42462312] [0.16946252]
[-0.41959799] [0.16748609]
[-0.41457286] [0.16546267]
[-0.40954774] [0.16339341]
[-0.40452261] [0.16127916]
[-0.39949749] [0.15912119]
[-0.39447236] [0.15692042]
[-0.38944724] [0.15467812]
[-0.38442211] [0.15239535]
[-0.37939698] [0.15007356]
[-0.37437186] [0.1477139]
[-0.36934673] [0.14531776]
[-0.36432161] [0.14288644]
[-0.35929648] [0.1404215]
[-0.35427136] [0.13792416]
[-0.34924623] [0.13539611]
[-0.34422111] [0.13283871]
[-0.33919598] [0.13025367]
[-0.33417085] [0.12764248]
[-0.32914573] [0.1250069]
[-0.3241206] [0.1223486]
[-0.31909548] [0.11966904]
[-0.31407035] [0.11697014]
[-0.30904523] [0.11425359]
[-0.3040201] [0.11152133]
[-0.29899497] [0.1087749]
[-0.29396985] [0.10601646]
[-0.28894472] [0.10324754]
[-0.2839196] [0.10047022]
[-0.27889447] [0.09768649]
[-0.27386935] [0.09489794]
[-0.26884422] [0.0921068]
[-0.2638191] [0.08931491]
[-0.25879397] [0.08652426]
[-0.25376884] [0.08373679]
[-0.24874372] [0.08095439]
[-0.24371859] [0.07817917]
[-0.23869347] [0.07541309]
[-0.23366834] [0.07265802]
[-0.22864322] [0.06991606]
[-0.22361809] [0.06718923]
[-0.21859296] [0.06447934]
[-0.21356784] [0.06178849]
[-0.20854271] [0.05911853]
[-0.20351759] [0.05647144]
[-0.19849246] [0.05384925]
[-0.19346734] [0.05125373]
[-0.18844221] [0.04868681]
[-0.18341709] [0.04615049]
[-0.17839196] [0.04364644]
[-0.17336683] [0.04117669]
[-0.16834171] [0.03874289]
[-0.16331658] [0.03634687]
[-0.15829146] [0.03399033]
[-0.15326633] [0.03167516]
[-0.14824121] [0.02940293]
[-0.14321608] [0.02717526]
[-0.13819095] [0.02499384]
[-0.13316583] [0.02286028]
[-0.1281407] [0.0207761]
[-0.12311558] [0.01874267]
[-0.11809045] [0.01676186]
[-0.11306533] [0.01483469]
[-0.1080402] [0.01296279]
[-0.10301508] [0.01114744]
[-0.09798995] [0.00938991]
[-0.09296482] [0.00769159]
[-0.0879397] [0.00605358]
[-0.08291457] [0.0044771]
[-0.07788945] [0.00296333]
[-0.07286432] [0.00151332]
[-0.0678392] [0.00012813]
[-0.06281407] [-0.00119129]
[-0.05778894] [-0.00244397]
[-0.05276382] [-0.00362901]
[-0.04773869] [-0.00474551]
[-0.04271357] [-0.0057927]
[-0.03768844] [-0.00676977]
[-0.03266332] [-0.00767608]
[-0.02763819] [-0.0085108]
[-0.02261307] [-0.00927347]
[-0.01758794] [-0.00996348]
[-0.01256281] [-0.01058028]
[-0.00753769] [-0.01112327]
[-0.00251256] [-0.01159212]
[0.00251256] [-0.0119864]
[0.00753769] [-0.01230577]
[0.01256281] [-0.0125499]
[0.01758794] [-0.01271853]
[0.02261307] [-0.01281141]
[0.02763819] [-0.01282828]
[0.03266332] [-0.01276921]
[0.03768844] [-0.01263374]
[0.04271357] [-0.0124222]
[0.04773869] [-0.01213433]
[0.05276382] [-0.01177019]
[0.05778894] [-0.01132978]
[0.06281407] [-0.01081328]
[0.0678392] [-0.01022079]
[0.07286432] [-0.0095524]
[0.07788945] [-0.00880829]
[0.08291457] [-0.00798891]
[0.0879397] [-0.00709423]
[0.09296482] [-0.00612465]
[0.09798995] [-0.00508054]
[0.10301508] [-0.00396235]
[0.1080402] [-0.00277015]
[0.11306533] [-0.00150467]
[0.11809045] [-0.00016631]
[0.12311558] [0.00124456]
[0.1281407] [0.00272749]
[0.13316583] [0.00428167]
[0.13819095] [0.00590681]
[0.14321608] [0.00760225]
[0.14824121] [0.00936736]
[0.15326633] [0.01120149]
[0.15829146] [0.01310407]
[0.16331658] [0.01507429]
[0.16834171] [0.01711155]
[0.17336683] [0.01921509]
[0.17839196] [0.02138421]
[0.18341709] [0.02361807]
[0.18844221] [0.025916]
[0.19346734] [0.02827699]
[0.19849246] [0.03070048]
[0.20351759] [0.03318542]
[0.20854271] [0.03573113]
[0.21356784] [0.03833658]
[0.21859296] [0.04100097]
[0.22361809] [0.04372352]
[0.22864322] [0.04650301]
[0.23366834] [0.04933885]
[0.23869347] [0.05222993]
[0.24371859] [0.05517522]
[0.24874372] [0.05817383]
[0.25376884] [0.06122481]
[0.25879397] [0.06432732]
[0.2638191] [0.06747992]
[0.26884422] [0.07068209]
[0.27386935] [0.0739326]
[0.27889447] [0.07723044]
[0.2839196] [0.08057454]
[0.28894472] [0.08396398]
[0.29396985] [0.08739763]
[0.29899497] [0.09087444]
[0.3040201] [0.09439346]
[0.30904523] [0.0979536]
[0.31407035] [0.10155372]
[0.31909548] [0.10519281]
[0.3241206] [0.10886969]
[0.32914573] [0.11258352]
[0.33417085] [0.11633309]
[0.33919598] [0.12011722]
[0.34422111] [0.12393505]
[0.34924623] [0.12778552]
[0.35427136] [0.13166726]
[0.35929648] [0.13557956]
[0.36432161] [0.13952123]
[0.36934673] [0.14349112]
[0.37437186] [0.14748825]
[0.37939698] [0.15151142]
[0.38442211] [0.1555597]
[0.38944724] [0.15963213]
[0.39447236] [0.16372743]
[0.39949749] [0.16784477]
[0.40452261] [0.17198303]
[0.40954774] [0.17614108]
[0.41457286] [0.18031791]
[0.41959799] [0.1845128]
[0.42462312] [0.18872443]
[0.42964824] [0.19295192]
[0.43467337] [0.19719402]
[0.43969849] [0.2014502]
[0.44472362] [0.20571916]
[0.44974874] [0.21000005]
[0.45477387] [0.21429165]
[0.45979899] [0.21859346]
[0.46482412] [0.22290424]
[0.46984925] [0.22722317]
[0.47487437] [0.23154928]
[0.4798995] [0.23588155]
[0.48492462] [0.24021927]
[0.48994975] [0.24456154]
[0.49497487] [0.24890746]
[0.5] [0.25325593]

 

你可能感兴趣的:(Tensorflow)