tensorflow中model.fit()用法

tensorflow中model.fit()用法
model.fit()方法用于执行训练过程

model.fit( 训练集的输入特征,

训练集的标签,

batch_size, #每一个batch的大小

epochs, #迭代次数

validation_data = (测试集的输入特征,测试集的标签),

validation_split = 从测试集中划分多少比例给训练集,

validation_freq = 测试的epoch间隔数)

实例代码 :
#第一步,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正则化
])

#第四步,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() #打印神经网络结构,统计参数数目
结果为:
E:\Anaconda3\envs\TF2\python.exe C:/Users/Administrator/PycharmProjects/untitled8/keras实现iris数据集.py
Using TensorFlow backend.
Train on 120 samples, validate on 30 samples
Epoch 1/500

32/120 [=======>......................] - ETA: 2s - loss: 5.2685 - sparse_categorical_accuracy: 0.4375
120/120 [==============================] - 1s 8ms/sample - loss: 2.7204 - sparse_categorical_accuracy: 0.4833
Epoch 2/500

32/120 [=======>......................] - ETA: 0s - loss: 0.8763 - sparse_categorical_accuracy: 0.6875
120/120 [==============================] - 0s 67us/sample - loss: 0.8910 - sparse_categorical_accuracy: 0.6500
Epoch 3/500

省略.....

32/120 [=======>......................] - ETA: 0s - loss: 0.3444 - sparse_categorical_accuracy: 0.9375
120/120 [==============================] - 0s 67us/sample - loss: 0.3559 - sparse_categorical_accuracy: 0.9333
Epoch 500/500

32/120 [=======>......................] - ETA: 0s - loss: 0.3086 - sparse_categorical_accuracy: 0.9688
120/120 [==============================] - 0s 150us/sample - loss: 0.3302 - sparse_categorical_accuracy: 0.9833 - val_loss: 0.3695 - val_sparse_categorical_accuracy: 0.9333
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) multiple 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________

Process finished with exit code 0

————————————————
版权声明:本文为CSDN博主「yunfeather」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yunfeather/article/details/106463327

相关文章

MNIST数据集可以说是深度学习的入门,但是使用模型预测单张M...
1、新建tensorflow环境(1)打开anacondaprompt,输入命令行...
这篇文章主要介绍“张量tensor是什么”,在日常操作中,相信...
tensorflow中model.fit()用法model.fit()方法用于执行训练过...
https://blog.csdn.net/To_be_little/article/details/12443...
根据身高推测体重const$=require('jquery');const...