搬运: 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 的权重

搬运: https://stackoverflow.com/questions/57610804/when-is-the-timing-to-use-sample-weights-in-kerasimport tensorflow as tfimport numpy as npdata_size = 100input_size=3classes=3x_train = np... class_weightsample_weight 1、class_weight 对训练集中的每个类别加一个权重,如果是大类别样本多那么可以设置低的权重,反之可以设置大的权重值 2、sample_weight 对每个样本加权中,思路与上面类似。样本多的类别样本权重低 例如 model.fit(class_weight={0:1.,1:
数据预处理 在tf.tensorflow.preprocessing包中有数据预处理的函数,包括:image、Sequence、text。 以tf.tensorflow.preprocessing.image中的ImageDataGenerator为例: tf.keras.preprocessing.image.ImageDataGenerator( featurewise_center=False, # samplewise_center=False, featurewise
sample_weightkeras中的fit的参数,中文文档介绍如下: 简单点的解释如下:参考https://blog.csdn.net/weixin_40755306/article/details/82290033#commentBox sample_weight的作用就是为数据集中的数据分配不同的权重。
sample_weight对每一个sample的梯度,乘以对应的weight import tensorflow as tf from tensorflow import keras from keras.layers import * import numpy as np tf.random.set_seed(1) np.random.seed(1) x_input = Input(shape=(None, 1)) y_input = Input(shape=(None, 1)) ki = kera
模型训练中,如果希望模型更偏向某一类数据或更聚焦于某一批样本,可以采用对数据类别和数据加权的形式达到上述效果。 keras 默认设置下,样本的权重取决于其在数据集中的频率,即原始样本的分布。有两种方法可以对数据进行加权,而与采样频 Tips: 如下代码为方便测试跑通准备,分别构建了深度模型并加载了手写数字识别数据,可以直接忽略看后面~ def get_uncompiled_model(): inputs = keras.Input(shape=(784,).
对于二分类问题,评价指标可以用 binary_accuracy,就是最直观上讲的准确率。 当面对多分类或者多标签的任务时,评价度量可能会用到这两个 categorical_accuracy和 sparse_categorical_accuracy binary_accuracy自然不必多讲,这篇文章讲一下categorical_accuracy和 sp... tf.keras.metrics.SparseCategoricalAccuracy( name='sparse_categorical_accuracy', dtype=None SparseCategoricalAccuracy函数用于计算多分类问题的准确率。计算过程如下: def sparse_categorical_accuracy(y_true, y_pred): return K.cast(K.equal(K.max(y_true, axis=
LightGBM在多分类问题上,使用与sklearn决策树中参数class_weight='balanced'的算法一致,都使用 n_samples / (n_classes * np.bincount(y)) 但sklearn中对于binary二分类问题,同样使用n_samples / (n_classes * np.bincount(y))计算class_weight='balanced'; 在利用神经网络进行分类和识别的时候,使用了keras这个封装层次比较高的框架,backend使用的是tensorflow-cpu。 在交叉验证的时候,出现val_categorical_accuracy: 0.0000e+00的问题。 问题分析: 首先,弄清楚,训练集、验证集、测试集的区别,验证集是从训练集中提前拿出一部分的数据集。在keras中,一般...
not_y_pred=np.logical_not(y_pred) y_int1=y_true*y_pred y_int0=np.logical_not(y_true)*not_y_pred TP=np.sum(y_pred*y_int1) FP=np.sum(y_pred)-TP TN=np.sum(not_y_pred*y...