Je souhaite calculer un polynôme à l'aide de l'API TensorFlow Python comme suit:
Polynôme: f (x) = a0 + a1 * x + a2 * x ^ 2 + a3 * x ^ 3 + a4 * x ^ 4 .
Le code est:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/trytf/sandbox.py", line 7, in <module>
polynomial = tf.constant([1, x, tf.pow(x, 2), tf.pow(x, 3), tf.pow(x, 4)])
File "C:\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "C:\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 442, in make_tensor_proto
_AssertCompatible(values, dtype)
File "C:\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 350, in _AssertCompatible
raise TypeError("List of Tensors when single Tensor expected")
TypeError: List of Tensors when single Tensor expected
Un morceau de code assez simple mais je n'arrive pas à le comprendre correctement.
Ici est la trace d'erreur:
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, shape=())
cfc = tf.placeholder(dtype=tf.float32, shape=5)
polynomial = tf.constant([1, x, tf.pow(x, 2), tf.pow(x, 3), tf.pow(x, 4)])
f = tf.tensordot(cfc, polynomial, 1)
with tf.Session() as sess:
result = sess.run(f, feed_dict={x: 1.0,
cfc: [0.0, 1.0, -1.0, 1.0, -1.0]})
print(result)
Je ne comprends pas pourquoi il dit qu'il y a une liste de tenseurs. S'il vous plaît donnez votre avis. Merci.
3 Réponses :
vous devriez remplacer le tf.constant par tf.stack car vous ne pouvez pas passer la liste des tenseurs comme argument de tf.constant
polynôme = tf.stack ([1, x, tf.pow (x, 2), tf.pow (x, 3), tf.pow (x, 4)])
Merci. Cela fonctionne et l'explication est aussi ce que je recherche.
Cela vaut-il également pour tf.cast ou tf.math.count_nonzero?
C'est parce que vous essayez de créer une constante en utilisant x, qui est un espace réservé qui accepte une valeur au moment de l'exécution. Ainsi, il vous jette cette erreur.
Voici une version modifiée du code qui renvoie un résultat lorsqu'il est exécuté sur Google Colab.
[ 1. 2. 4. 8. 16.]
Résultat:
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, shape=())
cfc = tf.placeholder(dtype=tf.float32, shape=(5))
polynomial = tf.Variable([1.0, 0.0, 0.0, 0.0, 0.0])
polynomial_op = polynomial.assign([1.0, x, tf.pow(x, 2), tf.pow(x, 3), tf.pow(x, 4)])
f = tf.tensordot(cfc, polynomial, 1)
init_op = tf.variables_initializer([polynomial])
with tf.Session() as sess:
sess.run(init_op)
result = sess.run(polynomial_op, feed_dict={x: 2.0, cfc: [0.0, 1.0, -1.0, 1.0, -1.0]})
print(result)
sess.close()
Ici, j'ai défini le polynôme comme une variable et je l'ai initialisé avec l'initialiseur de variables tf. Notez que depuis que je fais cela, j'ai attribué une valeur par défaut au début, puis je lui ai réaffecté la valeur qui serait calculée avec x en définissant une opération d'affectation, puis en l'exécutant. Vous pouvez choisir de le faire de toute autre manière confortable.
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, shape=())
cfc = tf.placeholder(dtype=tf.float32, shape=5)
polynomial = [1, x, x**2, x**3, x**4]
f = tf.tensordot(cfc, polynomial, 1)
with tf.Session() as sess:
result = sess.run(f, feed_dict={x: 1.0, cfc: [0.0, 1.0, -1.0, 1.0, -1.0]})
print(result)