如图上图所示:
step1:
先对happy按着字母顺序编码,a=0、h=1、p=2、y=3,这个编码很其实在电脑里就代表个位置,因为电脑都是以二进制存储,
step2:
happy的编码出来了:10223
step3:
happy的电脑储存形式就是:
h=1=0100(电脑数数是从0开始的)
a=0=1000
p=2=0010
p=2=0010
y=3=0001
好啦!这个就存储好了,看不懂来找我!!!
代码:
import torch
# Paramaters
#SEQ_LEN表示序列的长度,即在时间序列数据中每个序列包含的时间步数。
#BATCH_SIZE表示批量大小,即一次训练所处理的序列数量。
#INPUT_SIZE表示每个时间步的输入特征数量,这通常是独热编码后的特征维度。
SEQ_LEN = 5
BATCH_SIZE = 1
HIDDEN_SIZE = 4
INPUT_SIZE = 4
NUM_LAYERS = 1
# Prepare for the data
words_bag = ['a', 'h', 'p', 'y']
one_hot_bag = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]]
input = [1, 0, 2, 2, 3]
target = [2, 1, 0, 3, 2]
input_one_hot = [one_hot_bag[x] for x in input]
inputs = torch.Tensor(input_one_hot).view(SEQ_LEN, BATCH_SIZE, INPUT_SIZE)
targets = torch.LongTensor(target)
print('inputs.shape', inputs, 'targets.shape', targets)