tensorflow2.0数学运算验证笔记

加法

# 加法
a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(400, 400, 3), seed=1.0))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(400, 400, 3), seed=1.0))

c = a + b  # 形状相同的加法是每个元素对应相加
d = c + 1  # 这样子进行加法的话,是每个元素都进行加1
print(c)
print(d)

减法

import tensorflow as tf


a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(400, 400, 3), seed=1.0))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(400, 400, 3), seed=1.0))

c = a - b  # 形状相同的减法是每个元素对应相减,注意这样需要形状相同
d = c - 1  # 这样子进行减法的话,是每个元素都进行减1
print(c)
print(d)

乘法

import tensorflow as tf


a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))

c = a * b  # 形状相同的乘法是每个元素对应相乘,注意这样需要形状相同
d = c * 2  # 这样子进行相乘的话,是每个元素都乘与2
print(c)
print(d)

除法

import tensorflow as tf

a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))

c = a / b  # 形状相同的除法是每个元素对应,注意这样需要形状相同
d = c / 2  # 这样子进行除法的话,是每个元素都除于2
print(c)
print(d)

平方和开方

import tensorflow as tf

a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))

c = a ** 2  # 平方,将tensor张量里面的元素逐一进行平方生成另外一个张量
d = a ** (0.5)  # 开方,将tensor张量里面的元素逐一进行开方生成另外一个张量,要求数据大于0,如果小于0则用nan代替
e = tf.sqrt(a, name="e")  # 这个函数相当于a ** (0.5)
print(c)

取余

import tensorflow as tf

a = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))
b = tf.Variable(initial_value=tf.random.truncated_normal(shape=(4, 4, 3), seed=1.0, dtype=tf.float32))

d = a % b  # 取余数操作,对应位置的元素取余数
c = a % 2  # 取余数操作,每个元素都进行取余操作
print(c)

实现mse

import tensorflow as tf

y_true = tf.Variable(initial_value=tf.random.truncated_normal(shape=(32, 1470), seed=1.0, dtype=tf.float32))  
y_pred = tf.Variable(initial_value=tf.random.truncated_normal(shape=(32, 1470), seed=1.0, dtype=tf.float32))

c = y_pred - y_true
c2 = c ** 2

# d = tf.reduce_sum(input_tensor=c2, axis=0)

mse = tf.reduce_mean(input_tensor=c2, axis=1)
# mse的形状是(32, )
# 实现的公式如下图所示

pass

tensorflow2.0数学运算验证笔记_第1张图片

补充一两个累加和求多个数平均的函数,通过这些函数,我们就可以实现大多数损失函数了

import tensorflow as tf

y_true = tf.Variable(initial_value=tf.random.truncated_normal(shape=(32, 1470), seed=1.0, dtype=tf.float32))
y_pred = tf.Variable(initial_value=tf.random.truncated_normal(shape=(32, 1470), seed=1.0, dtype=tf.float32))

c = y_pred - y_true
c2 = c ** 2

d = tf.reduce_sum(input_tensor=c2, axis=0)  # 表示在第0维度上面进行累加和
e = tf.reduce_sum(input_tensor=c2, axis=1)  # 表示在第1维度上面进行累加和

f = tf.reduce_mean(input_tensor=c2, axis=0)  # 表示在第0维度上面进行求平均
g = tf.reduce_mean(input_tensor=c2, axis=1)  # 表示在第1维度上面进行求平均

"""
各种求回归和偏差的常用底层函数就到这里结束了
"""
pass

你可能感兴趣的:(计算机视觉)