keras字符编码
2024-08-30 04:24:41
https://www.jianshu.com/p/258a21ae0390
https://blog.csdn.net/apengpengpeng/article/details/80866034
#-*-coding:utf-8-*-
# import numpy as np
#
# samples = ['The cat sat on the mat.', 'The dog ate my homework.']
#
# # 10
# # 定义一个集合,得到{'The': 1, 'cat': 2, 'sat': 3, 'on': 4, 'the': 5, 'mat.': 6, 'dog': 7, 'ate': 8, 'my': 9, 'homework.': 10},也就是筛选出这个句子中对应的了哪些词,然后并赋予索引值,其实就是个词库
# token_index = {}
# for sample in samples:
# for word in sample.split():
# if word not in token_index:
# token_index[word] = len(token_index) + 1
#
# # 限制了读取的句子的长度,一句话最长10个词
# print(token_index)
# max_length = 10
# results = np.zeros(shape=(len(samples),
# max_length,
# max(token_index.values()) + 1))
#
# # print(results) 2, 10, 11
# for i, sample in enumerate(samples):
# for j, word in list(enumerate(sample.split()))[:max_length]:
# index = token_index.get(word)
# results[i, j, index] = 1.
# print(results) import numpy as np
import string
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
# 预先定义一个字符集 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\\'()*+,-./:;<=>?@[\\]^_`{|}~‘
characters = string.printable
token_index = dict(zip(range(1, len(characters) + 1), characters)) max_length = 50
results = np.zeros((len(samples), max_length, max(token_index.keys()) + 1))
for i, sample in enumerate(samples):
for j, character in enumerate(sample):
for key, value in token_index.items():
if value == character:
index = key
results[i, j, index] = 1. print(results)
最新文章
- ABP源码分析四十四:ZERO的配置
- 使用nginx为ArcGIS Server做反向代理
- ubuntu nginx 安装以及配置文件详解
- Simple GDB case
- opencv的一次性配置
- DDD的好文章
- Gulp-入门教程 搭配环境
- 【数学相关、规律】Codeforces 696B Puzzles
- Altium designer 10如何设置标题栏
- wemall app商城源码中基于JAVA通过Http请求获取json字符串的代码
- hdu3756三分基础题
- 2017年总结的前端文章——border属性的多方位应用和实现自适应三角形
- trigger click 和 click 的区别??
- Android开发艺术探索——第二章:IPC机制(中)
- python用类实现xrange
- Java基础8:深入理解内部类
- Python——线程1
- 机器学习基石笔记:09 Linear Regression
- [Android Pro] AndroidX了解一下
- Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪?(已解决)
热门文章
- HDU - 4082 Hou Yi&#39;s secret
- Arduino -- variables
- 刷题48. Rotate Image
- 量化投资_Multicharts数组操作函数_zeros()设定数组元素为0(自定义)
- iOS 保存图片(视频)到相册
- 完美的代价(swap成回文串、贪心)
- 2,StructuredStreaming的事件时间和窗口操作
- 不使用.h .lib文件使用DLL内的函数
- ABP 切换mysql 数据库报错mysqlexception: incorrect string value: ‘\xe7\xae\x80\xe4\xbd\x93…’ for column display name
- iOS 中的延时操作方法