a = tf.ones([2,2])
tf.norm(a) #
tf.sqrt(tf.reduce_sum(tf.square(a))) #
a = tf.ones([4,28,28,3])
tf.norm(a) #
tf.sqrt(tf.reduce_sum(tf.square(a))) #
统计某一维度
b = tf.ones([4,28,28,3])
tf.norm(b).shape
tf.norm(b, ord=2, axis=0).shape # ord 2范数 # Out[4]: TensorShape([28, 28, 3])
tf.norm(b, ord=2, axis=1).shape # TensorShape([4, 28, 3])
tf.norm(b, ord=1).shape # TensorShape([])
a = tf.random.normal([4,10])
tf.reduce_min(a), tf.reduce_max(a), tf.reduce_mean(a)
# (,
# ,
# )
tf.reduce_min(a, axis=1).shape # TensorShape([4])
tf.reduce_max(a, axis=0).shape # TensorShape([10])
a = tf.random.normal([4,10])
a.shape # TensorShape([4, 10])
tf.argmax(a) #
tf.argmax(a, axis=1) #
tf.argmin(a, axis=1) #
tf.argmax 的 默认axis=0
a = tf.constant([2, 1, 3, 2, 5])
b = tf.range(5)
res = tf.equal(a, b) #
tf.reduce_sum(tf.cast(res, dtype=tf.int32)) #
pre = tf.constant([[0.1, 0.2, 0.7],[0.9, 0.05, 0.05]])
pre
#
# # array([[0.1 , 0.2 , 0.7 ],
# # [0.9 , 0.05, 0.05]], dtype=float32)>
pre = tf.cast(tf.argmax(pre, axis=1), dtype=tf.int32)
#
y = tf.constant([2,1]) #
accuracy = tf.reduce_sum(tf.cast(tf.equal(pre, y), dtype=tf.int32))/pre[0]
#
tf.unique 去除重复
tf.gather 还原
a = tf.constant([4,2,2,1,4,0])
a #
unique_tensor, idx = tf.unique(a)
unique_tensor, idx
# (,
# )
origin_a = tf.gather(unique_tensor, idx)
#