一、数学计算

1.加减乘除

加法:tf.math.add( x, y, name=None)

减法:tf.math.subtract( x, y, name=None )

乘法:tf.math.multiply( x, y, name=None )

除法:tf.math.divide( x, y, name=None )

2.指数、开方、对数

指数:tf.math.pow( x, y, name=None ),x的y次方

开方:tf.math.sqrt( x, name=None )

对数:tf.math.log( x, name=None )

3.矩阵相乘

两个矩阵相乘,输出结果矩阵

tf.matmul(
    a,
    b,
    transpose_a=False,
    transpose_b=False,
    adjoint_a=False,
    adjoint_b=False,
    a_is_sparse=False,
    b_is_sparse=False,
    name=None
)
import tensorflow as tf
matrix1=tf.constant([[1,2],[3,4]])
matrix2=tf.constant([[5,6],[7,8]])
out=tf.matmul(matrix1,matrix2)
print(out)

二、图像编码与解码

编码:将png/jpg/bmp等格式的图像转换为字节数据的过程

解码:将字节数据的图像转换为png/jpg/bmp等格式图像的过程

1.转化成字节数据

使用

tf.io.read_file(
    filename, name=None
)
#或者
tf.io.gfile.GFile(
    name, mode='r'
)
import tensorflow as tf
import os
print(os.getcwd())
image_path="/home/misscao/下载/mmexport1658232104166.jpg";
image_bytes=tf.io.read_file(image_path)
print("图像字节数据:{}".format(image_bytes))

2.转为网络安全Base64编码

将上述求出的字节数据利用tf.io.encode_base64转换成Base64编码

image_s64=tf.io.encode_base64(image_bytes)
image_s64=format(image_s64)[2:-1]#第三个字符开始且不包含最后一个字符
print(image_s64)

3.网络安全的Base64编码解码

tf.io.decode_base64(
    input, name=None
)#base64解码
tf.io.decode_image(
    contents,
    channels=None,
    dtype=tf.dtypes.uint8,
    name=None,
    expand_animations=True
)#执行适当的操作将输入字节串转换为类型为dtype的张量
image_path="/home/misscao/下载/mmexport1658232104166.jpg"
image_bytes=tf.io.read_file(image_path)
image_s64=tf.io.encode_base64(image_bytes)
image_s64=tf.io.decode_base64(image_s64)
image_matrix=tf.io.decode_image(image_s64)
plt.imshow(image_matrix)
plt.show()

三、图像数据处理

图像处理是在神经网络训练之前的预处理

1.图像读取显示

image_path="/home/misscao/下载/tensorflow2_data/tensorflow_image/3565.jpg"
image=tf.io.read_file(image_path)#图像读取
image_decode=tf.io.decode_image(image,channels=3)
plt.imshow(image_decode)
plt.show()

注意:灰度图的通道数为1,彩色图的通道为3(RGB)

2.图像缩放

tf.image.resize(
images,
size,
method,
preserve_aspect_ratio,
antialias=False,
name=None
)
#method表示插值方法
image_path="/home/misscao/下载/tensorflow2_data/tensorflow_image/3565.jpg"
image=tf.io.read_file(image_path)#图像读取
image_decode=tf.io.decode_image(image,channels=3)
h,w,c=image_decode.shape#高 宽 通道数
print("before resize image hight:{},width:{},channels:{}".format(h,w,c))
image_resize=tf.image.resize(image_decode,(618,618),method="lanczos3")
h,w,c=image_resize.shape
print("after resize image hight:{},width:{},channels:{}".format(h,w,c))
image_resize=image_resize/255
plt.imshow(image_resize)
plt.show()

3.图像翻转

tf.image.flip_up_down(image)#上下翻转
tf.image.transpose(image)#对角线翻转
tf.image.flip.left_right(image)#左右翻转

4.图像旋转

tf.image.rot90(image,k)

其中k为图像旋转次数,图像逆时针旋转(k×90)度

5.图像色彩调整

亮度调整:tf.image.adjust_brightness(image,delta)

对比度调整:tf.image.adjust_contrast(image_data,delta)

四、Keras接口

tf.keras.layers命名空间中提供了网络层的类接口,如全连接层,激活函数层,池化层,卷积层神经网络层等。

tf.keras中有两个重要概念:模型model和层layer.layer封装了具体的计算流程和对应的变量如全连接层Dense,卷积层CNN,池化层Pooling;model是一系列层的连接和组织。对一个训练好的模型,可以直接通过Y_pred=model(X)完成X样本的深度网络预测。

五、Keras损失函数

用来计算预测值和我们期望的结果即标签之间的差距

均方差函数(Mean Squared Error MSE)

交叉熵函数(entropy)

六、Keras激活函数

在tensorflow.python.keras.activations可以看到多种激活函数

sigmoid函数将整个实数范围内的值映射到[0,1]范围内

tensorflow矩阵减法 tensorflow矩阵运算操作_深度学习

 

tanh可以通过sigmoid平移等操作得到

relu用于隐层神经元输出Rectified Linear Unit(ReLU)