使用 CRF 进行多元二元序列预测

2023-12-21

这个问题是一个延伸this one https://stackoverflow.com/questions/53977695/multivariate-binary-sequence-prediction-with-lstm其重点是 LSTM 而不是 CRF。不幸的是,我没有任何 CRF 经验,这就是我问这些问题的原因。

Problem:

我想预测多个非独立组的二进制信号序列。我的数据集相当小(每组约 1000 条记录),所以我想在这里尝试 CRF 模型。

可用数据:

我有一个包含以下变量的数据集:

  1. 时间戳
  2. Group
  3. 表示活动的二进制信号

我想使用这个数据集进行预测group_a_activity and group_b_activity均为 0 或 1。

请注意,这些组被认为是互相关的,并且可以从时间戳中提取附加特征 - 为了简单起见,我们可以假设我们从时间戳中只提取 1 个特征。

到目前为止我所拥有的:

这是您可以在自己的机器上重现的数据设置。

# libraries
import re
import numpy as np
import pandas as pd

data_length = 18  # how long our data series will be
shift_length = 3  # how long of a sequence do we want

df = (pd.DataFrame  # create a sample dataframe
    .from_records(np.random.randint(2, size=[data_length, 3]))
    .rename(columns={0:'a', 1:'b', 2:'extra'}))
df.head()  # check it out

# shift (assuming data is sorted already)
colrange = df.columns
shift_range = [_ for _ in range(-shift_length, shift_length+1) if _ != 0]
for c in colrange:
    for s in shift_range:
        if not (c == 'extra' and s > 0):
            charge = 'next' if s > 0 else 'last'  # 'next' variables is what we want to predict
            formatted_s = '{0:02d}'.format(abs(s))
            new_var = '{var}_{charge}_{n}'.format(var=c, charge=charge, n=formatted_s)
            df[new_var] = df[c].shift(s)

# drop unnecessary variables and trim missings generated by the shift operation
df.dropna(axis=0, inplace=True)
df.drop(colrange, axis=1, inplace=True)
df = df.astype(int)
df.head()  # check it out

#   a_last_03  a_last_02      ...        extra_last_02  extra_last_01
# 3          0          1      ...                    0              1
# 4          1          0      ...                    0              0
# 5          0          1      ...                    1              0
# 6          0          0      ...                    0              1
# 7          0          0      ...                    1              0
[5 rows x 15 columns]

在我们进入 CRF 部分之前,我怀疑我无法从多任务学习的角度来解决这个问题(通过一个模型预测 A 和 B 的模式),因此我必须预测每一个他们单独。

