Keras class_weight和sample_weight用法
搬运: https://stackoverflow.com/questions/57610804/when-is-the-timing-to-use-sample-weights-in-keras
import tensorflow as tf
import numpy as np
data_size = 100
input_size=3
classes=3
x_train = np.random.rand(data_size ,input_size)
y_train= np.random.randint(0,classes,data_size )
#sample_weight_train = np.random.rand(data_size)
x_val = np.random.rand(data_size ,input_size)
y_val= np.random.randint(0,classes,data_size )
#sample_weight_val = np.random.rand(data_size )
inputs = tf.keras.layers.Input(shape=(input_size))
pred=tf.keras.layers.Dense(classes, activation='softmax')(inputs)
model = tf.keras.models.Model(inputs=inputs, outputs=pred)
loss = tf.keras.losses.sparse_categorical_crossentropy
metrics = tf.keras.metrics.sparse_categorical_accuracy
model.compile(loss=loss , metrics=[metrics], optimizer='adam')
# Make model static, so we can compare it between different scenarios
for layer in model.layers:
layer.trainable = False
# base model no weights (same result as without class_weights)
# model.fit(x=x_train,y=y_train, validation_data=(x_val,y_val))
class_weights={0:1.,1:1.,2:1.}
model.fit(x=x_train,y=y_train, class_weight=class_weights, validation_data=(x_val,y_val))
# which outputs:
> loss: 1.1882 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1965 - val_sparse_categorical_accuracy: 0.3100
#changing the class weights to zero, to check which loss and metric that is affected
class_weights={0:0,1:0,2:0}
model.fit(x=x_train,y=y_train, class_weight=class_weights, validation_data=(x_val,y_val))
# which outputs:
> loss: 0.0000e+00 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1945 - val_sparse_categorical_accuracy: 0.3100
#changing the sample_weights to zero, to check which loss and metric that is affected
sample_weight_train = np.zeros(100)
sample_weight_val = np.zeros(100)
model.fit(x=x_train,y=y_train,sample_weight=sample_weight_train, validation_data=(x_val,y_val,sample_weight_val))
# which outputs:
> loss: 0.0000e+00 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1931 - val_sparse_categorical_accuracy: 0.3100
class_weight
: output 变量的权重
sample_weight
: data sample 的权重
Keras class_weight和sample_weight用法的更多相关文章
- keras中TimeDistributed的用法
TimeDistributed这个层还是比较难理解的.事实上通过这个层我们可以实现从二维像三维的过渡,甚至通过这个层的包装,我们可以实现图像分类视频分类的转化. 考虑一批32个样本,其中每个样本是一个 ...
- Keras 学习之旅(一)
软件环境(Windows): Visual Studio Anaconda CUDA MinGW-w64 conda install -c anaconda mingw libpython CNTK ...
- Keras官方中文文档:函数式模型API
\ 函数式模型接口 为什么叫"函数式模型",请查看"Keras新手指南"的相关部分 Keras的函数式模型为Model,即广义的拥有输入和输出的模型,我们使用M ...
- Keras官方中文文档:序贯模型API
Sequential模型接口 如果刚开始学习Sequential模型,请首先移步这里阅读文档,本节内容是Sequential的API和参数介绍. 常用Sequential属性 model.layers ...
- keras系列︱Sequential与Model模型、keras基本结构功能(一)
引自:http://blog.csdn.net/sinat_26917383/article/details/72857454 中文文档:http://keras-cn.readthedocs.io/ ...
- Python机器学习笔记:深入理解Keras中序贯模型和函数模型
先从sklearn说起吧,如果学习了sklearn的话,那么学习Keras相对来说比较容易.为什么这样说呢? 我们首先比较一下sklearn的机器学习大致使用流程和Keras的大致使用流程: skl ...
- 深度学习(六)keras常用函数学习
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9769301.html Keras是什么? Keras:基于Theano和TensorFlow的 ...
- Keras(一)Sequential与Model模型、Keras基本结构功能
keras介绍与基本的模型保存 思维导图 1.keras网络结构 2.keras网络配置 3.keras预处理功能 模型的节点信息提取 config = model.get_config() 把mod ...
- Keras Model Sequential模型接口
Sequential 模型 API 在阅读这片文档前,请先阅读 Keras Sequential 模型指引. Sequential 模型方法 compile compile(optimizer, lo ...
随机推荐
- MySQL 常见面试知识点
之前简单总结了一下MySQL的场景面试知识点 1.讲下MVCC原理 2.MySQL高可用架构介绍 3.OSC(在线更改表结构)原理 4.MySQL性能调优有哪些关键点/经验 5.MySQL在线备份方案 ...
- mysql 8.0.12 安装配置方法图文教程
一.安装 1.从网上下载MySQL8.0.12版本,下载地址 2. 下载完成后解压 我解压的路径是:D:\Java\mysql-8.0.12-winx64 3. 配置文件 首先在解压的路径下查看是否含 ...
- C++随笔(1)——关于C++11中的线程创建,join和detach
主要是和之前的博文有关,之前在这里有一部分代码是通过创建新的进程来应对新的用户请求的,但是基本没怎么解释怎么用的,所以这里做点小笔记. join 首先引入的库: #include <thread ...
- JsonProperty 使用
引入 依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> ...
- Editor placeholder in source code错误
When you insert code via autocompletion (or via a code snippet, sometimes), there may be placeholder ...
- Process进程 ProcessStartInfo.UseShellExecute 属性
https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-1.1/k7z89z41(v=vs.80) 启动进程示例: ...
- numpy使用数组进行数据处理
numpy使用数组进行数据处理 meshgrid函数 理解: 二维坐标系中,X轴可以取三个值1,2,3, Y轴可以取三个值7,8, 请问可以获得多少个点的坐标? 显而易见是6个: (1,7)(2,7) ...
- MySQL总结(4)
insert 数据的插入 INSERT插入数据
- linux tricks 之 ALIGN解析.
------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...
- NTFS,FAT32和exFAT文件系统的区别
NTFS,FAT32和exFAT文件系统的区别 本文所有资料来源于网络,仅做个人学习使用,如有侵权,请联系删除 1.什么是文件系统 文件系统是系统对文件的存放排列方式,不同格式的文件系统关系到数据是如 ...