提交 7f75695d 编写于 作者: W wizardforcel

2021-01-15 23:27:49

上级 2bf7173f
......@@ -134,25 +134,23 @@
1. Import the following libraries:
将熊猫作为 pd 导入
将 numpy 导入为 np
进口火炬
```py
import pandas as pd
import numpy as np
import torch
```
2. Create a Pandas DataFrame that's 10 x 5 in size, filled with random numbers ranging from 0 to 100\. Name the five columns as follows: **["Week1", "Week2", "Week3", "Week4", "Week5"]**.
确保将随机种子设置为`0`,以便能够重现本书中显示的结果:
np.random.seed(0)
数据= pd.DataFrame(np.random.randint(0,100,size =(10,5)),
column = ['Week1','Week2','Week3',\
'Week4','Week5'])
数据
```py
np.random.seed(0)
data = pd.DataFrame(np.random.randint(0,100,size=(10, 5)),
                    columns=['Week1','Week2','Week3',\
                             'Week4','Week5'])
data
```
注意
......@@ -166,15 +164,17 @@
3. Create an input and a target variable, considering that the input variable should contain all the values of all the instances, except the last column of data. The target variable should contain all the values of all the instances, except the first column:
输入= data.iloc [:,:-1]
目标= inputs.shift(-1,axis =“ columns”,\
fill_value = data.iloc [:,-1:])
```py
inputs = data.iloc[:,:-1]
targets = inputs.shift(-1, axis="columns", \
                       fill_value=data.iloc[:,-1:])
```
4. Print the input variable to verify its contents, as follows:
输入
```py
inputs
```
输入变量应如下所示:
......@@ -184,7 +184,9 @@
5. Print the resulting target variable using the following code:
目标
```py
targets
```
运行前面的代码将显示以下输出:
......@@ -202,29 +204,20 @@
在 PyTorch 中,类似于任何其他层,循环层在一行代码中定义。 然后,将在网络的转发功能中调用此代码,如以下代码所示:
RNN 类(nn.Module):
def __init __(self,input_size,hidden_​​size,num_layers):
super().__ init __()
self.hidden_​​size = hidden_​​size
self.rnn = nn.RNN(input_size,hidden_​​size,num_layers,\
batch_first = True)
self.output = nn.Linear(hidden_​​size,1)
def forward(自我,x,隐藏):
out,隐藏= self.rnn(x,隐藏)
out = out.view(-1,self.hidden_​​size)
out = self.output(输出)
返回,隐藏
```py
class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers):
        super().__init__()
        self.hidden_size = hidden_size
        self.rnn = nn.RNN(input_size, hidden_size, num_layers,\
                          batch_first=True)
        self.output = nn.Linear(hidden_size, 1)
    def forward(self, x, hidden):
        out, hidden = self.rnn(x, hidden)
        out = out.view(-1, self.hidden_size)
        out = self.output(out)
        return out, hidden
```
在这里,必须将循环层定义为采用输入中预期特征数的参数(`input_size`); 由用户定义的处于隐藏状态的功能部件数(·hidden_​​size); 和循环层数(·num_layers)。
......@@ -238,21 +231,16 @@ out = self.output(输出)
此外,这种网络的培训可以按以下方式处理:
对于范围(1,纪元+1)中的 i:
隐藏=无
对于投入,成批目标:
pred,hidden =模型(输入,隐藏)
损失= loss_function(pred,目标)
Optimizer.zero_grad()
loss.backward()
Optimizer.step()
```py
for i in range(1, epochs+1):
    hidden = None
    for inputs, targets in batches:
        pred, hidden = model(inputs, hidden)
        loss = loss_function(pred, targets)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
```
对于每个时期,隐藏状态都初始化为`None`。 这是因为,在每个时期,网络都会尝试将输入映射到目标(给定一组参数时)。 该映射应该与数据集中之前的运行没有任何偏差(隐藏状态)地发生。
......@@ -388,19 +376,15 @@ Optimizer.step()
可以通过以下代码片段实现上述输出:
text =“这是测试文本!”
字符=列表(设置(文本))
indexer = {char:(index,char)的索引\
以枚举(字符)}
```py
text = "this is a test text!"
chars = list(set(text))
indexer = {char: index for (index, char) \
           in enumerate(chars)}
indexed_data = []
对于文本中的 c:
indexed_data.append(indexer [c])
for c in text:
    indexed_data.append(indexer[c])
```
代码的第二行创建一个包含文本字母(即文本序列中的字母和字符)的列表。 接下来,使用每个字母或字符作为键并使用与其关联的数字表示作为值来创建字典。 最后,通过对文本执行`for`循环,可以将每个字母或字符替换为其数字表示形式,从而将文本转换为数字矩阵。
......@@ -430,13 +414,12 @@ indexed_data.append(indexer [c])
批处理的生成可以通过以下代码片段实现:
x = np.array(indexed_data).reshape((2,-1))
对于范围为(0,x.shape [1],5)的 b:
批次= x [:,b:b + 5]
打印(批量)
```py
x = np.array(indexed_data).reshape((2,-1))
for b in range(0, x.shape[1], 5):
    batch = x[:,b:b+5]
    print(batch)
```
首先,将数字矩阵划分为多个序列(根据需要)。 接下来,通过`for`循环,可以将已排序的数据划分为指定长度的批处理。 通过打印`batch`变量,可以观察到结果。
......@@ -456,21 +439,16 @@ x = np.array(indexed_data).reshape((2,-1))
这可以通过以下代码片段实现:
批处理= np.array([[2 4 7 6 5]
[2 1 6 2 5]])
batch_flatten = batch.flatten()
onehot_flat = np.zeros(((batch.shape [0] \
* batch.shape [1],len(indexer)))
onehot_flat [range(len(batch_flatten)),batch_flatten] = 1
onehot = onehot_flat.reshape(((batch.shape [0],\
batch.shape [1],-1))
```py
batch = np.array([[2 4 7 6 5]
                  [2 1 6 2 5]])
batch_flatten = batch.flatten()
onehot_flat = np.zeros((batch.shape[0] \
                        * batch.shape[1],len(indexer)))
onehot_flat[range(len(batch_flatten)), batch_flatten] = 1
onehot = onehot_flat.reshape((batch.shape[0],\
                              batch.shape[1], -1))
```
首先,将二维批次展平。 接下来,创建一个矩阵,并用零填充。 当我们需要在给定位置表示正确的字符时,用零代替零。 最后,展平的尺寸再次扩大。
......@@ -480,71 +458,73 @@ batch.shape [1],-1))
1. Import NumPy:
将 numpy 导入为 np
```py
import numpy as np
```
2. Create a variable named **text**, which will contain the text sample **"Hello World!"**:
文字=“世界你好!”
```py
text = "Hello World!"
```
3. Create a dictionary by mapping each letter to a number:
字符=列表(设置(文本))
indexer = {char:(index,char)的索引\
以枚举(字符)}
打印(索引器)
```py
chars = list(set(text))
indexer = {char: index for (index, char) \
           in enumerate(chars)}
print(indexer)
```
运行前面的代码将得到以下输出:
{'d':0,'o':1,'H':2,'':3,'e':4,'W':5,'!':6,'l':7,'r ':8}
```py
{'d': 0, 'o': 1, 'H': 2, ' ': 3, 'e': 4, 'W': 5, '!': 6, 'l': 7, 'r': 8}
```
4. Encode your text sample with the numbers we defined in the previous step:
编码= []
对于文本中的 c:
encode.append(indexer [c])
```py
encoded = []
for c in text:
    encoded.append(indexer[c])
```
5. Convert the encoded variable into a NumPy array and reshape it so that the sentence is divided into two sequences of the same size:
编码= np.array(编码).reshape(2,-1)
编码的
```py
encoded = np.array(encoded).reshape(2,-1)
encoded
```
运行前面的代码将得到以下输出:
数组([[2,4,7,7,1,1,3,
[5, 1, 8, 7, 0, 6]])
```py
array([[2, 4, 7, 7, 1, 3],
       [5, 1, 8, 7, 0, 6]])
```
6. Define a function that takes an array of numbers and creates a one-hot matrix:
def index2onehot(批次):
batch_flatten = batch.flatten()
onehot_flat = np.zeros(((batch.shape [0] \
* batch.shape [1],len(indexer)))
onehot_flat [range(len(batch_flatten)),\
batch_flatten] = 1
onehot = onehot_flat.reshape((batch.shape [0],\
batch.shape [1],-1))
归还
```py
def index2onehot(batch):
    batch_flatten = batch.flatten()
    onehot_flat = np.zeros((batch.shape[0] \
                            * batch.shape[1], len(indexer)))
    onehot_flat[range(len(batch_flatten)), \
                batch_flatten] = 1
    onehot = onehot_flat.reshape((batch.shape[0], \
                                  batch.shape[1], -1))
    return onehot
```
7. Convert the encoded array into a one-hot matrix by passing it through the previously defined function:
one_hot = index2onehot(已编码)
```py
one_hot = index2onehot(encoded)
one_hot
```
输出应如下所示:
......@@ -564,45 +544,28 @@ batch.shape [1],-1))
与其他神经网络类似,可以在一行代码中轻松定义 LSTM 层。 但是,包含网络体系结构的类必须包含一个函数,该函数允许初始化隐藏状态和单元状态(即网络的两个内存)。 LSTM 网络体系结构的示例如下:
LSTM(nn.Module)类:
def __init __(self,char_length,hidden_​​size,n_layers):
super().__ init __()
self.hidden_​​size = hidden_​​size
self.n_layers = n_layers
self.lstm = nn.LSTM(char_length,hidden_​​size,\
n_layers,batch_first = True)
self.output = nn.Linear(hidden_​​size,char_length)
def forward(自我,x,状态):
出,状态= self.lstm(x,状态)
out = out.contiguous()。view(-1,self.hidden_​​size)
out = self.output(输出)
返回状态
def init_states(self,batch_size):
隐藏= next(self.parameters())。data.new(self.n_layers,\
batch_size,self.hidden_​​size).zero_()
单元格= next(self.parameters())。data.new(self.n_layers,\
batch_size,self.hidden_​​size).zero_()
状态=(隐藏的单元格)
返回状态
```py
class LSTM(nn.Module):
    def __init__(self, char_length, hidden_size, n_layers):
        super().__init__()
        self.hidden_size = hidden_size
        self.n_layers = n_layers
        self.lstm = nn.LSTM(char_length, hidden_size, \
                            n_layers, batch_first=True)
        self.output = nn.Linear(hidden_size, char_length)
    def forward(self, x, states):
        out, states = self.lstm(x, states)
        out = out.contiguous().view(-1, self.hidden_size)
        out = self.output(out)
        return out, states
    def init_states(self, batch_size):
        hidden = next(self.parameters()).data.new(self.n_layers, \
                      batch_size, self.hidden_size).zero_()
        cell = next(self.parameters()).data.new(self.n_layers, \
                    batch_size, self.hidden_size).zero_()
        states = (hidden, cell)
        return states
```
注意
......@@ -622,61 +585,37 @@ batch_size,self.hidden_​​size).zero_()
一旦定义了损失函数和优化算法,就该训练模型了。 这是通过采用与其他神经网络体系结构非常相似的方法来实现的,如以下代码片段所示:
#步骤 1:穿越时代
对于范围(1,历元+1)中的 e:
#步骤 2:内存已初始化
状态= model.init_states(n_seq)
#步骤 3:for 循环用于批量拆分数据。
对于范围为(0,x.shape [1],seq_length)的 b:
x_batch = x [:,b:b + seq_length]
如果 b == x.shape [1]-seq_length:
y_batch = x [:,b + 1:b + seq_length]
y_batch = np.hstack((y_batch,indexer [“。”] \
* np.ones((y_batch.shape [0],1))))
其他:
y_batch = x [:,b + 1:b + seq_length + 1]
"""
步骤 4:将输入数据转换为一热矩阵。
输入和目标将转换为张量。
"""
x_onehot =火炬张量(index2onehot(x_batch))
y =火炬。张量(y_batch).view(n_seq * seq_length)
"""
第 5 步:获取预测并执行
向后传播
"""
pred,状态=模型(x_onehot,状态)
损失= loss_function(pred,y.long())
Optimizer.zero_grad()
loss.backward(retain_graph = True)
Optimizer.step()
```py
# Step 1: for through epochs
for e in range(1, epochs+1):
    # Step 2: Memory initialized
    states = model.init_states(n_seq)
    # Step 3: for loop to split data in batches.
    for b in range(0, x.shape[1], seq_length):
        x_batch = x[:,b:b+seq_length]
        
        if b == x.shape[1] - seq_length:
            y_batch = x[:,b+1:b+seq_length]
            y_batch = np.hstack((y_batch, indexer["."] \
                      * np.ones((y_batch.shape[0],1))))
        else:
            y_batch = x[:,b+1:b+seq_length+1]
        """
        Step 4: input data is converted to one-hot matrix.
        Inputs and targets are converted to tensors.
        """
        x_onehot = torch.Tensor(index2onehot(x_batch))
        y = torch.Tensor(y_batch).view(n_seq * seq_length)
        """
        Step 5: get a prediction and perform the
        backward propagation
        """
        pred, states = model(x_onehot, states)
        loss = loss_function(pred, y.long())
        optimizer.zero_grad()
        loss.backward(retain_graph=True)
        optimizer.step()
```
如前面的代码所示,步骤如下:
......@@ -692,55 +631,35 @@ Optimizer.step()
这可以通过以下代码片段实现:
# 第 1 步
starter =“这是入门文本”
状态=无
# 第 2 步
对于启动器中的 ch:
x = np.array([[indexer [ch]]])
x = index2onehot(x)
x =火炬张量(x)
pred,状态=模型(x,状态)
#步骤 3
计数器= 0
而 starter [-1]!=“ 计数器<50:
计数器+ = 1
x = np.array([[indexer [starter [-1]]]])
x = index2onehot(x)
x =火炬张量(x)
pred,状态=模型(x,状态)
pred = F.softmax(pred,dim = 1)
p,上层=上一层(10)
p = p.detach()。numpy()[0]
top = top.numpy()[0]
索引= np.random.choice(top,p = p / p.sum())
# 步骤 4
入门级+ =字符[索引]
打印(入门)
```py
# Step 1
starter = "This is the starter text"
states = None
# Step 2
for ch in starter:
    x = np.array([[indexer[ch]]])
    x = index2onehot(x)
    x = torch.Tensor(x)
    
    pred, states = model(x, states)
# Step 3
counter = 0
while starter[-1] != "." and counter < 50:
    counter += 1
    x = np.array([[indexer[starter[-1]]]])
    x = index2onehot(x)
    x = torch.Tensor(x)
    
    pred, states = model(x, states)
    pred = F.softmax(pred, dim=1)
    p, top = pred.topk(10)
    p = p.detach().numpy()[0]
    top = top.numpy()[0]
    index = np.random.choice(top, p=p/p.sum())
    # Step 4
    starter += chars[index]
    print(starter)
```
在上一个代码段中,将执行以下步骤:
......@@ -843,15 +762,13 @@ NLP 是**人工智能**(**AI**)的子字段,它通过使计算机能够理
这可以通过使用`string`模块的`punctuation`预初始化的字符串来完成,该字符串提供了可用于在文本序列中标识它们的标点符号列表,如 以下代码段:
test = pd.Series(['嘿!这是示例#1。',\
'嘿! 这是示例 2。',\
'嘿! 这是示例 3。'])
对于我的标点符号:
测试= test.str.replace(i,“”)
```py
test = pd.Series(['Hey! This is example #1.', \
                  'Hey! This is example #2.', \
                  'Hey! This is example #3.'])
for i in punctuation:
    test = test.str.replace(i,"")
```
* **带编号的标签**:类似于前面介绍的映射字符的过程,词汇表中的每个单词都映射到一个整数,该整数将用于替换输入文本的单词,以便将它们输入网络 :
......@@ -867,33 +784,22 @@ PyTorch 无需执行一次性编码,而是将单词嵌入单行代码,这些
以下是体系结构的示例:
LSTM(nn.Module)类:
def __init __(self,vocab_size,embed_dim,\
hidden_​​size,n_layers):
super().__ init __()
self.hidden_​​size = hidden_​​size
self.embedding = nn.Embedding(vocab_size,embed_dim)
self.lstm = nn.LSTM(embed_dim,hidden_​​size,n_layers)
self.output = nn.Linear(hidden_​​size,1)
def forward(自我,x,状态):
出= self.embedding(x)
out,状态= self.lstm(out,状态)
out = out.contiguous()。view(-1,self.hidden_​​size)
out = self.output(输出)
返回状态
```py
class LSTM(nn.Module):
    def __init__(self, vocab_size, embed_dim, \
                 hidden_size, n_layers):
        super().__init__()
        self.hidden_size = hidden_size
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.lstm = nn.LSTM(embed_dim, hidden_size, n_layers)
        self.output = nn.Linear(hidden_size, 1)
    def forward(self, x, states):
        out = self.embedding(x)
        out, states = self.lstm(out, states)
        out = out.contiguous().view(-1, self.hidden_size)
        out = self.output(out)
        return out, states
```
如您所见,嵌入层将整个词汇表的长度以及用户设置的嵌入维度作为参数。 嵌入尺寸将是 LSTM 层的输入尺寸。 其余的体系结构将保持与以前相同。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册