tensorflow拟合曲线
来源:互联网 发布:网站域名带不带www 编辑:程序博客网 时间:2024/04/28 04:56
单值法:
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# Prepare train datatrain_X = np.linspace(0, np.pi, 100)train_Y=np.sin(train_X)# Define the modelX1 = tf.placeholder("float")X2 = tf.placeholder("float")X3 = tf.placeholder("float")X4 = tf.placeholder("float")Y = tf.placeholder("float")w1 = tf.Variable(0.0, name="weight1")w2 = tf.Variable(0.0, name="weight2")w3 = tf.Variable(0.0, name="weight3")w4 = tf.Variable(0.0, name="weight4")b = tf.Variable(0.0, name="bias")y1=w1*X1+(w2)*(X2)**3+w3*(X3)**5+(w4)*(X4)**7loss = tf.reduce_mean(tf.square(Y - y1))c1=[]c2=[]c3=[]c4=[]loss1=[]##loss = tf.reduce_mean(tf.square(Y - w1*X1 -(w2)*(X2)**3-(w3)*(X3)**5))optimizer =tf.train.AdamOptimizer()train_op=optimizer.minimize(loss)## Create session to runwith tf.Session() as sess: init=tf.global_variables_initializer() sess.run(init) epoch = 1 for i in range(100): for (x, y) in zip(train_X, train_Y): _,ww1, ww2,ww3,ww4,loss_= sess.run([train_op, w1, w2,w3,w4,loss],feed_dict={X1:x,X2:x,X3:x,X4:x,Y: y}) print("Epoch: {}, w1: {}, w2: {},w3:{},loss:{}".format(epoch, ww1,ww2,ww3,loss_)) epoch += 1 c1=c1+[ww1] c2=c2+[ww2] c3=c3+[ww3] c4=c4+[ww4] loss1=loss1+[loss_]plt.plot(train_X,train_Y,"+")plt.plot(train_X,ww1*train_X+(ww2)*(train_X**3)+ww3*(train_X**5)+ww4*(train_X**7))plt.show()
向量法:
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# Prepare train datatrain_X = np.linspace(0, np.pi, 100)train_Y=np.sin(train_X)# Define the modelX1 = tf.placeholder(tf.float32,shape=(100,))X2 = tf.placeholder(tf.float32,shape=(100,))X3 = tf.placeholder(tf.float32,shape=(100,))X4 = tf.placeholder(tf.float32,shape=(100,))Y = tf.placeholder(tf.float32,shape=(100,))w1 = tf.Variable(0.0,name="weight1")w2 = tf.Variable(0.0, name="weight2")w3 = tf.Variable(0.0, name="weight3")w4 = tf.Variable(0.0, name="weight4")y1=w1*X1+w2*X2+w3*X3+w4*X4loss = tf.reduce_mean(tf.square(Y - y1))optimizer =tf.train.AdamOptimizer()train_op=optimizer.minimize(loss)## Create session to runwith tf.Session() as sess: init=tf.global_variables_initializer() sess.run(init) epoch = 1 for i in range(10000): _,ww1, ww2,ww3,ww4,loss_= sess.run([train_op, w1, w2,w3,w4,loss],feed_dict={X1:train_X,X2:train_X**3,X3:train_X**5,X4:train_X**7,Y: train_Y}) print("Epoch: {}, w1: {}, w2: {},w3:{},w4:{},loss:{}".format(epoch, ww1,ww2,ww3,ww4,loss_)) epoch += 1plt.plot(train_X,train_Y,"+",label='data')plt.plot(train_X,ww1*train_X+(ww2)*(train_X**3)+ww3*(train_X**5)+ww4*(train_X**7),label='curve')plt.savefig('1.png',dpi=200)plt.axis([0,np.pi,-2,2])plt.legend(loc=1)plt.show()
阅读全文
1 0
- tensorflow拟合曲线
- TensorFlow拟合曲线
- TensorFlow学习记录-- 4.拟合曲线
- 拟合曲线
- 最小二乘法拟合曲线
- MATLAB拟合曲线
- 拟合二元多次曲线
- 多项式拟合曲线
- 使用G2O拟合曲线
- g2o拟合曲线
- Matlab拟合曲线
- tensorflow之过拟合
- TensorFlow 最小二乘法拟合
- TensorFlow拟合一条直线
- 最小二乘法拟合二元多次曲线
- 用最小二乘法构造拟合曲线
- 最小二乘法求多项式拟合曲线
- 【matlab】拟合曲线 polyfit set
- JSONP原理
- 大数据可视化应用领域探析
- C++源码之目录遍历
- 关于3年买车5年买房的那些事
- Fragment获取Activity值的时候拿不到
- tensorflow拟合曲线
- JS判断页面是否在微信浏览器、QQ浏览器等打开的方法
- 简单介绍solrj的使用
- webService与Spring整合后调用步骤
- intellij-maven-spring-mybatis遇到的问题
- 经典SQL练习题
- Billboard HDU 2795
- Android版本的"Wannacry"文件加密病毒样本分析(附带锁机)
- 求数轴上一点到数轴上一些点距离之和最小