首页 > 解决方案 > 如何使用 Tensorflow 找到值的立方根?

问题描述

我尝试了以下方法:

>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> tf.math.pow(3,3)
2019-05-06 16:05:51.508296: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
<tf.Tensor: id=3, shape=(), dtype=int32, numpy=27>
>>> tf.math.pow(9,(1/3))
<tf.Tensor: id=7, shape=(), dtype=int32, numpy=1>
>>> tf.math.pow(27,(1/3))
<tf.Tensor: id=11, shape=(), dtype=int32, numpy=1>
>>> tf.math.pow(27,0.3)
<tf.Tensor: id=15, shape=(), dtype=int32, numpy=1>
>>> tf.math.pow(4,0.5)
<tf.Tensor: id=19, shape=(), dtype=int32, numpy=1>
>>> tf.math.pow(4,1/2)
<tf.Tensor: id=23, shape=(), dtype=int32, numpy=1>

我知道这sqrt是用于平方根的。但是如果我需要一个数字的立方根,我如何用 tensorflow 计算它?

标签: pythontensorflow

解决方案


请试试这个:

>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> tf.math.pow(27.0,1.0/3.0)
2019-05-06 16:22:39.403646: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
<tf.Tensor: id=3, shape=(), dtype=float32, numpy=3.0>

猜猜问题出在数据类型上。


推荐阅读