天天看点

2.6tf.norm()函数

该函数可以用来求L1_norm范数和Eukl_norm范数

L1-Norm计算方式(看不懂可以看例子)

Eukl-Norm计算方式

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
a=tf.ones([2,2])
print(tf.norm(a))#Eukl_norm
print(tf.norm(a,ord=1))#L1_norm
print(tf.norm(a,ord=2))#Eukl_norm
print(tf.sqrt(tf.reduce_sum(tf.square(a))))#Eukl_norm
print(tf.norm(a,ord=3))
a=tf.constant([[1,2],[3,4]])
print(tf.norm(a,ord=1,axis=0))#L1_norm,axis=0,列
print(tf.norm(a,ord=1,axis=1))#L1_norm,axis=1,行      
#output
# tf.Tensor(2.0, shape=(), dtype=float32)
# tf.Tensor(4.0, shape=(), dtype=float32)
# tf.Tensor(2.0, shape=(), dtype=float32)
# tf.Tensor(2.0, shape=(), dtype=float32)
# tf.Tensor(1.587401, shape=(), dtype=float32)
# tf.Tensor([4 6], shape=(2,), dtype=int32)
# tf.Tensor([3 7], shape=(2,), dtype=int32)      

继续阅读