Is sympy solve compatible with tensorflow GradientTape?

194 views Asked by At

I want to solve p for Vo using sympy solve, an algorithm that solves an equation without an initial value. Then, I want to find the derivative of p for Vo using TensorFlow's automatic differentiation. I wrote the code below, and the value was calculated well. However, it could not be converted to tensorflow dtype at the end. Is there a way to use TensorFlow tensors compatible with sympy?

from sympy import solve, Symbol, re
import tensorflow as tf`

class Concentrate_calculator:
    def __init__(self, k, k1, m):
        self.k=k
        self.k1=k1
        self.m=m
    
    def calp(self, Vo):
        p=Symbol('p')
        equation = k1*m/(p+k1)+k/p-2*Vo-p
        solution = solve(equation, p)
        p=float(re(solution[2]))
        return p


k=10.0**15
k1=10.0**9.5
m=10.0**19.2    

cal = Concentrate_calculator(k,k1,m)

Vo = tf.Variable(1e18)
#Vo=1e18
with tf.GradientTape() as t:
    t.watch(Vo)
    p=cal.calp(Vo)
  
dpdVo = t.gradient(p,Vo)
print(dpdVo)

TypeError: Cannot convert value 21897084140.095097 to a TensorFlow DType.

0

There are 0 answers