tensorflow and keras

Tensor

A tensor is a multidimensional array. Similar to numpy ndarray objects

insert image description here

basic method

import tensorflow as tf
#创建0维张量
tensor_1=tf.constant(4)
print(tensor_1)
#创建1维张量
tensor_2=tf.constant([2.0,3.0,4.0])
print(tensor_2)
#创建2维张量
tensor_3=tf.constant([[1,2],
                      [3,4],
                      [5,6]],dtype=tf.float16)
print(tensor_3)

Metal device set to: Apple M1
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([2. 3. 4.], shape=(3,), dtype=float32)
tf.Tensor(
[[1. 2.]
[3. 4.]
[5. 6.]], shape=(3, 2), dtype=float16)

convert to numpy

np.array(tensor_1)
tensor_1.numpy()

Common functions

a=tf.constant([[1,2],
               [3,4]])
b=tf.constant([[1,1],
               [1,1]])
tf.add(a,b)
tf.multiply(a,b) #对应元素相乘
tf.matmul(a,b) #矩阵乘法
#最大值
tf.reduce_max(a)
#最大值索引
tf.argmax(a)
#平均值
tf.reduce_mean(a)

variable

is a special tensor whose shape is immutable but whose parameters can be changed

var=tf.Variable([[1,2],[3,4]])
var

insert image description here

#进行修改
var.assign([[2,3],[4,5]])

insert image description here

Introduction to tf.keras

Common modules

insert image description here

common method

import

import tensorflow as tf
from tensorflow import keras

data input

For small data sets, you can directly use data in numpy format for training and evaluation models. For large data sets or cross-device training, use it for data tf.data.datasetsinput

model building

  1. Simple models are constructed using Sequential
  2. Complex models are built using functional programming
  3. Custom layers

Training and Evaluation

Configure training process
Model training
Model evaluation
Model prediction

callback function (callbacks)

Used in the model training process to control the model training behavior, you can customize the callback function

Model saving and restoration

save only parameters

#只保存模型的权重
model.save_weights('./my_model')
#加载模型的权重
model.load_weights('./mu_model')

save the whole model

#保存模型架构与权重在h5文件中
model.save('my_model.h5')
#加载模型:包括架构和对应的权重
model=keras.models.load_model('my_model.h5')

example

Iris Dataset

#导入相关的库
import numpy as np
from sklearn.model_selection import train_test_split

#深度学习:tf.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import utils
#数据处理
x=iris.values[:,:4]
y=iris.values[:,4]
train_x,test_x,train_y,test_y=train_test_split(x,y,test_size=0.5,random_state=0)

#目标值的热编码
def one_hot_encode(arr):
    #获取目标值中的所有类别,并进行独热编码
    uniques,ids=np.unique(arr,return_inverse=True)
    return utils.to_categorical(ids,len(uniques))

#对目标值进行编码
train_y_one=one_hot_encode(train_y)
test_y_one=one_hot_encode(test_y)

#模型构建
#通过sequential进行构建
model=Sequential([
    #隐藏层
    Dense(10,activation='relu',input_shape=(4,))
    Dense(10,activation'relu')
    #输出层
    Dense(3,activation='softmax')
])

model.summary()
utils.plot_model(model,show_shapes=True) #查看网络结构

#模型预测与评估
#模型编译
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
#类型转换
train_x=np.array(train_x,dtype=np.float32)
test_x=np.array(test_x,dtype=np.float32)
#模型训练
model.fit(train_x,train_y_one,epochs=10,batch_size=1,verbose=1)
#模型评估
loss,accuracy=model.evaluate(test_x,test_y_one,verbose=1)

Guess you like

Origin blog.csdn.net/qq_40527560/article/details/131477970