In [1]:
import pandas as pd
import codecs
import torch
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader, TensorDataset
import numpy as np
import pandas as pd
import random
import re
from transformers import BertTokenizer
from transformers import BertForSequenceClassification, get_linear_schedule_with_warmup
from torch.optim import AdamW
from torch.nn.functional import softmax 
C:\Program Files\Python311\Lib\site-packages\threadpoolctl.py:1226: RuntimeWarning: 
Found Intel OpenMP ('libiomp') and LLVM OpenMP ('libomp') loaded at
the same time. Both libraries are known to be incompatible and this
can cause random crashes or deadlocks on Linux when loaded in the
same Python program.
Using threadpoolctl may cause crashes or deadlocks. For more
information and possible workarounds, please see
    https://github.com/joblib/threadpoolctl/blob/master/multiple_openmp.md

  warnings.warn(msg, RuntimeWarning)
In [ ]:
df = pd.read_csv('1.csv')
df.replace(np.nan, 0, inplace=True)
# 标签
list = []
for i in df['need']:
    list.append(i)
news_label = list
news_label
In [ ]:
# 文本
list = []
for i in df['comment']:
    list.append(i)
news_text = list
news_text
In [2]:
df = pd.read_excel('sample.xlsx')
df.replace(np.nan, 0, inplace=True)
# 标签
list = []
for i in df['专利类型(1替代型,0节约型)']:
    list.append(i)
news_label = list
# 文本
list = []
for i in df['专利摘要']:
    list.append(i)
news_text = list
In [3]:
# 划分为训练集和验证集
# stratify 按照标签进行采样,训练集和验证部分同分布
x_train, x_test, train_label, test_label =  train_test_split(news_text[:], 
                      news_label[:], test_size=0.2, stratify=news_label[:])
In [4]:
# 分词器,词典
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
train_encoding = tokenizer(x_train, truncation=True, padding=True, max_length=64)
test_encoding = tokenizer(x_test, truncation=True, padding=True, max_length=64)
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
In [5]:
# 数据集读取
class NewsDataset(Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels
    
    # 读取单个样本
    def __getitem__(self, idx):
        item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
        item['labels'] = torch.tensor(int(self.labels[idx]))
        return item
    
    def __len__(self):
        return len(self.labels)

train_dataset = NewsDataset(train_encoding, train_label)
test_dataset = NewsDataset(test_encoding, test_label)
In [6]:
model = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=2)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# 单个读取到批量读取
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
test_dataloader = DataLoader(test_dataset, batch_size=16, shuffle=True)

# 优化方法
optim = AdamW(model.parameters(), lr=2e-5)
total_steps = len(train_loader) * 1
scheduler = get_linear_schedule_with_warmup(optim, 
                                            num_warmup_steps = 0, # Default value in run_glue.py
                                            num_training_steps = total_steps)
Loading weights:   0%|          | 0/199 [00:00<?, ?it/s]
BertForSequenceClassification LOAD REPORT from: bert-base-chinese
Key                                        | Status     | 
-------------------------------------------+------------+-
cls.seq_relationship.bias                  | UNEXPECTED | 
cls.predictions.transform.LayerNorm.weight | UNEXPECTED | 
cls.predictions.transform.LayerNorm.bias   | UNEXPECTED | 
cls.predictions.bias                       | UNEXPECTED | 
cls.predictions.transform.dense.weight     | UNEXPECTED | 
cls.seq_relationship.weight                | UNEXPECTED | 
cls.predictions.transform.dense.bias       | UNEXPECTED | 
classifier.weight                          | MISSING    | 
classifier.bias                            | MISSING    | 

Notes:
- UNEXPECTED:	can be ignored when loading from different task/architecture; not ok if you expect identical arch.
- MISSING:	those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.
In [46]:
def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()    # 取出最大值对应的索引
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)
# 训练函数
def train():
    model.train()
    total_train_loss = 0
    iter_num = 0
    total_iter = len(train_loader)
    for batch in train_loader:
        # 正向传播
        optim.zero_grad()
        input_ids = batch['input_ids'].to(device)
        attention_mask = batch['attention_mask'].to(device)
        labels = batch['labels'].to(device)
        outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
        loss = outputs[0]
        total_train_loss += loss.item()
        
        # 反向梯度信息
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        
        # 参数更新
        optim.step()
        scheduler.step()

        iter_num += 1
        if(iter_num % 100==0):
            print("epoth: %d, iter_num: %d, loss: %.4f, %.2f%%" % (epoch, iter_num, loss.item(), iter_num/total_iter*100))
        
    print("Epoch: %d, Average training loss: %.4f"%(epoch, total_train_loss/len(train_loader)))
    
def validation():
    model.eval()
    total_eval_accuracy = 0
    total_eval_loss = 0
    for batch in test_dataloader:
        with torch.no_grad():
            # 正常传播
            input_ids = batch['input_ids'].to(device)
            attention_mask = batch['attention_mask'].to(device)
            labels = batch['labels'].to(device)
            outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
        
        loss = outputs[0]
        logits = outputs[1]

        total_eval_loss += loss.item()
        logits = logits.detach().cpu().numpy()
        label_ids = labels.to('cpu').numpy()
        total_eval_accuracy += flat_accuracy(logits, label_ids)
        
    avg_val_accuracy = total_eval_accuracy / len(test_dataloader)
    print("Accuracy: %.4f" % (avg_val_accuracy))
    print("Average testing loss: %.4f"%(total_eval_loss/len(test_dataloader)))
    print("-------------------------------")
    

for epoch in range(11):
    print("------------Epoch: %d ----------------" % epoch)
    train()
    validation()
------------Epoch: 0 ----------------
Epoch: 0, Average training loss: 0.7383
Accuracy: 0.7917
Average testing loss: 0.6004
-------------------------------
------------Epoch: 1 ----------------
Epoch: 1, Average training loss: 0.6226
Accuracy: 0.7500
Average testing loss: 0.6135
-------------------------------
------------Epoch: 2 ----------------
Epoch: 2, Average training loss: 0.6368
Accuracy: 0.7708
Average testing loss: 0.6186
-------------------------------
------------Epoch: 3 ----------------
Epoch: 3, Average training loss: 0.6157
Accuracy: 0.7500
Average testing loss: 0.6248
-------------------------------
------------Epoch: 4 ----------------
Epoch: 4, Average training loss: 0.5959
Accuracy: 0.7500
Average testing loss: 0.6238
-------------------------------
------------Epoch: 5 ----------------
Epoch: 5, Average training loss: 0.5956
Accuracy: 0.7917
Average testing loss: 0.6016
-------------------------------
------------Epoch: 6 ----------------
Epoch: 6, Average training loss: 0.6087
Accuracy: 0.7917
Average testing loss: 0.6011
-------------------------------
------------Epoch: 7 ----------------
Epoch: 7, Average training loss: 0.6159
Accuracy: 0.7708
Average testing loss: 0.6197
-------------------------------
------------Epoch: 8 ----------------
Epoch: 8, Average training loss: 0.6155
Accuracy: 0.7708
Average testing loss: 0.6082
-------------------------------
------------Epoch: 9 ----------------
Epoch: 9, Average training loss: 0.6092
Accuracy: 0.7708
Average testing loss: 0.6178
-------------------------------
------------Epoch: 10 ----------------
Epoch: 10, Average training loss: 0.6055
Accuracy: 0.7708
Average testing loss: 0.6146
-------------------------------
In [47]:
text = ['一种医疗终端和用于其的数据接收方法及装置、存储介质,所述数据接收方法包括:监听来自于医疗终端外部的物联网设备的设备数据,其中,所述设备数据是所述物联网设备按照预设的时间间隔向所述医疗终端发送的;将接收到的所述设备数据进行数据处理和/或上传至服务器。采用本发明方案可以降低医疗终端外部的物联网设备的功耗并利于准确评估其功耗。 关注公众号“马 克 数 据 网”']
batch = tokenizer(text, truncation=True, padding=True, max_length=64)
batch["input_ids"] = torch.LongTensor(batch["input_ids"])
batch["attention_mask"] = torch.LongTensor(batch["attention_mask"])
model.eval()
with torch.no_grad():
    input_ids = batch['input_ids'].to(device)
    attention_mask = batch['attention_mask'].to(device)
    outputs = model(input_ids, attention_mask=attention_mask)
torch.nn.Softmax(-1)(outputs['logits'])
Out[47]:
tensor([[0.6031, 0.3969]], device='cuda:0')
In [48]:
df = pd.read_csv('d:/suibe403/专利/1985-2022_3571w/result/人工智能.csv')
df
Out[48]:
专利公开号 专利名称 专利类型 专利摘要 申请人 专利申请号 申请日 申请公布日 授权公布号 授权公布日 ... 优先权 国际申请 国际公布 代理人 省份或国家代码 法律状态 专利领域 专利学科 多次公布 所属国省
0 CN209167900U 一种姿态自调整的移动平衡装置 实用新型 本实用新型提供一种姿态自调整的移动平衡装置,其包括检测单元、控制单元、以及角度调整单元。本申... 上海太昂科技有限公司 CN201822247470.4 2018-12-28 NaN CN209167900U 2019-07-26 ... NaN NaN NaN 高彦 31 [{'legal_status_date': '2019-07-26', 'legal_st... 信息科技 自动化技术 NaN NaN
1 CN203616766U 光学指纹采集装置及便携式电子装置 实用新型 本实用新型公开了一种光学指纹采集装置及便携式电子装置。所述光学指纹采集装置,包括:指纹采集膜... 格科微电子(上海)有限公司 CN201320838454.7 2013-12-18 NaN CN203616766U 2014-05-28 ... NaN NaN NaN 吴靖靓;骆苏华 31 [{'legal_status_date': '2014-05-28', 'legal_st... 信息科技 计算机软件及计算机应用 NaN NaN
2 CN209962302U 一种掌经络生物识别的门禁控制系统 实用新型 本实用新型涉及门禁控制技术领域,具体公开了一种掌经络生物识别的门禁控制系统,包括壳体组件;所... 驿网无际(上海)信息科技有限公司 CN201921097656.4 2019-07-12 NaN CN209962302U 2020-01-17 ... NaN NaN NaN 郑海松 31 [{'legal_status_date': '2020-01-17', 'legal_st... 工程科技Ⅱ辑 仪器仪表工业 NaN NaN
3 CN216748828U 一种带有防撞结构的门禁闸机 实用新型 本实用新型公开了一种带有防撞结构的门禁闸机,包括底座、顶板和挡板,底座的顶端固定有主体,且主... 上海崴屿自动化控制系统有限公司 CN202220317113.4 2022-02-17 NaN CN216748828U 2022-06-14 ... NaN NaN NaN 衣然 31 [{'legal_status_date': '2022-06-14', 'legal_st... 工程科技Ⅱ辑 仪器仪表工业 NaN NaN
4 CN209343351U 一种便于安装的虹膜识别仪 实用新型 本实用新型公开了一种便于安装的虹膜识别仪,包括主机,所述主机的外侧设置有安装盒,安装盒的两侧... 上海百豪新材料有限公司 CN201920063999.2 2019-01-15 NaN CN209343351U 2019-09-03 ... NaN NaN NaN 赵俊寅 31 [{'legal_status_date': '2019-09-03', 'legal_st... 信息科技 计算机软件及计算机应用 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
122702 CN206224647U 一种翼闸门禁管理系统 实用新型 本实用新型公开了一种翼闸门禁管理系统,包括控制开关、计算器、打印机、出门按钮、供能装置、翼闸... 黑龙江联益智能系统股份有限公司 CN201621329406.5 2016-12-06 NaN CN206224647U 2017-06-06 ... NaN NaN NaN 陈方舟 23 [{'legal_status_date': '2017-06-06', 'legal_st... 工程科技Ⅱ辑 仪器仪表工业 NaN NaN
122703 CN213844136U 一种翻译笔连接装置 实用新型 本实用新型公开了一种翻译笔连接装置,包括顶盖、上安装罩和下安装罩,所述下安装罩的底端固定在底... 齐齐哈尔医学院 CN202120125632.6 2021-01-18 NaN CN213844136U 2021-07-30 ... NaN NaN NaN 李燕妮 23 [{'legal_status_date': '2021-07-30', 'legal_st... 工程科技Ⅱ辑 电力工业 NaN NaN
122704 CN213459075U 一种基于物联网的智慧医疗控制装置 实用新型 本实用新型涉及智慧医疗控制装置技术领域,公开了一种基于物联网的智慧医疗控制装置,包括:床板,... 哈尔滨惠软科技发展有限公司 CN202023022425.2 2020-12-15 NaN CN213459075U 2021-06-15 ... NaN NaN NaN 汪浩 23 [{'legal_status_date': '2021-06-15', 'legal_st... 工程科技Ⅱ辑 工业通用技术及设备 NaN NaN
122705 CN214586899U 一种便携式商务英语翻译装置 实用新型 本实用新型公开了一种便携式商务英语翻译装置,包括机盖,所述机盖底部的左右两侧均设置限位槽,所... 张洋 CN202121158593.6 2021-05-27 NaN CN214586899U 2021-11-02 ... NaN NaN NaN 胡海山 23 [{'legal_status_date': '2021-11-02', 'legal_st... 工程科技Ⅱ辑 电力工业 NaN NaN
122706 CN206209602U 一种计算机用智能无线数据采集VR终端 实用新型 本实用新型公开了一种计算机用智能无线数据采集VR终端,计算机上设有信号输出端,计算机的外界环... 齐齐哈尔齐三机床有限公司 CN201621208044.4 2016-11-09 NaN CN206209602U 2017-05-31 ... NaN NaN NaN 张伟 23 [{'legal_status_date': '2017-05-31', 'legal_st... 信息科技 计算机硬件技术 NaN NaN

122707 rows × 27 columns

In [51]:
# 创建存储结果的列表
save_prob = []

# 循环处理每个专利摘要
for text in df['专利摘要']:
    # 文本预处理和编码
    batch = tokenizer(text, truncation=True, padding=True, max_length=64, return_tensors="pt")
    
    # 将数据移动到GPU
    input_ids = batch['input_ids'].to(device)
    attention_mask = batch['attention_mask'].to(device)
    
    # 模型预测
    with torch.no_grad():
        outputs = model(input_ids, attention_mask=attention_mask)
    
    # 计算softmax概率
    probs = softmax(outputs.logits, dim=-1)
    
    # 将结果从GPU移回CPU并转换为numpy数组
    probs = probs.cpu().numpy()[0]
    save_prob.append(probs)

# 将结果转换为DataFrame
prob_df = pd.DataFrame(save_prob, columns=['劳动节约型概率', '劳动替代型概率'])

# 将结果合并到原始DataFrame
df = pd.concat([df, prob_df], axis=1)

# 显示结果
print(df[['专利摘要', '劳动节约型概率', '劳动替代型概率']].head())
                                                专利摘要   劳动节约型概率   劳动替代型概率
0  本实用新型提供一种姿态自调整的移动平衡装置,其包括检测单元、控制单元、以及角度调整单元。本申...  0.417426  0.582574
1  本实用新型公开了一种光学指纹采集装置及便携式电子装置。所述光学指纹采集装置,包括:指纹采集膜...  0.375457  0.624543
2  本实用新型涉及门禁控制技术领域,具体公开了一种掌经络生物识别的门禁控制系统,包括壳体组件;所...  0.364523  0.635477
3  本实用新型公开了一种带有防撞结构的门禁闸机,包括底座、顶板和挡板,底座的顶端固定有主体,且主...  0.383209  0.616791
4  本实用新型公开了一种便于安装的虹膜识别仪,包括主机,所述主机的外侧设置有安装盒,安装盒的两侧...  0.362513  0.637487
In [53]:
df.to_csv('d:/suibe403/专利/1985-2022_3571w/result/人工智能.csv', index=False)
In [55]:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 解决中文无法显示的问题
# 转换申请日为datetime格式并提取年份
df['申请日'] = pd.to_datetime(df['申请日'])
df['申请年份'] = df['申请日'].dt.year

# 判断专利类型
df['专利类型分类'] = df.apply(lambda x: '劳动替代型' if x['劳动替代型概率'] > x['劳动节约型概率'] else '劳动节约型', axis=1)

# 按年份统计
yearly_counts = df.groupby(['申请年份', '专利类型分类']).size().unstack(fill_value=0)

# 绘制折线图
plt.figure(figsize=(10, 6))
for col in yearly_counts.columns:
    plt.plot(yearly_counts.index, yearly_counts[col], marker='o', label=col)

plt.title('劳动节约型与劳动替代型专利年度趋势')
plt.xlabel('年份')
plt.ylabel('专利数量')
plt.legend()
plt.grid(True)
plt.show()
No description has been provided for this image
In [ ]: