<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ai on Shaka</title><link>https://shakaxi.github.io/tags/ai/</link><description>Recent content in Ai on Shaka</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sat, 27 Sep 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://shakaxi.github.io/tags/ai/index.xml" rel="self" type="application/rss+xml"/><item><title>K - Means Clustering</title><link>https://shakaxi.github.io/posts/k_means/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/k_means/</guid><description>&lt;p&gt;&#10;K - Means is a classical clustering algorithm, targeting intra-cluster close resemblance and inter-cluster difference.&lt;/p&gt;&#10;&lt;p&gt;&#10;Without loss of generality, the procedure of assigning $n$ samples to $k$ clusters can be listed as follows.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Initialize the cluster centers: Randomly select $k$ different samples as cluster centers.&lt;/li&gt;&#10;&lt;li&gt;Assign samples: For each sample, compute its distance to each cluster center, and assign the sample to its closest cluster.&lt;/li&gt;&#10;&lt;li&gt;Update cluster centers: For each cluster, update the mean value of intra-cluster samples as the new cluster center.&lt;/li&gt;&#10;&lt;li&gt;Repeat steps 2 and 3 until cluster centers do not change anymore or a maximum/expected number of iterations are reached.&lt;/li&gt;&#10;&lt;/ol&gt;</description></item><item><title>Pytorch Lightning</title><link>https://shakaxi.github.io/posts/pytorch_lightning/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/pytorch_lightning/</guid><description>&lt;p&gt;&#10;&lt;a href="https://github.com/Lightning-AI/lightning"&gt;&lt;em&gt;Pytorch Lightning&lt;/em&gt;&lt;/a&gt; is a high level framework and encapsulation on top of pytorch, which provides simplified interfaces and elegant code structure relating to AI. Detailed usage can be found in its &lt;a href="https://pytorch-lightning.readthedocs.io/en/stable"&gt;manual&lt;/a&gt;.&lt;/p&gt;&#10;&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;Installation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;p&gt;The installation of Pytorch Lightning is very simple. Taking &lt;em&gt;pip&lt;/em&gt; for instance, following command completes the procedure.&lt;/p&gt;&#10;&lt;div class="src src-shell"&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-shell" data-lang="shell"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; pip install pytorch-lightning&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-2"&gt;&#10;&lt;h2 id="headline-2"&gt;&#10;Usage&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-3"&gt;&#10;&lt;h3 id="headline-3"&gt;&#10;Neural network&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;&#10;&lt;p&gt;The structure of neural network can be defined as a subclass of &lt;code class="verbatim"&gt;pytorch_lightning.LightningModule&lt;/code&gt;, which is essentially a subclass of &lt;code class="verbatim"&gt;torch.nn.Module&lt;/code&gt;. Besides the constructor &lt;code class="verbatim"&gt;__init__&lt;/code&gt; and function &lt;code class="verbatim"&gt;forward&lt;/code&gt;, a series of hook functions has been defined and can be simply overridden, e.g.,&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;configure_optimizers&lt;/code&gt; can be used to further tweak the behavior of the optimizer, e.g., the initial value of learning rate and how it is decaying.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;on_train_start&lt;/code&gt;, &lt;code class="verbatim"&gt;on_validation_start&lt;/code&gt;, and &lt;code class="verbatim"&gt;on_test_start&lt;/code&gt; are the hook functions provoked at the beginning of training, validating, and testing, respectively.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;on_train_end&lt;/code&gt;, &lt;code class="verbatim"&gt;on_validation_end&lt;/code&gt;, and &lt;code class="verbatim"&gt;on_test_end&lt;/code&gt; are the hook functions provoked at the end of training, validating, and testing, respectively.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;training_step&lt;/code&gt;, &lt;code class="verbatim"&gt;validation_step&lt;/code&gt;, and &lt;code class="verbatim"&gt;test_step&lt;/code&gt; are the hook functions provoked for each batch in training, validating, and testing respectively, e.g., in which loss function is calculated.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;training_epoch_end&lt;/code&gt;, &lt;code class="verbatim"&gt;validation_epoch_end&lt;/code&gt;, and &lt;code class="verbatim"&gt;test_epoch_end&lt;/code&gt; are the hook functions provoked at the end of each epoch in training, validating, and testing respectively.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-3"&gt;&#10;&lt;h3 id="headline-4"&gt;&#10;Data module&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;&#10;&lt;p&gt;In &lt;code class="verbatim"&gt;python_lightning&lt;/code&gt;, a &lt;em&gt;data module&lt;/em&gt; class is defined in &lt;code class="verbatim"&gt;python_lightning.LightningDataModule&lt;/code&gt;, which provides a unified interface for data loading for training, validating, and testing. We can simply define our own data module as a subclass of &lt;code class="verbatim"&gt;python_lightning.LightningDataModule&lt;/code&gt;, and override the following functions.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;setup&lt;/code&gt; can define one or more datasets needed.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;train_dataloader&lt;/code&gt;, &lt;code class="verbatim"&gt;val_dataloader&lt;/code&gt;, and &lt;code class="verbatim"&gt;test_dataloader&lt;/code&gt; return separate dataloaders for training, validating and testing, respectively.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-5" class="outline-3"&gt;&#10;&lt;h3 id="headline-5"&gt;&#10;Procedure&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;&#10;&lt;p&gt;The main procedure is completed based on an instance of class &lt;code class="verbatim"&gt;python_lightning.Trainer&lt;/code&gt;. In its constructor, we can customize it via a series of keyword arguments, e.g.,&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;default_root_dir&lt;/code&gt; indicates the output directory.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;max_epochs&lt;/code&gt; is the maximum number of epochs.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;check_val_every_n_epoch&lt;/code&gt; is the period of validation in epoch number.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;logger&lt;/code&gt; is object for logging, e.g., &lt;code class="verbatim"&gt;python_lightning.loggers.TensorBoardLogger&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;enable_progress_bar&lt;/code&gt; is a boolean indicator of progress bar.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;strategy&lt;/code&gt; defines the training type, e.g., &lt;code class="verbatim"&gt;DDP&lt;/code&gt;, &lt;code class="verbatim"&gt;DDP2&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;gpus&lt;/code&gt; indicates the identities of selected GPU(s).&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The training and testing can be finished by calling its member functions, e.g.,&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;fit&lt;/code&gt; for training.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;test&lt;/code&gt; for testing.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;save_checkpoint&lt;/code&gt; to save the state of the &lt;code class="verbatim"&gt;python_lightning.Trainer&lt;/code&gt; object, including the coefficients or weights of the trainable parameters in the neural network.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item><item><title>MLP-Mixer</title><link>https://shakaxi.github.io/posts/mlp_mixer/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/mlp_mixer/</guid><description>&lt;p&gt;&#10;&lt;em&gt;Multi-layer perceptron mixer (MLP-Mixer)&lt;/em&gt; is a novel neural network model, which attracts the attentions of both academic circle and industrial area. Besides the proposal paper, I found another blog post providing an excellent illustration.&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;img src="./mlp_mixer.png" alt="./mlp_mixer.png" title="./mlp_mixer.png" /&gt;&lt;figcaption&gt;&#10;A MLP-Mixer layer&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;&#10;The structure of MLP-Mixer can be shown in the figure above.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The input and output tensors have the same size, denoted by $B\times C\times P$, where $B$ is the batch size, $C$ is the number of channels, $P$ is the size of each patch. Usually, the patch size is a product of patch size in each dimension. E.g., a 2-D image of size $H\times W$ can be partitioned into a multiplicity of patches, with each of size $P = P_h \times P_w$. Then different patches correspond to different channels, i.e., $C = \dfrac{H}{P_{h}} \cdot \dfrac{W}{P_{w}}$. Without loss of generality, we stretch each patch into 1-D irrespecitve of its dimensions.&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Just as its name indicates, each MLP-Mixer layer has two MLP modules, targeting for intra-patch feature extraction, a.k.a. &lt;em&gt;token mixing&lt;/em&gt;, and inter-patch feature processing, a.k.a. &lt;em&gt;channel mixing&lt;/em&gt;, respectively. Of course, token mixing and channel mixing operates along the dimension of patch (of size $P$) and channel (of size $C$), respectively.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Generally, a MLP module is an artificial neural network comprising an input layer, a plurality of hidden layers, and an output layer. A typical MLP module can be shown in the figure below, which is comprised of 2 full-connection (FC) layers seperated by a non-linear activate function, e.g., &lt;em&gt;Gaussian error linear unit, (GeLU)&lt;/em&gt;, which has been widely used in &lt;em&gt;natural language processing (NLP)&lt;/em&gt;.&lt;/p&gt;&#10;\begin{align*}&#10; \mathrm{GeLU}(x) = x \cdot p(X &amp;lt; x),&#10;\end{align*}&#10;&lt;p&gt;where $p(X &amp;lt; x)$ is the probability of a Gaussian distributed stochastic variable $X\sim \mathcal{N}(0, 1)$ less than $x$.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>Tensorflow</title><link>https://shakaxi.github.io/posts/tensorflow/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/tensorflow/</guid><description>&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;Introducation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;p&gt;Tensorflow is a deep learning library, which provides two programming interfaces, C++ and Python. Moreover, The Python API integrates very well with &lt;a href="http://www.numpy.org"&gt;numpy&lt;/a&gt;. In many places, Tensorflow tensors can be interchanged with numpy &lt;code class="verbatim"&gt;ndarrays&lt;/code&gt;.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-2"&gt;&#10;&lt;h2 id="headline-2"&gt;&#10;Representation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;&#10;&lt;p&gt;In tensorflow, machine learning algorithms are represented as &lt;em&gt;computational graphs&lt;/em&gt;, in which a vertex/node and an edge represent an operation (&lt;code class="verbatim"&gt;op&lt;/code&gt;) and a data flowing, respectively.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-2"&gt;&#10;&lt;h2 id="headline-3"&gt;&#10;Operation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-2"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;An operation can have zero or more inputs and produce zero or more outputs.&lt;/li&gt;&#10;&lt;li&gt;An operation may stand for a mathematical equation, a variable, a constant, a control flow directive, a file I/O operation, or even a network communication port.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-2"&gt;&#10;&lt;h2 id="headline-4"&gt;&#10;Tensor&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-2"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;A tensor is a multidimentional array or list, the generalization of scalars, 1-D vector, and 2-D matrices.&lt;/li&gt;&#10;&lt;li&gt;Tensors are the connecting edges, passed between nodes/ops in the flow diagram.&lt;/li&gt;&#10;&lt;li&gt;A tensor is a symbolic handle, which does not hold or store values in memory, but provides only an interface for retrieving the value referenced by the tensor.&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Parameters&lt;/p&gt;&#10;&lt;dl&gt;&#10;&lt;dt&gt;&#10;Rank&#10;&lt;/dt&gt;&#10;&lt;dd&gt;The number of dimensions, i.e., the depth of brackets.&lt;/dd&gt;&#10;&lt;dt&gt;&#10;Shape&#10;&lt;/dt&gt;&#10;&lt;dd&gt;The number of components in each dimension, e.g., the number of rows and columns. Tensorflow defines following shape-related functions, &lt;code class="verbatim"&gt;tensorflow.shape&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.size&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.rank&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.reshape&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.expand_dims&lt;/code&gt;.&lt;/dd&gt;&#10;&lt;dt&gt;&#10;Type&#10;&lt;/dt&gt;&#10;&lt;dd&gt;Data type assigned to the elements of the tensor, e.g., &lt;code class="verbatim"&gt;DT_FLOAT&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_DOUBLE&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_INT8&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_UINT8&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_INT16&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_INT32&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_INT64&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_STRING&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_BOOL&lt;/code&gt;, &lt;code class="verbatim"&gt;DT_COMPLEX64&lt;/code&gt;. Additionally, tensorflow provides following type transformation functions, e.g., &lt;code class="verbatim"&gt;tensorflow.string_to_number&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.to_double&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.to_float&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.to_int32&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.to_int64&lt;/code&gt;, &lt;code class="verbatim"&gt;tensorflow.cast&lt;/code&gt;.&lt;/dd&gt;&#10;&lt;/dl&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item><item><title>Neural Network</title><link>https://shakaxi.github.io/posts/nn/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/nn/</guid><description>&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;Introducation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The network is composed of an input layer, an output layer, and optionally a series of hidden layers.&lt;/li&gt;&#10;&lt;li&gt;Input layer does not carry out any operation/function, but the hidden and output layers consist of functional neurons, i.e., have activation functions.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-2"&gt;&#10;&lt;h2 id="headline-2"&gt;&#10;McCulloch and Pitts (M-P) model for neurons&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;&#10;\begin{align*}&#10; y = f(\mathbf{W} \mathbf{x} - \theta)&#10;\end{align*}&#10;&lt;p&gt;where \(f\) is termed &lt;em&gt;activation function&lt;/em&gt;.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-2"&gt;&#10;&lt;h2 id="headline-3"&gt;&#10;Activation function&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-2"&gt;&#10;&lt;p&gt;Activation function aims to introduce the non-linear capability, compensate the shortage of linear models.&lt;/p&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-3"&gt;&#10;&lt;h3 id="headline-4"&gt;&#10;Sign&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;&#10;\begin{align*}&#10; \text{sign}(x) = \begin{cases}&#10; 1, &amp;amp; x \ge 0; \\&#10; 0, &amp;amp; x &amp;lt; 0.&#10; \end{cases}&#10;\end{align*}&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-5" class="outline-3"&gt;&#10;&lt;h3 id="headline-5"&gt;&#10;Sigmoid&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;&#10;\begin{align*}&#10; \text{sigmoid}(x) = \frac{1}{1 + e^{-x}}.&#10;\end{align*}&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-6" class="outline-3"&gt;&#10;&lt;h3 id="headline-6"&gt;&#10;Tanh&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-6" class="outline-text-3"&gt;&#10;\begin{align*}&#10; \text{tanh}(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}.&#10;\end{align*}&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-7" class="outline-3"&gt;&#10;&lt;h3 id="headline-7"&gt;&#10;ReLU&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-7" class="outline-text-3"&gt;&#10;\begin{align*}&#10; \text{ReLU}(x) = \max(x, 0)&#10;\end{align*}&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-8" class="outline-3"&gt;&#10;&lt;h3 id="headline-8"&gt;&#10;Softmax&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-8" class="outline-text-3"&gt;&#10;&lt;p&gt;Softmax function can transform scores/logits into a valid probability distribution.&lt;/p&gt;&#10;\begin{align*}&#10; \text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}&#10;\end{align*}&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item><item><title>Pytorch</title><link>https://shakaxi.github.io/posts/pytorch/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/pytorch/</guid><description>&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;张量&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;p&gt;在pytorch中，神经网络的输入、输出、以及网络的参数等都采用了“张量”的数据结构。张量与numpy中的多维数组(&lt;code class="verbatim"&gt;numpy.ndarray&lt;/code&gt;)非常类似，区别在于张量可以在GPU或其他硬件上运行。&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;成员函数名称末尾加上 &lt;code class="verbatim"&gt;_&lt;/code&gt; ，会采用替换模式，也即该成员函数直接修改张量对象本身。&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;.item()&lt;/code&gt; 返回其具体数值，该方法仅适用于大小为1（也即只含有一个元素）的张量。&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;.view()&lt;/code&gt; 功能为reshape该张量（保持元素个数不变），其参数为新的shape。特别地，值为-1的维度可以通过元素总数不变推导得出。&lt;/li&gt;&#10;&lt;li&gt;定义一个张量时，可以通过参数 &lt;code class="verbatim"&gt;device&lt;/code&gt; 参数指定该张量存储于CPU还是GPU。也可以通过其成员函数 &lt;code class="verbatim"&gt;.to()&lt;/code&gt; 来转换。&lt;/li&gt;&#10;&lt;li&gt;定义一个张量时，参数 &lt;code class="verbatim"&gt;requires_grad=True&lt;/code&gt; 表示需要计算并存储对于该张量的梯度。该张量的函数均带有 &lt;code class="verbatim"&gt;grad_fn&lt;/code&gt; 属性。对于其任一函数，当调用其成员函数 &lt;code class="verbatim"&gt;.backward()&lt;/code&gt; 时，该函数对于该张量自变量的梯度将会被计算并保存在该张量的 &lt;code class="verbatim"&gt;.grad&lt;/code&gt; 属性中。&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;有时候需要停止跟踪记录某个张量（记为 &lt;code class="verbatim"&gt;x&lt;/code&gt; ）的梯度（比如在更新网络参数的权重系数时），可以采用以下任一方式：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;调用该张量的成员函数 &lt;code class="verbatim"&gt;x.requires_grad_(False)&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;li&gt;调用该张量的成员函数 &lt;code class="verbatim"&gt;x.detach()&lt;/code&gt; 会产生一个新的张量，其属性 &lt;code class="verbatim"&gt;requires_grad=False&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;li&gt;采用with语句，也即 &lt;code class="verbatim"&gt;with torch.no_grad():&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-3"&gt;&#10;&lt;h3 id="headline-2"&gt;&#10;与 numpy.ndarray 相互转换&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;张量的成员函数 &lt;code class="verbatim"&gt;.numpy()&lt;/code&gt; 返回对应的 &lt;code class="verbatim"&gt;numpy.ndarray&lt;/code&gt; 。但是，该方法仅适用于存储在CPU的张量。&lt;/li&gt;&#10;&lt;li&gt;函数 &lt;code class="verbatim"&gt;torch.from_numpy()&lt;/code&gt; 可以将一个 &lt;code class="verbatim"&gt;numpy.ndarray&lt;/code&gt; 转换为张量。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;值得注意的是，当一个张量存储在CPU上时，该张量与其对应的 &lt;code class="verbatim"&gt;numpy.ndarray&lt;/code&gt; 是连接着的，也即对其中一个的修改会导致另一个也会被修改。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-2"&gt;&#10;&lt;h2 id="headline-3"&gt;&#10;神经网络&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-2"&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-3"&gt;&#10;&lt;h3 id="headline-4"&gt;&#10;典型训练过程&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;构建含有一些可学习参数(或权重)的神经网络。&lt;/li&gt;&#10;&lt;li&gt;遍历训练数据集。&lt;/li&gt;&#10;&lt;li&gt;让输入通过网络得到输出。&lt;/li&gt;&#10;&lt;li&gt;计算预测误差(或损失函数)。&lt;/li&gt;&#10;&lt;li&gt;通过反向传播，计算并存储网络各个参数的梯度。&lt;/li&gt;&#10;&lt;li&gt;根据各个参数的梯度对其进行调整，如 &lt;code class="verbatim"&gt;weight = weight - learning_rate x gradient&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-5" class="outline-3"&gt;&#10;&lt;h3 id="headline-5"&gt;&#10;正向传播与反向传播&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;&#10;&lt;p&gt;神经网络的训练包括两个步骤&lt;/p&gt;&#10;&lt;dl&gt;&#10;&lt;dt&gt;&#10;正向传播&#10;&lt;/dt&gt;&#10;&lt;dd&gt;神经网络根据输入对输出进行最佳预测。&lt;/dd&gt;&#10;&lt;dt&gt;&#10;反向传播&#10;&lt;/dt&gt;&#10;&lt;dd&gt;神经网络根据预测误差调整其参数。&lt;/dd&gt;&#10;&lt;/dl&gt;&#10;&lt;p&gt;当调用误差张量的成员函数 &lt;code class="verbatim"&gt;.backward()&lt;/code&gt; 时，开始反向传播，整个网络将被微分。Pytorch的自动差分引擎 &lt;code class="verbatim"&gt;torch.autograd&lt;/code&gt; 会对计算每个属性 &lt;code class="verbatim"&gt;requires_grad=True&lt;/code&gt; 的网络参数的梯度并将其累积在该网络参数的 &lt;code class="verbatim"&gt;.grad&lt;/code&gt; 属性中。所以，在反向传播开始之前，需要清空神经网络各个参数和反向传播的梯度缓冲区。&lt;/p&gt;&#10;&lt;p&gt;&#10;然后，需要从 &lt;code class="verbatim"&gt;torch.optim&lt;/code&gt; 中加载一个优化器(如随机梯度下降，SGD)。通过调用该优化器的 &lt;code class="verbatim"&gt;.step()&lt;/code&gt; 方法启动梯度下降，优化器会根据每个网络参数的梯度(存储于其 &lt;code class="verbatim"&gt;.grad&lt;/code&gt; 属性中)来对其进行调整。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-6" class="outline-3"&gt;&#10;&lt;h3 id="headline-6"&gt;&#10;定义网络&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-6" class="outline-text-3"&gt;&#10;&lt;p&gt;可以使用 &lt;code class="verbatim"&gt;torch.nn&lt;/code&gt; 包来构建神经网络。&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;基类 &lt;code class="verbatim"&gt;torch.nn.Module&lt;/code&gt; 包含&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;网络的各个层(作为类属性)，如输入层、输出层、隐藏层等。&lt;/li&gt;&#10;&lt;li&gt;成员函数 &lt;code class="verbatim"&gt;.forward&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;li&gt;成员函数 &lt;code class="verbatim"&gt;.zero_grad&lt;/code&gt; ，用于清空神经网络中所有参数的梯度缓冲区。相当于整个网络中属性 &lt;code class="verbatim"&gt;requires_grad=True&lt;/code&gt; 的张量均调用成员函数 &lt;code class="verbatim"&gt;.grad.zero_()&lt;/code&gt; 。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;构建神经网络可以通过定义以 &lt;code class="verbatim"&gt;torch.nn.Module&lt;/code&gt; 为基类的派生类完成。&lt;/li&gt;&#10;&lt;li&gt;只需要在派生类中重载成员函数 &lt;code class="verbatim"&gt;.forward&lt;/code&gt; ， &lt;code class="verbatim"&gt;torch.autograd&lt;/code&gt; 就会自动为网络各个参数定义 &lt;code class="verbatim"&gt;.backward()&lt;/code&gt; 函数。&lt;/li&gt;&#10;&lt;li&gt;所构建的神经网络的可学习参数由所述派生类的成员函数 &lt;code class="verbatim"&gt;.parameters()&lt;/code&gt; 返回。&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;torch.nn&lt;/code&gt; 包中定义了很多损失函数(如 &lt;code class="verbatim"&gt;torch.nn.MSELoss&lt;/code&gt;)。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;下面是一个非常简单的神经网络。该神经网络只包含一个全连接层和一个激活函数relu。包括两个成员函数， &lt;code class="verbatim"&gt;TRAIN&lt;/code&gt; 和 &lt;code class="verbatim"&gt;TEST&lt;/code&gt; 。顾名思义，前者用于从训练数据集中加载数据，并训练神经网络中的可学习参数，并把所有可学习参数的最终权重（训练结果）保存在文件中；后者从权重文件中读取并加载所有可学习参数的权重，然后从测试数据集中加载数据，计算误差，从而完成对训练结果（可学习参数的权重）的测试。&lt;/p&gt;&#10;&lt;div class="src src-python"&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;class&lt;/span&gt; &lt;span style="color:#1f2328"&gt;NEURAL_NETWORK&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;nn&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;Module&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;def&lt;/span&gt; &lt;span style="color:#6639ba"&gt;__init__&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; IN_FEATURES&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; OUT_FEATURES&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#6639ba"&gt;super&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;NEURAL_NETWORK&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;&lt;span style="color:#6639ba"&gt;__init__&lt;/span&gt;&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;FC &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;nn&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;Module&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;Linear&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;IN_FEATURES&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; OUT_FEATURES&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;def&lt;/span&gt; &lt;span style="color:#6639ba"&gt;forward&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; INPUT&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;nn&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;function&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;relu&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;FC&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;INPUT&lt;span style="color:#1f2328"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;def&lt;/span&gt; &lt;span style="color:#6639ba"&gt;TRAIN&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; DATALOADER_FOR_TRAIN&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; OPT &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;optim&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;Adam&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;parameters&lt;span style="color:#1f2328"&gt;(),&lt;/span&gt; LEARNING_RATE&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LEARNING_RATE_SCHEDULER &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;optim&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;lr_scheduler&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;ExponentialLR&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;OPT&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; gamma&lt;span style="color:#0550ae"&gt;=&lt;/span&gt;&lt;span style="color:#0550ae"&gt;0.9&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;for&lt;/span&gt; EPOCH_INDEX &lt;span style="color:#0550ae"&gt;in&lt;/span&gt; &lt;span style="color:#6639ba"&gt;range&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;EPOCH_NUM&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;for&lt;/span&gt; SAMPLE &lt;span style="color:#0550ae"&gt;in&lt;/span&gt; DATALOADER_FOR_TRAIN&lt;span style="color:#1f2328"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; SAMPLE &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; SAMPLE&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;to&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;device&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34;cuda&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#cf222e"&gt;if&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;cuda&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;is_available&lt;span style="color:#1f2328"&gt;()&lt;/span&gt; &lt;span style="color:#cf222e"&gt;else&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;device&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34;cpu&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; VALUE_PREDICTED &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;forward&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;SAMPLE&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; VALUE_IDEAL &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; CALC_VALUE_IDEAL&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;SAMPLE&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LOSS &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;nn&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;MSELoss&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;VALUE_PREDICTED&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; VALUE_IDEAL&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LOSS&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;requires_grad_&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;True&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; OPT&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;zero_grad&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LOSS&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;backward&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; OPT&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;step&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LEARNING_RATE_SCHEDULER&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;step&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;save&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;state_dict&lt;span style="color:#1f2328"&gt;(),&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;NEURAL_NETWORK_WEIGHT.pth&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;def&lt;/span&gt; &lt;span style="color:#6639ba"&gt;TEST&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; DATALOADER_FOR_TEST&lt;span style="color:#1f2328"&gt;):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;load_state_dict&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;load&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34;NEURAL_NETWORK_WEIGHT.pth&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#cf222e"&gt;False&lt;/span&gt;&lt;span style="color:#1f2328"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;with&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;no_grad&lt;span style="color:#1f2328"&gt;():&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;for&lt;/span&gt; SAMPLE &lt;span style="color:#0550ae"&gt;in&lt;/span&gt; DATALOADER_FOR_TEST&lt;span style="color:#1f2328"&gt;:&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; SAMPLE &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; SAMPLE&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;to&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;device&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34;cuda&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#cf222e"&gt;if&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;cuda&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;is_available&lt;span style="color:#1f2328"&gt;()&lt;/span&gt; &lt;span style="color:#cf222e"&gt;else&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;device&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0a3069"&gt;&amp;#34;cpu&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; VALUE_PREDICTED &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#6a737d"&gt;self&lt;/span&gt;&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;forward&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;SAMPLE&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; VALUE_IDEAL &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; CALC_VALUE_IDEAL&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;SAMPLE&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; LOSS &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; torch&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;nn&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;MSELoss&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;VALUE_PREDICTED&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; VALUE_IDEAL&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item><item><title>Machine Learning</title><link>https://shakaxi.github.io/posts/ml/</link><pubDate>Wed, 25 Nov 2026 08:04:32 +0000</pubDate><guid>https://shakaxi.github.io/posts/ml/</guid><description>&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;Introducation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;p&gt;Machine learning (ML) is a subfield of artificial intelligence (AI).&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-2"&gt;&#10;&lt;h2 id="headline-2"&gt;&#10;Application&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-3"&gt;&#10;&lt;h3 id="headline-3"&gt;&#10;Image processing&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Image tagging&lt;/li&gt;&#10;&lt;li&gt;Optical character recognition (OCR)&lt;/li&gt;&#10;&lt;li&gt;Self-driving cars&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-3"&gt;&#10;&lt;h3 id="headline-4"&gt;&#10;Text analysis&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Spam filtering&lt;/li&gt;&#10;&lt;li&gt;Sentiment analysis&lt;/li&gt;&#10;&lt;li&gt;Information extract&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-5" class="outline-3"&gt;&#10;&lt;h3 id="headline-5"&gt;&#10;Data mining&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;&#10;&lt;p&gt;Discover patterns and make predictions from data.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Anomaly detection&lt;/li&gt;&#10;&lt;li&gt;Association rules&lt;/li&gt;&#10;&lt;li&gt;Grouping&lt;/li&gt;&#10;&lt;li&gt;Predictions&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-6" class="outline-3"&gt;&#10;&lt;h3 id="headline-6"&gt;&#10;Video games &amp;amp; robotics&#10;&lt;/h3&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-7" class="outline-2"&gt;&#10;&lt;h2 id="headline-7"&gt;&#10;Algorithms&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-7" class="outline-text-2"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;ML algorithms try to solve the same problem: learn to transform every input to the correct output.&lt;/li&gt;&#10;&lt;li&gt;ML algorithms try to make generalizations, and create the simplest hypothesis (with fewest assumptions) that explains most of the training examples.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div id="outline-container-headline-8" class="outline-3"&gt;&#10;&lt;h3 id="headline-8"&gt;&#10;Categories&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-8" class="outline-text-3"&gt;&#10;&lt;div id="outline-container-headline-9" class="outline-4"&gt;&#10;&lt;h4 id="headline-9"&gt;&#10;Supervised learning&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-9" class="outline-text-4"&gt;&#10;&lt;p&gt;For each training example, the correct output must be provided.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-10" class="outline-4"&gt;&#10;&lt;h4 id="headline-10"&gt;&#10;Unsupervised learning&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-10" class="outline-text-4"&gt;&#10;&lt;p&gt;The training examples but not the desired output are the input.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-11" class="outline-3"&gt;&#10;&lt;h3 id="headline-11"&gt;&#10;Algorithms&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-11" class="outline-text-3"&gt;&#10;&lt;div id="outline-container-headline-12" class="outline-4"&gt;&#10;&lt;h4 id="headline-12"&gt;&#10;Support vector machines (SVM&amp;#39;s)&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-12" class="outline-text-4"&gt;&#10;&lt;p&gt;The model tries to build a set of hyperplanes in a high dimensional space which tries to separate instances of different classes by getting the largest separation between the nearest instances from different classes.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-13" class="outline-4"&gt;&#10;&lt;h4 id="headline-13"&gt;&#10;Probabilistic models&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-13" class="outline-text-4"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The models try to predict the correct response by modeling the problem with a probability distribution, e.g., Naive Bayes classifiers.&lt;/li&gt;&#10;&lt;li&gt;It returns the prediction as well as the corresponding degree of certainty.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-14" class="outline-4"&gt;&#10;&lt;h4 id="headline-14"&gt;&#10;Deep learning&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-14" class="outline-text-4"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;It is based on the artificial neural network models. They try to emulate (in a very simplified way) the way the brain works.&lt;/li&gt;&#10;&lt;li&gt;The network consist of a huge set of interconnected neurons, i.e., the basic processing unit, organized in various layers.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-15" class="outline-3"&gt;&#10;&lt;h3 id="headline-15"&gt;&#10;Feature engineering&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-15" class="outline-text-3"&gt;&#10;&lt;div id="outline-container-headline-16" class="outline-4"&gt;&#10;&lt;h4 id="headline-16"&gt;&#10;Feature extraction&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-16" class="outline-text-4"&gt;&#10;&lt;p&gt;Before fed to the ML algorithms, the raw data should be tranformed into a vector of features.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-17" class="outline-4"&gt;&#10;&lt;h4 id="headline-17"&gt;&#10;Feature selection&#10;&lt;/h4&gt;&#10;&lt;div id="outline-text-headline-17" class="outline-text-4"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Score each feature and return only the most valuable ones.&lt;/li&gt;&#10;&lt;li&gt;Do not use huge feature sets.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-18" class="outline-3"&gt;&#10;&lt;h3 id="headline-18"&gt;&#10;Performance metric&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-18" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The instances for training and testing must be disjoint.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item></channel></rss>