用于一个回归任务或者正则问题
1 # l2损失,output= sum(x ** 2)/2 2 inputdata = tf.Variable(np.random.rand(2,3), dtype=np.float32) 3 loss = tf.nn.l2_loss(inputdata) # 标量 4 with tf.Session() as sess: 5 sess.run(tf.global_variables_initializer()) 6 print (sess.run(inputdata)) 7 print (sess.run(loss))
输出:
[[ 0.99520844 0.57105571 0.4224354 ]
[ 0.77398151 0.40279421 0.6592328 ]]
1.34544