现在是 CRF 部分。我找到了一些相关的例子(这里是one https://github.com/scrapinghub/python-crfsuite/blob/master/examples/CoNLL%202002.ipynb)但它们都倾向于根据先验序列预测单个类值。

这是我在这里使用 CRF 的尝试:

import pycrfsuite

crf_features = []  # a container for features
crf_labels = []  # a container for response
# lets focus on group A only for this one
current_response = [c for c in df.columns if c.startswith('a_next')]
# predictors are going to have to be nested otherwise I'll run into problems with dimensions
current_predictors = [c for c in df.columns if not 'next' in c]
current_predictors = set([re.sub('_\d+$','',v) for v in current_predictors])
for index, row in df.iterrows():
    # not sure if its an effective way to iterate over a DF...
    iter_features = []
    for p in current_predictors:
        pred_feature = []
        # note that 0/1 values have to be converted into booleans
        for k in range(shift_length):
            iter_pred_feature = p + '_{0:02d}'.format(k+1)
            pred_feature.append(p + "=" + str(bool(row[iter_pred_feature])))
        iter_features.append(pred_feature)
    iter_response = [row[current_response].apply(lambda z: str(bool(z))).tolist()]
    crf_labels.extend(iter_response)
    crf_features.append(iter_features)

trainer = pycrfsuite.Trainer(verbose=True)
for xseq, yseq in zip(crf_features, crf_labels):
    trainer.append(xseq, yseq)

trainer.set_params({
    'c1': 0.0,   # coefficient for L1 penalty
    'c2': 0.0,  # coefficient for L2 penalty
    'max_iterations': 10,  # stop earlier
    # include transitions that are possible, but not observed
    'feature.possible_transitions': True
})

trainer.train('testcrf.crfsuite')
tagger = pycrfsuite.Tagger()
tagger.open('testcrf.crfsuite')
tagger.tag(xseq)
# ['False', 'True', 'False']

看来我确实设法让它工作,但我不确定我是否正确地处理它。我将在“问题”部分中提出我的问题,但首先,这是一种使用的替代方法keras_contrib包裹:

from keras import Sequential
from keras_contrib.layers import CRF
from keras_contrib.losses import crf_loss

# we are gonna have to revisit data prep stage again
# separate predictors and response
response_df_dict = {}
for g in ['a','b']:
    response_df_dict[g] = df[[c for c in df.columns if 'next' in c and g in c]]

# reformat for LSTM
# the response for every row is a matrix with depth of 2 (the number of groups) and width = shift_length
# the predictors are of the same dimensions except the depth is not 2 but the number of predictors that we have

response_array_list = []
col_prefix = set([re.sub('_\d+$','',c) for c in df.columns if 'next' not in c])
for c in col_prefix:
    current_array = df[[z for z in df.columns if z.startswith(c)]].values
    response_array_list.append(current_array)

# reshape into samples (1), time stamps (2) and channels/variables (0)
response_array = np.array([response_df_dict['a'].values,response_df_dict['b'].values])
response_array = np.reshape(response_array, (response_array.shape[1], response_array.shape[2], response_array.shape[0]))
predictor_array = np.array(response_array_list)
predictor_array = np.reshape(predictor_array, (predictor_array.shape[1], predictor_array.shape[2], predictor_array.shape[0]))

model = Sequential()
model.add(CRF(2, input_shape=(predictor_array.shape[1],predictor_array.shape[2])))
model.summary()
model.compile(loss=crf_loss, optimizer='adam', metrics=['accuracy'])
model.fit(predictor_array, response_array, epochs=10, batch_size=1)
model_preds = model.predict(predictor_array)  # not gonna worry about train/test split here

问题:

我的主要问题是我是否正确构建了两个 CRF 模型。让我担心的是(1)关于 CRF 模型的文档并不多,(2)CRF 主要用于预测给定序列的单个标签,(3)输入特征是嵌套的,(4)什么时候以多任务方式使用,我不确定它是否有效。

我还有几个额外的问题:

  1. CRF 适合解决这个问题吗?
  2. 这两种方法怎么样(一种基于pycrfuite以及一个基于keras_contrib)不同,它们的优点/缺点是什么?
  3. 从更一般的意义上来说,将 CRF 和 LSTM 模型结合在一起有什么好处(就像讨论的那样)here https://www.depends-on-the-definition.com/sequence-tagging-lstm-crf/)

非常感谢!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 CRF 进行多元二元序列预测 的相关文章

  • 在相同任务上,Keras 比 TensorFlow 慢

    我正在使用 Python 运行斩首 DCNN 本例中为 Inception V3 来获取图像特征 我使用的是 Anaconda Py3 6 和 Windows7 使用 TensorFlow 时 我将会话保存在变量中 感谢 jdehesa 并
  • Keras:多类 NLP 任务中 model.evaluate 与 model.predict 的准确性差异

    我正在使用以下代码在 keras 中为 NLP 任务训练一个简单模型 训练集 测试集和验证集的变量名称是不言自明的 该数据集有 19 个类 因此网络的最后一层有 19 个输出 标签也是 one hot 编码的 nb classes 19 m
  • 如何使用一个模型中间层的输出作为另一个模型的输入?

    我训练一个模型A并尝试使用中间层的输出name layer x 作为模型的附加输入B 我尝试像 Keras 文档一样使用中间层的输出https keras io getting started faq how can i obtain th
  • 验证 Transformer 中多头注意力的实现

    我已经实施了MultiAttention head in Transformers 周围有太多的实现 所以很混乱 有人可以验证我的实施是否正确 DotProductAttention 引用自 https www tensorflow org
  • 为什么我的结果仍然无法重现?

    我想要为 CNN 获得可重复的结果 我使用带有 GPU 的 Keras 和 Google Colab 除了建议插入某些代码片段 这应该允许再现性 之外 我还在层中添加了种子 This is the first code snipped to
  • 在 keras 中保存和加载权重

    我试图从我训练过的模型中保存和加载权重 我用来保存模型的代码是 TensorBoard log dir output model fit generator image a b gen batch size steps per epoch
  • 混淆矩阵不支持多标签指示符

    multilabel indicator is not supported是我在尝试运行时收到的错误消息 confusion matrix y test predictions y test is a DataFrame其形状为 Horse
  • 将 tf.contrib.layers.xavier_initializer() 更改为 2.0.0

    我该如何改变 tf contrib layers xavier initializer tf 版本 gt 2 0 0 所有代码 W1 tf get variable W1 shape self input size h size initi
  • 在按顺序读取的多个特征文件上训练 Keras 模型以节省内存

    当我尝试读取大量功能文件时 我遇到了内存问题 见下文 我想我应该分割训练文件并按顺序读取它们 做到这一点的最佳方法是什么 x train np load path features x train npy y train np load p
  • Learning_rate 不是合法参数

    我正在尝试通过实现 GridSearchCV 来测试我的模型 但我似乎无法在 GridSearch 中添加学习率和动量作为参数 每当我尝试通过添加这些代码来执行代码时 我都会收到错误 这是我创建的模型 def define model op
  • 为什么平均百分比误差(mape)非常高?

    我已获得代码掌握机器学习 https machinelearningmastery com time series prediction lstm recurrent neural networks python keras 我修改了mod
  • 车辆分割和跟踪

    我已经从事一个项目一段时间了 目的是在无人机捕获的视频中检测和跟踪 移动 车辆 目前我正在使用 SVM 该 SVM 接受了从车辆和背景图像中提取的局部特征的特征袋表示的训练 然后 我使用滑动窗口检测方法来尝试定位图像中的车辆 然后我想要跟踪
  • keras:zca 美白卡住了 train_datagen.fit()

    我尝试将 zca whitening 与 keras 图像处理选项一起使用 但计算陷入困境并且永远不会结束 我导致问题的代码部分如下所示 train datagen ImageDataGenerator rotation range 30
  • model.predict() 返回类而不是概率

    Hello 我是第一次使用 Keras 我训练并保存了一个模型 作为 json 文件及其权重 该模型旨在将图像分为 3 个类别 我的编译方法 model compile loss categorical crossentropy optim
  • 在 R 中使用深度网络和 MNIST 数据读取手写数字第 3 部分

    我尝试编写一个基于深度网络的程序来读取手写数字 我在 Youtube 上找到了一个代码 https www youtube com watch v 5bso 5X7Zu4 https www youtube com watch v 5bso
  • Keras - Nan 总结直方图 LSTM

    我使用 Keras 编写了一个 LSTM 模型 并使用 LeakyReLU 高级激活 ADAM Optimizer with learning rate decay opt optimizers Adam lr 0 0001 beta 1
  • 使用决策树

    我知道 tl dr 我将尝试解释我的问题 而不会用大量蹩脚的代码来打扰您 我正在做一项学校作业 我们有蓝精灵的图片 我们必须通过前景背景分析来找到它们 我有一个 Java 决策树 其中包含所有数据 HSV 直方图 1 一个节点 然后尝试找到
  • Colab 上没有名为“tensorflow.compat.v2”的模块

    我正在关注这个https thebinarynotes com how to train mask r cnn on the custom dataset https thebinarynotes com how to train mask
  • 批量大小不适用于带有deploy.prototxt的caffe

    我正在努力让我的分类过程更快一些 我想增加我的deploy prototxt中的第一个input dim 但这似乎不起作用 甚至比对每张图像进行分类还要慢一点 部署 prototxt input data input dim 128 inp
  • 在NN中指定连接(在keras中)

    我正在使用 keras 和tensorflow 1 4 我想明确指定哪些神经元在两层之间连接 因此 每当第一层中的神经元 i 连接到第二层中的神经元 j 且其他地方为零时 我就有一个矩阵 A 其中包含 1 我的第一次尝试是创建一个带有内核的

随机推荐

  • Windows 上的 django-admin.py 和 virtualenv 问题

    在我的系统中 系统范围内安装了 Django 1 2 3 C gt python c import django print django get version 1 2 3 C gt django admin py version 1 2
  • 如何将 MySQL 设置为 Rails 3 中的默认数据库?

    我去年 4 月开始使用 Rails 2 但今年 6 月停止使用 因为我认为在 Rails 3 发布时学习它会更实用 因为它的很多内容都被完全重构和重组 我曾经使用 Ubuntu 10 04 使用 SQLite3 作为默认数据库 但现在我使用
  • 部分表单类 C# - 仅显示类的代码视图

    我有一个用 C 编写的空白表单项目 我想将函数和事件分离到表单类上的不同代码文件中 为了当它变得很大并且多人在 CodeControl 上使用它时使其更易于管理 所以我创造了 Form1 Functions cs Form1 Events
  • 如何解决 React Native run-android 中的“您的项目中需要有 MainApplication”

    我从博览会上被驱逐 并尝试react native run android 我收到此错误 FAILURE Build failed with an exception Where Script C Users simil OneDrive
  • 单一类型特征中的双向链接

    我正在尝试创建一个特征 它实现具有双向链接的树 以便当节点添加父节点时 该节点将添加到父节点的子节点中 我得到的错误如下 类型不匹配 找到 PolyTree this type 具有基础类型 PolyTree T 必需 T 知道为什么此代码
  • HTML5 / JS 线性仪表 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想显示一个线性仪表 如融合图 http docs fusioncharts com flex charts Contents Wid
  • 从类方法调用实例方法

    所以我需要从 Objective C 中的类方法调用一些实例方法 例子 id barWithFoo NSFoo self foo Raises compiler warning void foo cool stuff 所以我的问题是 Sta
  • 通过 AT 命令终止语音通话

    我正在从事一个涉及 Arduino 和 TC35 GSM 模块的业余爱好项目 一切都很顺利 但我想知道是否有一个 AT 命令来终止语音通话 ATD 电话号码 因为我似乎无法在以下任何一个中找到一个我搜索过的文献 谢谢 戴夫 而 经典 命令A
  • 将 const 参数作为参数传递时出现隐秘错误消息

    所以我正在编写双向链表的实现 这是实现各个节点的类的构造函数 DNode DNode const int a key const DNode a prev const DNode a next key a key prev a prev n
  • 未捕获的 DOMException:无法在“存储”上执行“setItem”:设置“domains”的值超出了配额

    当我打开控制台时 每次在 Chrome 中都会显示此错误 未捕获的 DOMException 无法在 存储 上执行 setItem 设置 域名 的值超出了配额 在 HTMLScriptElement a onload https dl me
  • 在 Ruby 中如何调度对 instance_methods 的调用?

    instance methods被定义为公共实例方法Module班级 为什么以及如何我们能够调用Object instance methods 类方法调用的语法是什么 Because instance methods http www ru
  • 如何使用 Google Services Gradle 插件在 Circle CI 上测试 Android 应用程序

    我正在使用 Google Services Gradle 插件开发 Android 应用程序 Firebase 需要 并且该插件需要 google services json 我认为 json 文件不应该受到 git 的控制 因为它有一些应
  • d3js tree.nodes() 不是函数

    虽然下面的代码在 d3v3 中可以工作 但在 v4 中却失败了 var nodes tree nodes root reverse links tree links nodes 未捕获的类型错误 tree nodes 不是函数 v4 中它的
  • Stripe - 延迟市场应用程序中的传输

    我正在构建一个市场应用程序 我使用 Stripe 接受买家付款并将一定比例转给卖家 但是 条纹只能让您从条纹余额中转移 因此 如果我接受买家付款 且需要 2 个工作日才能显示在我的条带余额中 则我无法在销售发生时转移付款 我从 Stripe
  • 有没有办法在 CSS 中对齐多个表中的列?

    见下图 请注意 按日期分隔的两个表并未对齐 ZZ 列 XX 列和第三列 这是因为我们目前的实现方式是根据里面的文本改变列宽 我尝试使用固定宽度 但它在多个设备上看起来不太好 有没有办法我可以实现这个 以便 列是对齐的 无论内容如何 它都会通
  • Google REST API v3 - 修订:列表与“显示更详细的修订”

    我需要获取 Google 文档的修订的详细列表 使用 Google API Explorer 时 GET https www googleapis com drive v3 files fileId revisions 我只得到非详细列表
  • 开始检测活动以获得结果

    我有一项活动假设为 活动 A 我通过两种方式开始a StartActivity b StartActivityForResult 现在我有一些方法对于活动开始的方式有不同的行为 现在我想检测 活动 A 是否已启动以获得结果 那么我的问题是我
  • HTML5 历史 API 和书签

    如果 History API 创建的 URL 不是 真实 URL 那么使用 History API 有何意义 当然 我可以随心所欲地推送状态 但如果我不能将这些 URL 之一作为书签包含在某处 那还有什么意义呢 我真正的问题是 如何设置一个
  • 在 nextjs 中将 props 从一个页面传递到另一个页面

    我有一个虚拟项目 在我的项目中 我有两个页面 test1 和 test2 我想将一个道具从 page1 传递到 page2 我知道我可以使用 useRouter 钩子 但我不想将此道具设置为查询字符串 在我的 test1 页面中 我有一个颜
  • 使用 CRF 进行多元二元序列预测

    这个问题是一个延伸this one https stackoverflow com questions 53977695 multivariate binary sequence prediction with lstm其重点是 LSTM