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:
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.
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:
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.
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`):
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:
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:
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):
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:
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:
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:
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:
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:
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:
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:
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:
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: