You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Keras2(Theano后端)训练的样本分配差异化权重?

How to Assign Lower Weights to Specific Training Samples in Keras (Theano Backend)

Great question! When working with time series data fed into a Conv1D layer—where you can’t just discard low-importance samples—adjusting sample weights during training is exactly the right approach. There are two practical ways to do this in Keras with the Theano backend, depending on your needs:

方法一:使用Keras内置的sample_weight参数(最简洁)

If your sample weights are fixed (i.e., you know upfront which samples should have lower impact), Keras’ built-in sample_weight parameter is the easiest solution. This lets you pass an array where each value corresponds to a training sample’s weight—higher values mean the sample contributes more to loss calculation, while lower values reduce its influence.

Example Implementation

import numpy as np
from keras.models import Model
from keras.layers import Input, Conv1D, Dense, Flatten

# 模拟时间序列数据:形状为 (样本数, 时间步长, 特征数)
X = np.random.rand(1000, 50, 10)
y = np.random.randint(0, 2, (1000, 1))  # 二分类标签

# 定义样本权重:前800个为重要样本(权重1),后200个为低重要性样本(权重0.2)
sample_weights = np.ones(1000)
sample_weights[800:] = 0.2

# 构建Conv1D模型
inputs = Input(shape=(50, 10))
x = Conv1D(filters=32, kernel_size=3, activation='relu')(inputs)
x = Flatten()(x)
outputs = Dense(1, activation='sigmoid')(x)

model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='binary_crossentropy')

# 训练时传入sample_weight参数
model.fit(
    X, y,
    sample_weight=sample_weights,
    epochs=10,
    batch_size=32
)

This works seamlessly with the Theano backend—Keras handles the weight multiplication under the hood, so you don’t need to modify your loss function at all.

方法二:自定义带样本权重的损失函数(更灵活)

If you need dynamic weights (e.g., weights calculated from input data itself, or changing during training), create a custom loss function that incorporates sample weights. You’ll add an extra input layer to pass in weights, then scale the loss by these values.

Example Implementation

import numpy as np
from keras.models import Model
from keras.layers import Input, Conv1D, Dense, Flatten
from keras import backend as K

# 模拟数据
X = np.random.rand(1000, 50, 10)
y = np.random.randint(0, 2, (1000, 1))
sample_weights = np.ones(1000)
sample_weights[800:] = 0.2
sample_weights = sample_weights.reshape(-1, 1)  # 调整形状匹配输入层

# 自定义加权损失函数
def weighted_binary_crossentropy(y_true, y_pred):
    # 获取样本权重(从额外输入层传入)
    weights = K.placeholder(shape=(None, 1))
    # 计算基础二元交叉熵损失
    base_loss = K.binary_crossentropy(y_true, y_pred)
    # 乘以样本权重后取均值
    weighted_loss = K.mean(base_loss * weights)
    return weighted_loss

# 构建包含权重输入的模型
time_series_input = Input(shape=(50, 10))
weight_input = Input(shape=(1,))

x = Conv1D(filters=32, kernel_size=3, activation='relu')(time_series_input)
x = Flatten()(x)
outputs = Dense(1, activation='sigmoid')(x)

# 模型接受两个输入:时间序列数据和样本权重
model = Model(inputs=[time_series_input, weight_input], outputs=outputs)
model.compile(optimizer='adam', loss=weighted_binary_crossentropy)

# 训练时传入两个输入
model.fit(
    [X, sample_weights], y,
    epochs=10,
    batch_size=32
)

Key Notes for Theano Backend

  • Always use Keras backend functions (like K.mean, K.binary_crossentropy) instead of raw NumPy operations—this ensures compatibility with Theano’s computation graph.
  • For fully dynamic weights (e.g., based on input features), you can calculate weights directly within the model graph instead of passing them as a separate input.

内容的提问来源于stack exchange,提问作者ascripter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:01:16