美文网首页
2018-11-29 输出数组值 均匀分布

2018-11-29 输出数组值 均匀分布

作者: 七月那个阿瓜呀 | 来源:发表于2018-11-29 20:34 被阅读2次

1. 如何直接输出数组值而不是数组类型?

第一种

import tensorflow as tf
x = tf.constant(1)
print (x)

输出:
Tensor("Const:0", shape=(), dtype=int32)

第二种

import tensorflow as tf

a = tf.constant(0.1, shape=(1,10))

sess = tf.Session()
sess.run(a)
print(a)

输出:
Tensor("Const:0", shape=(1, 10), dtype=float32

是因为:
print只能打印输出shape的信息,而要打印输出tensor的值,需要借助 tf.Session,tf.InteractiveSession
因为我们在建立graph的时候,只建立 tensor 的 结构形状信息 ,并没有 执行 数据的操作。

解决办法:

法一

import tensorflow as tf
x = tf.constant(1)
with tf.Session() as sess:
    print sess.run(x)

输出 1

法二

import tensorflow as tf
x = tf.constant(1)
sess = tf.InteractiveSession()
print x.eval()

输出1

注意eval函数!后续跟进用法

2. 随机生成 均匀分布样本

numpy.random.uniform(low,high,size)

功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.

相关文章

网友评论

      本文标题:2018-11-29 输出数组值 均匀分布

      本文链接:https://www.haomeiwen.com/subject/gjcqcqtx.html