码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow-不动点迭代求一元方程

时间:2018-12-09 00:02:45      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:email   logic   coding   logical   sep   type   sub   body   res   

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Sep 6 10:16:37 2018 @author: myhaspl @email:myhaspl@myhaspl.com 不动点法求解f(x)=x """ import tensorflow as tf import numpy as np def f(x): y=tf.cos(x)+tf.sin(x) return y def body(x,fx,tol,i,n): x=f(x) fx=f(x) return (tf.Print(x,[x],"x:"),tf.Print(fx,[fx],"fx:"),tf.Print(tol,[tol],"tol:"),tf.Print(i+1,[i],"i:"),tf.Print(n,[n],"n:")) def c(x,fx,tol,i,n): t1=tf.greater(tf.abs(tf.subtract(fx,x)),tol) t2=tf.less(i,n) return tf.logical_and(t1,t2) x = tf.placeholder(tf.float32,shape=(),name="myx") tol= tf.placeholder(tf.float32,shape=(),name="mytol") fx = tf.constant(0,dtype=tf.float32,name="myfx") i = tf.constant(0,dtype=tf.int32,name="myi") n = tf.constant(0,dtype=tf.int32,name="myn") input_dict={x:0.,fx:np.cos(0)+np.sin(0),tol:1e-8,i:0,n:100} res = tf.while_loop(c, body, loop_vars=[x,fx,tol,i,n]) with tf.Session() as sess: y=sess.run(res,feed_dict=input_dict) print y

tol:[1e-08]
x:[1.25872827]
fx:[1.25872803]
n:[100]
i:[98]tol:[1e-08]x:[1.25872803]
fx:[1.25872827]

n:[100]
x:[1.25872827]i:[99]tol:[1e-08]
fx:[1.25872803]

(1.2587283, 1.258728, 1e-08, 100, 100)

tensorflow-不动点迭代求一元方程

标签:email   logic   coding   logical   sep   type   sub   body   res   

原文地址:http://blog.51cto.com/13959448/2327991

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!