VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之数据统计(2)

# 对True和False转换为1和0 tf.reduce_sum(tf.cast(res, dtype=tf.int32))
<tf.Tensor: id=191, shape=(), dtype=int32, numpy=0>

Accuracy

a = tf.random.normal([2, 3])
a
<tf.Tensor: id=198, shape=(2, 3), dtype=float32, numpy=
array([[ 0.25201225, -1.3897187 ,  0.29240564],
       [-1.0671712 ,  2.1487093 ,  0.690736  ]], dtype=float32)>
pred = tf.cast(tf.argmax(a, axis=1), dtype=tf.int32)
pred.shape
TensorShape([2])
y = tf.constant([2, 1])
y
<tf.Tensor: id=163, shape=(2,), dtype=int32, numpy=array([2, 1], dtype=int32)>
tf.equal(y, pred)
<tf.Tensor: id=165, shape=(2,), dtype=bool, numpy=array([ True,  True])>
correct = tf.reduce_sum(tf.cast(tf.equal(y, pred), dtype=tf.int32))
correct
<tf.Tensor: id=170, shape=(), dtype=int32, numpy=2>
correct / 2
<tf.Tensor: id=175, shape=(), dtype=float64, numpy=1.0>

tf.unique

  • 用于去重
a = tf.range(5)
a
<tf.Tensor: id=235, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
# 返回索引
tf.unique(a)
Unique(y=<tf.Tensor: id=237, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>, idx=<tf.Tensor: id=238, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>)
a = tf.constant([4, 2, 2, 4, 3])
a
<tf.Tensor: id=226, shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3], dtype=int32)>
res = tf.unique(a)
Unique(y=<tf.Tensor: id=228, shape=(3,), dtype=int32, numpy=array([4, 2, 3], dtype=int32)>, idx=<tf.Tensor: id=229, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2], dtype=int32)>)

相关教程