From ebac23cf6194c4965603e9e6df4619292f7e3f15 Mon Sep 17 00:00:00 2001 From: wizardforcel <562826179@qq.com> Date: Sat, 16 Jan 2021 21:55:57 +0800 Subject: [PATCH] 2021-01-16 21:55:57 --- new/dl-pt-workshop/7.md | 80 ++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/new/dl-pt-workshop/7.md b/new/dl-pt-workshop/7.md index 39e22a28..c7d59bda 100644 --- a/new/dl-pt-workshop/7.md +++ b/new/dl-pt-workshop/7.md @@ -1665,7 +1665,7 @@ (0.229, 0.224, 0.225))]) -3. Define an image loader function. It should open the image and load it. Call the image loader function to load both input images: +3. 定义一个图像加载器功能。它应该打开图像并加载它。调用图像加载器函数来加载两个输入图像。 def image_loader(图片名称): @@ -1693,7 +1693,7 @@ style_img = image_loader(“ images / monet.jpg”)。to(device) -4. To be able to display the images, set the transformations to revert the normalization of the images and to convert the tensors into **PIL** images: +4. 为了能够显示图像,设置变换以恢复图像的归一化,并将张量转换为`PIL`图像。 卸载程序= transforms.Compose([\ @@ -1707,7 +1707,7 @@ transforms.ToPILImage()]) -5. Create a function (**tensor2image**) that's capable of performing the previous transformation over tensors. Call the function for both images and plot the results: +5. 创建一个函数(`tensor2image`),它能够在张量上执行前面的变换。对两幅图像调用该函数并绘制结果。 def tensor2image(张量): @@ -1763,7 +1763,7 @@ plt.show() -6. Load the VGG-19 model: +6. 加载VGG-19模型。 模型= models.vgg19(pretrained = True)。功能 @@ -1775,7 +1775,7 @@ model.to(设备) -7. Create a dictionary for mapping the index of the relevant layers (keys) to a name (values). Then, create a function to extract the feature maps of the relevant layers. Use them to extract the features of both input images. +7. 创建一个字典,用于将相关层的索引(键)映射到名称(值)。然后,创建一个函数来提取相关图层的特征图。用它们来提取两个输入图像的特征。 以下函数应为每个相关层提取给定图像的特征: @@ -1811,7 +1811,7 @@ related_layers) -8. Calculate the gram matrix for the style features. Also, create the initial target image. +8. 计算风格特征的 Gram 矩阵。同时,创建初始目标图像。 以下代码段为用于提取样式特征的每个层创建了 gram 矩阵: @@ -1839,7 +1839,7 @@ require_grad_(True).to(设备) -9. Set the weights for different style layers, as well as the weights for the content and style losses: +9. 设置不同样式层的权重,以及内容和样式损耗的权重。 style_weights = {'conv1_1':1.,'conv2_1':0.8,\ @@ -1851,7 +1851,7 @@ Beta = 1e5 -10. Run the model for 500 iterations. Define the Adam optimization algorithm before starting to train the model, using`0.001`as the learning rate: +10. 运行模型500次迭代。在开始训练模型之前,定义Adam优化算法,以`0.001`作为学习率。 注意 @@ -1931,7 +1931,7 @@ plt.show() -11. Plot the **content**, **style**, and **target** images to compare the results: +11. 绘制**内容**、**风格**、**目标**的图片,比较结果。 图,(ax1,ax2,ax3)= plt.subplots(1,3,图大小=(15,5)) @@ -1965,7 +1965,7 @@ ### 解决方案 -1. Import the required libraries, as follows: +1. 导入所需的库,具体如下: 将熊猫作为 pd 导入 @@ -1975,7 +1975,7 @@ 从火炬进口 nn,乐观 -2. Load the dataset and then slice it so that it contains all the rows but only the columns from index 1 to 52: +2. 加载数据集,然后对其进行切片,使其包含所有的行,但只包含索引1到52的列。 数据= pd.read_csv(“ Sales_Transactions_Dataset_Weekly.csv”) @@ -1989,7 +1989,7 @@ 图 6.26:显示索引 1 到 52 列的数据集 -3. Plot the weekly sales transactions of five randomly chosen products from the entire dataset. Use a random seed of`0`when performing random sampling in order to achieve the same results as in the current activity: +3. 绘制从整个数据集中随机选择的五种产品的每周销售交易情况。在进行随机抽样时,使用随机种子`0`,以达到与当前活动相同的结果。 plot_data = data.sample(5,random_state = 0) @@ -2015,7 +2015,7 @@ 图 6.27:输出图 -4. Create the **inputs** and **targets** variables that will be fed to the network to create the model. These variables should be of the same shape and be converted into PyTorch tensors. +4. 创建`input`和`target`变量,这些变量将被输入到网络中以创建模型。这些变量应具有相同的形状,并转换为 PyTorch 张量。 `input`变量应包含除上周外的所有星期的所有产品数据,因为模型的目的是预测最后一周。 @@ -2033,7 +2033,7 @@ 目标=火炬。张量(targets.values) -5. Create a class containing the architecture of the network. Note that the output size of the fully connected layer should be`1`: +5. 创建一个包含网络架构的类。注意,全连接层的输出大小应该是`1`。 RNN 类(nn.Module): @@ -2061,7 +2061,7 @@ 与之前的活动一样,该类包含`__init__`方法以及网络架构,以及`forward`方法,该方法确定信息在各层之间的流动。 -6. Instantiate the **class** function containing the model. Feed the input size, the number of neurons in each recurrent layer (`10`), and the number of recurrent layers (`1`): +6. 实例化包含模型的**类**函数。输入输入大小、每个循环层的神经元数量(`10`)和循环层数量(`1`)。 模型= RNN(data_train.shape [1],10,1) @@ -2077,7 +2077,7 @@ ) -7. Define a loss function, an optimization algorithm, and the number of epochs to train the network. Use the MSE loss function, the Adam optimizer, and 10,000 epochs to do this: +7. 定义一个损失函数,一个优化算法,以及训练网络的周期数。使用MSE损失函数、Adam优化器和10,000个纪元来完成这一任务。 loss_function = nn.MSELoss() @@ -2085,7 +2085,7 @@ 纪元= 10000 -8. Use a **for** loop to perform the training process by going through all the epochs. In each epoch, a prediction must be made, along with the subsequent calculation of the loss function and the optimization of the parameters of the network. Save the loss of each of the epochs: +8. 使用`for`循环来执行训练过程,经历所有的周期。在每个纪元中,必须进行预测,以及随后的损失函数计算和网络参数的优化。保存每个纪元的损失。 注意 @@ -2137,7 +2137,7 @@ 时代:10000 ...损失函数:7.311776161193848 -9. Plot the losses of all epochs, as follows: +9. 将所有时代的损失绘制如下: x_range =范围(len(损失)) @@ -2155,7 +2155,7 @@ 图 6.28:显示所有时期的损失的图 -10. Using a scatter plot, display the predictions that were obtained in the last epoch of the training process against the ground truth values (that is, the sales transactions of the last week): +10. 使用散点图,显示在训练过程的最后一个纪元中获得的预测值与地面真实值(即上周的销售交易)的对比。 x_range =范围(len(数据)) @@ -2193,7 +2193,7 @@ ### 解决方案 -1. Import the required libraries, as follows: +1. 导入所需的库,具体如下: 导入数学 @@ -2207,7 +2207,7 @@ 导入功能为 F 的 torch.nn。 -2. Open and read the text from *Alice in Wonderland* into the notebook. Print an extract of the first 50 characters and the total length of the text file: +2. 打开并将《爱丽丝梦游仙境》中的文字读入笔记本。打印前50个字符的摘要和文本文件的总长度。 使用 open('alice.txt' ,'r',encoding ='latin1')作为 f: @@ -2217,7 +2217,7 @@ print(“ Length:”,len(data)) -3. Create a variable containing a list of the unduplicated characters in your dataset. Then, create a dictionary that maps each character to an integer, where the characters will be the keys and the integers will be the values: +3. 创建一个变量,其中包含数据集中未重复的字符列表。然后,创建一个字典,将每个字符映射到一个整数,其中字符将是键,整数将是值。 字符=列表(设置(数据)) @@ -2229,7 +2229,7 @@ 长度:145178 -4. Encode each letter of your dataset to its paired integer. Print the first 50 encoded characters and the total length of the encoded version of your dataset: +4. 将数据集的每个字母编码为其配对的整数。打印前50个编码字符和数据集编码版本的总长度。 indexed_data = [] @@ -2247,7 +2247,7 @@ 长度:145178 -5. Create a function that takes in a batch and encodes it as a one-hot matrix: +5. 创建一个函数,接收一个批量,并将其编码为单热矩阵。 def index2onehot(批量): @@ -2267,7 +2267,7 @@ 此函数采用二维矩阵并将其展平。 接下来,它创建一个平坦矩阵的形状和包含字母的字典长度的零填充矩阵(在“步骤 3”中创建)。 接下来,它用一个字符填充对应于批量中每个字符的字母。 最后,它对矩阵进行整形以使其为三维。 -6. Create a class that defines the architecture of the network. This class should contain an additional function that initializes the states of the LSTM layers: +6. 创建一个定义网络架构的类。这个类应该包含一个额外的函数,用于初始化LSTM层的状态。 LSTM(nn.Module)类: @@ -2315,7 +2315,7 @@ 此类包含`__init__`方法(其中定义了网络的架构),`forward`方法(用于确定通过层的数据流)以及`init_state`用零初始化隐藏状态和单元状态的方法。 -7. Determine the number of batches to be created out of your dataset, bearing in mind that each batch should contain 100 sequences, each with a length of 50\. Next, split the encoded data into 100 sequences: +7. 确定要从数据集中创建的批次数量,记住每个批次应该包含100个序列,每个序列的长度为50个。接下来,将编码后的数据分成100个序列。 #每批序列​​数 @@ -2333,7 +2333,7 @@ x = np.array(x).reshape(([ n_seq,-1)) -8. Instantiate your model by using`256`as the number of hidden units for a total of two recurrent layers: +8. 通过使用`256`作为共两个递归层的隐藏单元数来实例化你的模型。 模型= LSTM(仅(字符),256、2) @@ -2353,7 +2353,7 @@ 模型= LSTM(len(chars),256,2).to(“ cuda”) -9. Define the loss function and the optimization algorithms. Use the Adam optimizer and the cross-entropy loss to do this. Train the network for`20`epochs: +9. 定义损失函数和优化算法。使用Adam优化器和交叉熵损失来完成。训练网络`20`周期。 loss_function = nn.CrossEntropyLoss() @@ -2365,7 +2365,7 @@ 时代= 500 -10. In each epoch, the data must be divided into batches with a sequence length of 50\. This means that each epoch will have 100 batches, each with a sequence of 50: +10. 在每个时代,数据必须被划分为序列长度为50的批次。这意味着每个时代将有100个批次,每个批次的序列长度为50。 损失= [] @@ -2513,7 +2513,7 @@ 可以看出,通过将训练过程运行更多的时间段,损失函数将达到较低的值。 -11. Plot the progress of the loss over time: +11. 绘制损失随时间推移的进展情况。 x_range =范围(len(损失)) @@ -2533,7 +2533,7 @@ 如我们所见,在 20 个历元之后,损失函数仍然可以减少,这就是为什么强烈建议训练更多历元以便从模型中获得良好结果的原因。 -12. Feed the following sentence **starter** into the trained model for it to complete the sentence: **"So she was considering in her own mind "**: +12. 将下面的句子`starter`输入到训练好的模型中,让它来完成这个句子:`"So she was considering in her own mind "`。 starter =“所以她在 中考虑自己的想法” @@ -2613,7 +2613,7 @@ 导入功能为 F 的 torch.nn。 -2. Load the dataset containing a set of 1,000 product reviews from Amazon, which is paired with a label of`0`(for negative reviews) or`1`(for positive reviews). Separate the data into two variables – one containing the reviews and the other containing the labels: +2. 加载包含亚马逊1,000条产品评论的数据集,这些评论与`0`(负面评论)或`1`(正面评论)的标签配对。将数据分离成两个变量--一个包含评论,另一个包含标签。 数据= pd.read_csv(“ amazon_cells_labelled.txt”,sep =“ \ t”,\ @@ -2623,13 +2623,13 @@ 情绪= data.iloc [:,1] .values -3. Remove the punctuation from the reviews: +3. 去掉评论中的标点符号。 对于我的标点符号: 评论= reviews.str.replace(i,“”) -4. Create a variable containing the vocabulary of the entire set of reviews. Additionally, create a dictionary that maps each word to an integer, where the words will be the keys and the integers will be the values: +4. 创建一个变量,包含整个评论集的词汇量。此外,创建一个字典,将每个单词映射到一个整数,其中单词将是键,整数将是值。 words =''.join(评论) @@ -2641,7 +2641,7 @@ 枚举(词汇)} -5. Encode the reviews data by replacing each word in a review with its paired integer: +5. 通过将评论中的每个词替换为其配对的整数来对评论数据进行编码。 indexed_reviews = [] @@ -2651,7 +2651,7 @@ for review.split()中的单词) -6. Create a class containing the architecture of the network. Make sure that you include an embedding layer: +6. 创建一个包含网络架构的类。确保你包含一个嵌入层。 LSTM(nn.Module)类: @@ -2689,7 +2689,7 @@ 该类包含用于定义网络架构的`__init__`方法和用于确定数据流经不同层的方式的`forward`方法。 -7. Instantiate the model using 64 embedding dimensions and 128 neurons for three LSTM layers: +7. 使用64个嵌入维度和128个神经元为三个LSTM层实例化模型。 模型= LSTM(仅(词汇),64、128、3) @@ -2707,7 +2707,7 @@ ) -8. Define the loss function, an optimization algorithm, and the number of epochs to train for. For example, you can use the binary cross-entropy loss as the loss function, the Adam optimizer, and train for 10 epochs: +8. 定义损失函数,优化算法,以及训练的纪元数。例如,您可以使用二进制交叉熵损失作为损失函数,Adam优化器,并训练10个周期。 loss_function = nn.BCELoss() @@ -2715,7 +2715,7 @@ 时代= 10 -9. Create a **for** loop that goes through the different epochs and through every single review individually. For each review, perform a prediction, calculate the loss function, and update the parameters of the network. Additionally, calculate the accuracy of the network on that training data: +9. 创建一个`for`循环,通过不同的纪元,并分别通过每一个单次评论。对于每一个评论,进行预测,计算损失函数,并更新网络的参数。此外,计算该训练数据上网络的准确性。 损失= [] @@ -2771,7 +2771,7 @@ 与以前的活动一样,训练过程包括进行预测,将其与基本事实进行比较以计算损失函数,并执行向后传递以最小化损失函数。 -10. Plot the progress of the loss and accuracy over time. The following code is used to plot the loss function: +10. 绘制损失和精度随时间的进展情况。以下代码用于绘制损失函数。 x_range =范围(len(损失)) -- GitLab