【机器学习】 - TensorFlow.Keras 建立模型 model.evaluate 和 model.predict 的区别

一、概述

 

model.evaluate

函数原型:

evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None)

输入数据和标签,输出损失值和选定的指标值(如精确度accuracy)

	# 评估模型,不输出预测结果
	loss,accuracy = model.evaluate(X_test,Y_test)
	print('\ntest loss',loss)
	print('accuracy',accuracy)

另外,返回了多少个值,是不固定的,如果在complie的时候没有指定metrics的话,默认只有loss一个返回值。

可以使用model.metrics_names查看。

model.predict

输入测试数据,输出预测结果
(通常用在需要得到预测结果的时候,比如需要拿到结果来画图)

 

二、区别


1.输入输出不同
model.evaluate      输入数据(data)和真实标签(label),然后将预测结果与真实标签相比较,得到两者误差并输出.
model.predict         输入数据(data),输出预测结果
2
是否需要真实标签
model.evaluate       需要,因为需要比较预测结果与真实标签的误差
model.predict          不需要,只是单纯输出预测结果,全程不需要标签的参与。

 

三、附源码:

Returns the loss value & metrics values for the model in test mode.

Computation is done in batches.

Arguments

x: Numpy array of test data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs). If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays. x can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).

y: Numpy array of target (label) data (if the model has a single output), or list of Numpy arrays (if the model has multiple outputs). If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays. y can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
batch_size: Integer or None. Number of samples per evaluation step. If unspecified, batch_sizewill default to 32.

verbose: 0 or 1. Verbosity mode. 0 = silent, 1 = progress bar.

sample_weight: Optional Numpy array of weights for the test samples, used for weighting the loss function. You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specifysample_weight_mode="temporal" in compile().

steps: Integer or None. Total number of steps (batches of samples) before declaring the evaluation round finished. Ignored with the default value of None.
Returns

Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs.

参考链接:https://blog.csdn.net/DoReAGON/article/details/88552348

Keras官方文档:https://keras.io/models/model/#evaluate

  • 23
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Tensorflow2.1中的tf.keras.sequential是一种创建顺序模型的方法。顺序模型是最简单的神经网络模型,也是最常用的模型。在这个方法中,我们可以将一系列的层按照顺序添加到模型中。 首先,我们需要导入Tensorflow和tf.keras库。接下来,我们可以使用tf.keras.sequential()函数创建一个空的顺序模型。 接下来,我们可以使用add()方法依次添加各个层到模型中。例如,我们可以使用Dense层添加全连接层,使用Conv2D层添加卷积层,使用MaxPooling2D层添加池化层等。每个层都可以设置不同的参数,例如激活函数、输入大小、输出大小等。 在添加完所有层之后,我们可以使用compile()方法配置模型的优化器、损失函数和评估指标。优化器用于定义模型的训练方式,损失函数用于定义模型的优化目标,评估指标用于评估模型的性能。 最后,我们可以使用fit()方法来训练模型。在fit()方法中,我们需要传入训练数据和标签,并设置一些参数,例如训练轮数、批大小等。训练完成后,我们可以使用evaluate()方法评估模型在测试数据上的性能,使用predict()方法对新数据进行预测。 总而言之,tf.keras.sequential是Tensorflow2.1中创建顺序模型的一种方法,它可以方便地添加、配置和训练各种神经网络层,并用于解决各种机器学习和深度学习任务。它是一种非常有用且易于使用的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值