深度学习_tensorflow初识

#two demos

 

#deme 01
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train,x_test
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


#demo 02
#第一步,import
import tensorflow as tf #导入模块
from sklearn import datasets #从sklearn中导入数据集
import numpy as np #导入科学计算模块
import keras
 
#第二步,train, test
x_train = datasets.load_iris().data #导入iris数据集的输入
 
y_train = datasets.load_iris().target #导入iris数据集的标签
 
np.random.seed(120) #设置随机种子,让每次结果都一样,方便对照
 
np.random.shuffle(x_train) #使用shuffle()方法,让输入x_train乱序
 
np.random.seed(120) #设置随机种子,让每次结果都一样,方便对照
 
np.random.shuffle(y_train) #使用shuffle()方法,让输入y_train乱序
 
tf.random.set_seed(120) #让tensorflow中的种子数设置为120
#第三步,models.Sequential()
model = tf.keras.models.Sequential([ #使用models.Sequential()来搭建神经网络
    tf.keras.layers.Dense(3, activation = "softmax", kernel_regularizer = tf.keras.regularizers.l2()) #全连接层,三个神经元,激活函数为softmax,使用l2正则化
])
#解释下softmax:将输出的值映射为[0,1],然后累计为1,最后选取输出结点的时候,我们就可以选取概率最大(也就是值对应最大的)结点,作为我们的预测目标
#tf.keras.regularizers.l:L2正则被用于防止模型的过拟合,L1正则项被用于产生稀疏权值矩阵
#第四步,model.compile()
model.compile(  #使用model.compile()方法来配置训练方法
    optimizer = tf.keras.optimizers.SGD(lr = 0.1), #使用SGD优化器,学习率为0.1
    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False), #配置损失函数
    metrics = ['sparse_categorical_accuracy'] #标注网络评价指标
)
 
#第五步,model.fit()
model.fit(  #使用model.fit()方法来执行训练过程,
    x_train, y_train, #告知训练集的输入以及标签,
    batch_size = 32, #每一批batch的大小为32,
    epochs = 500, #迭代次数epochs为500
    validation_split = 0.2, #从测试集中划分80%给训练集
    validation_freq = 20 #测试的间隔次数为20
)
 
#第六步,model.summary()
model.summary() #打印神经网络结构,统计参数数目

 

 
posted @ 2022-07-17 21:43  是冰美式诶  阅读(35)  评论(0)    收藏  举报