Weights:
[[ 0.30919516 0.29567152 0.11157229]
[ 0.26642913 -0.21269836 -0.58886886]]
第一个print语句按预期工作。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-b0e4f93d01f8> in <module>()
4 print('Weights:')
5 print(sess.run(weights))
----> 6 print("{0:2f}".format(sess.run(weights)))
> TypeError: non-empty format string passed to object.__format__
使用str.format()的第二个print语句给出了以下错误。
np.set_printoptions(precision=2)
weights = tf.Variable(tf.truncated_normal([2,3]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Weights:')
print(sess.run(weights))
Weights:
[[ 0.01 -0.42 -1.57]
[-0.44 1.62 0.27]]
除了以下答案,我还发现np.set_printoptions(precision = 2)也有效。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample</title>
</head>
<body>
<h1 id="hello_world">Sample</h1>
<script type="text/javascript">
var div = document.createElement("div");
var t = document.createElement('template');
t.innerHTML = "Check Console tab for javascript output: Hello world!!!<br/><script type='text/javascript' >console.log('Hello world!!!');<\/script>";
for (var i=0; i < t.content.childNodes.length; i++){
var node = document.importNode(t.content.childNodes[i], true);
div.appendChild(node);
}
document.body.appendChild(div);
</script>
</body>
</html>
答案 0 :(得分:1)
您尝试打印整个数组,而format
期望可以表示为float的单个值。试试这样:
print(np.around(sess.run(weights), 2)
#[[ 0.31 0.30 0.11]
# [ 0.27 -0.21 -0.59]]
此外,正确的格式为0:.2f