欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tensorflow的练习Demo

程序员文章站 2022-05-27 09:50:35
...

第一次练习:

import tensorflow as tf
import numpy as np

#creat data
x_data = np.random.rand(100).astype(np.float32)#产生[0,1]之间的100个随机数
y_data = np.multiply(x_data, x_data)*0.1 + x_data*0.2 + 0.3#y=0.1x^2 + 0.2x + 0.3

#creat tensorflow struchtre start
a = tf.Variable(tf.random_uniform([1], -1, 1))
b = tf.Variable(tf.random_uniform([1], -1, 1))
c = tf.Variable(tf.random_uniform([1], -1, 1))

y = a*np.multiply(x_data, x_data) + b*x_data + c
loss = tf.reduce_mean(tf.square(y-y_data))#误差
optimizer = tf.train.GradientDescentOptimizer(0.65)#选择了梯度下降的优化方法。0.65是学习效率,是0到1的数。越大收敛越快。

train = optimizer.minimize(loss)
init = tf.global_variables_initializer()

#creat tensorflow struchtre end
sess = tf.Session()
sess.run(init)#Very important

for step in range(1001):
    sess.run(train)
    if(step%20 == 0):
        print("step:%d a:%f b:%f c:%f"%(step, sess.run(a), sess.run(b), sess.run(c)))

step:0 a:-0.326912 b:0.021422 c:0.528646
step:20 a:0.010856 b:0.276253 c:0.288528
step:40 a:0.025957 b:0.279414 c:0.284537
step:60 a:0.030900 b:0.274777 c:0.285513
step:80 a:0.035218 b:0.270136 c:0.286428
step:100 a:0.039254 b:0.265769 c:0.287275
step:120 a:0.043037 b:0.261674 c:0.288067
step:140 a:0.046584 b:0.257833 c:0.288811
step:160 a:0.049911 b:0.254231 c:0.289507
step:180 a:0.053031 b:0.250854 c:0.290161
step:200 a:0.055956 b:0.247687 c:0.290774
step:220 a:0.058699 b:0.244717 c:0.291348
step:240 a:0.061271 b:0.241932 c:0.291887
step:260 a:0.063683 b:0.239320 c:0.292392
step:280 a:0.065944 b:0.236872 c:0.292866
step:300 a:0.068065 b:0.234575 c:0.293310
step:320 a:0.070054 b:0.232422 c:0.293727
step:340 a:0.071919 b:0.230403 c:0.294118
step:360 a:0.073668 b:0.228510 c:0.294484
step:380 a:0.075308 b:0.226734 c:0.294828
step:400 a:0.076846 b:0.225069 c:0.295150
step:420 a:0.078288 b:0.223508 c:0.295452
step:440 a:0.079640 b:0.222044 c:0.295735
step:460 a:0.080908 b:0.220671 c:0.296001
step:480 a:0.082097 b:0.219384 c:0.296250
step:500 a:0.083212 b:0.218176 c:0.296483
step:520 a:0.084257 b:0.217044 c:0.296702
step:540 a:0.085238 b:0.215983 c:0.296908
step:560 a:0.086157 b:0.214988 c:0.297100
step:580 a:0.087019 b:0.214054 c:0.297281
step:600 a:0.087828 b:0.213179 c:0.297450
step:620 a:0.088586 b:0.212358 c:0.297609
step:640 a:0.089297 b:0.211589 c:0.297758
step:660 a:0.089963 b:0.210867 c:0.297897
step:680 a:0.090588 b:0.210190 c:0.298028
step:700 a:0.091174 b:0.209555 c:0.298151
step:720 a:0.091724 b:0.208960 c:0.298266
step:740 a:0.092239 b:0.208402 c:0.298374
step:760 a:0.092723 b:0.207879 c:0.298476
step:780 a:0.093176 b:0.207388 c:0.298571
step:800 a:0.093601 b:0.206928 c:0.298660
step:820 a:0.093999 b:0.206497 c:0.298743
step:840 a:0.094373 b:0.206092 c:0.298821
step:860 a:0.094724 b:0.205713 c:0.298895
step:880 a:0.095052 b:0.205357 c:0.298964
step:900 a:0.095360 b:0.205023 c:0.299028
step:920 a:0.095649 b:0.204711 c:0.299089
step:940 a:0.095920 b:0.204417 c:0.299145
step:960 a:0.096174 b:0.204142 c:0.299199
step:980 a:0.096413 b:0.203884 c:0.299249
step:1000 a:0.096636 b:0.203642 c:0.299295

总体上a、b、c越来越接近真实傎。
查了一下tf.random_uniform的用法:

tf.random_uniform((4, 4), minval=-0.5,maxval=0.5,dtype=tf.float32)))
相关标签: ML