如何获得张量的值?Python [英] How to get the value of a tensor? Python

查看:31
本文介绍了如何获得张量的值?Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行一些计算时,我最终计算出了 average_acc.当我尝试打印它时,它输出:tf.Tensor(0.982349, shape=(), dtype=float32).如何获取它的 0.98.. 值并将其用作普通浮点数?

While doing some calculations I end up calculating an average_acc. When I try to print it, it outputs: tf.Tensor(0.982349, shape=(), dtype=float32). How do I get the 0.98.. value of it and use it as a normal float?

我想要做的是将一堆这些放在一个数组中并绘制一些图形,但为此,据我所知,我需要简单的浮点数.

What I'm trying to do is get a bunch of those in an array and plot some graphs, but for that, I need simple floats as far as I can tell.

推荐答案

在我看来,您好像还没有计算过张量.您可以调用 tensor.eval() 来评估结果,或者使用 session.run(tensor).

It looks to me as if you have not evaluated the tensor. You can call tensor.eval() to evaluate the result, or use session.run(tensor).

import tensorflow as tf

a = tf.constant(3.5)
b = tf.constant(4.5)
c = a * b

with tf.Session() as sess:
    result = c.eval()
    # Or use sess.run:
    # result = sess.run(c)

    print(result) 
    # out: 15.75

    print(type(result))
    # out: <class 'numpy.float32'>

这篇关于如何获得张量的值?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