tf.squared_difference()
相关参数
- x ,类型:张量
- y ,类型:张量
- name=None 类型:string , 运算名称
- 功能:计算张量 x、y 对应元素差的平方
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
arr1 = [[1, 1], [4, 4]]
x = tf.Variable(arr1, dtype=tf.float32)
arr2 = [[3, 3], [3, 3]]
y = tf.Variable(arr2, dtype=tf.float32)
squ_diff = tf.compat.v1.squared_difference(x, y)
init_op = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
sess.run(init_op)
print(sess.run(squ_diff ))
输出:
[[4. 4.]
[1. 1.]]
tf.reduce_mean()
相关参数
-
input_tensor :输入的张量1 -
axis=None:指定轴,默认None,即计算所有元素的均值 -
keep_dims=False: 是否降维,默认False,降维;True为输出结果与输入的形状保持一致【新版本已经没有改参数】 -
name=None:操作的名称 -
功能:用于计算张量沿着指定的数轴(某一维度)上的的平均值,主要用作降维或计算tensor的平均值。
import numpy as np
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
arr1 = [[1, 1], [4, 4]]
x = tf.Variable(arr1, dtype=tf.float32)
arr2 = [[3, 3], [3, 3]]
y = tf.Variable(arr2, dtype=tf.float32)
squ_diff = tf.compat.v1.squared_difference(x, y)
mean_all = tf.compat.v1.reduce_mean(squ_diff, keep_dims=False)
mean_col = tf.compat.v1.reduce_mean(squ_diff, axis=0, keep_dims=False)
mean_row = tf.compat.v1.reduce_mean(squ_diff, axis=1, keep_dims=False)
init_op = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
sess.run(init_op)
print(sess.run(squ_diff))
print(sess.run(mean_all))
print(sess.run(mean_col))
print(sess.run(mean_row))
输出:
[[4. 4.] # squ_diff
[1. 1.]]
2.5 # mean_all
[2.5 2.5] # mean_col
[4. 1.] # mean_row
|