Tag Archives: Machine learning

Syntax error: invalid syntax before Python string

The usage of adding f before Python string


import time
t0 = time.time ()
time.sleep (1)
name = ‘processing’

#Starting with {f} indicates that Python expressions in braces are supported in strings
Print (f ‘{name} done in{ time.time () – t0:.2f} s’) 

processing done in 1.00 s


Why report a mistake

This usage is only used after 3.6. Mine is Python 3.5, which is so direct and simple


What’s the way?Of course, it’s anaconda

The difference between LSTM and Gru

First, some conclusions are given

The performance of Gru and LSTM is equal in many tasks. With fewer Gru parameters, it is easier to converge, but LSTM performs better when the data set is large. Structurally, Gru has only two gates (update and reset), LSTM has three gates (forge, input and output). Gru directly transfers the hidden state to the next unit, while LSTM packages the hidden state with memory cell.

1. Basic structure

1.1 GRU

Gru is designed to better capture long-term dependencies. Let’s look at the input first




How can Gru calculate the output


Reset gate


Responsible for decision making


For new memory


How important is it if


If it’s about 0,


It will not be passed to new memory


new memory


It’s a new input


And the hidden state of the previous moment


The summary of this paper. Calculate the new vector summed up


Contains the above information and new input


. Update gate


Responsible for deciding how much to pass




. If


If it’s about one,


Almost directly copied to


On the contrary, if


About 0, new memory


Pass directly to


. Hidden state:






The weight of the two is determined by update gate


Control. ​

1.2 LSTM

LSTM is also designed to better capture long-term dependencies, but the structure is different and more complex. Let’s take a look at the calculation process:

The new memory cell step is similar to the new memory in Gru. The output vector


It’s all about new input


And the hidden state of the previous moment


The summary of this paper. Input gate


Responsible for determining input


Whether the information is worth saving. Forget gate


Responsible for determining past memory cell




It’s important. final memory cell






The weight is determined by forge gate and input gate respectively. The output gate is not available in Gru. It’s responsible for making decisions


Which parts of should be passed to hidden state


2. Difference

1. Control of memory

LSTM: controlled by output gate and transmitted to the next unit

Gru: pass it directly to the next unit without any control

2. Input gate and reset gate have different action positions

LSTM: computing new memory


Instead of controlling the information of the previous moment, we use forge gate to achieve this independently

Gru: computing new memory


Reset gate is used to control the information of the previous time.

3. Similar

The biggest similarity is that addition is introduced in the update from t to T-1.

The advantage of this addition is that it can prevent gradient dispersion, so LSTM and Gru are better than RNN.

1. https://cs224d.stanford.edu/lecture_ notes/LectureNotes4.pdf
2. Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling
3. https://feature.engineering/difference-between-lstm-and-gru-for-rnns/

The Usage of Np.random.uniform()

np.random.uniform (low=0.0, high=1.0, size=None)

Function: random sampling from a uniform distribution [low, high]. Note that the definition field is left closed and right open, that is, it contains low but not high

Low: sampling lower bound, float type, default value is 0; high: sampling upper bound, float type, default value is 1; size: output sample number, int or tuple type, for example, size = (m, N, K), then output MNK samples, default value is 1. Return value: ndarray type, whose shape is consistent with the description in the parameter size.

The uniform () method randomly generates the next real number, which is in the range [x, y]

Evenly distributed, left closed, right open

np.random.uniform(1.75, 1, 100000000)
array([1.25930467, 1.40160844, 1.53509096, ..., 1.57271193, 1.25317863,

Draw a picture to see the distribution

import matplotlib.pyplot as plt
# Generate a uniformly distributed random number
x1 = np.random.uniform(-1, 1, 100000000) # output the number of samples 100000000

# Draw a graph to see the distribution
# 1) Create a canvas
plt.figure(figuresize=(20, 8), dpi=100)

# 2) Plot the histogram
plt.hist(x1, 1000) # x represents the data to be used, bins represents the number of intervals to be divided

# 3) Display the image




numpy.random.randn (d0, d1, … , DN) is to return one or more sample values from the standard normal distribution.  
numpy.random.rand (d0, d1, … , DN) in [0,1].   


numpy.random.rand (d0,d1,… ,dn)

The rand function generates data between [0,1] according to the given dimension, including 0 and excluding 1DN table. The return value of each dimension is the array of the specified dimension

array([[0.64959905, 0.14584702],
       [0.56862369, 0.5992007 ],
       [0.42512475, 0.83075541],
       [0.75685279, 0.00910825]])

np.random.rand(4,3,2) # shape: 4*3*2
array([[[0.07304796, 0.48810928],
        [0.59523586, 0.83281804],
        [0.47530734, 0.50402275]],

       [[0.63153869, 0.19636159],
        [0.93727986, 0.13564719],
        [0.11122609, 0.59646316]],

       [[0.17276155, 0.66621767],
        [0.81926792, 0.28781293],
        [0.20228714, 0.72412133]],

       [[0.29365696, 0.53956076],
        [0.19105394, 0.47044441],
        [0.85930046, 0.3867359 ]]])


PIP installation error: Microsoft Visual C + + 14.0 is required perfect solution

In the process of using Python development, we often need to install various modules, but we often encounter various errors in the installation process, such as: error: Microsoft Visual C + + 14.0 is required, this error is due to the lack of C + + compiler components.

There are two solutions to this problem

One is to directly download the corresponding wheel file for installation, providing three download addresses:

Tsinghua download (domestic site, fast speed) https://pypi.tuna.tsinghua.edu.cn/simple/pip/

Official download: https://pypi.org/project/face-recognition/1.0.0/#files

Python package https://www.lfd.uci.edu/~gohlke/pythonlibs/

Another solution is to install the missing components.

As for the second solution, I believe most people will download a visual studio 201x and install it as I used to. However, installing a vs takes up a lot of space, not to mention the key. Some people find it useless after installation, which is really irritating.

After all kinds of search, it was found that the C + + runtime provided by the government can be directly installed, which can be solved perfectly. The address is Microsoft Visual C + + build tools 2015


After downloading, double-click run to install with default settings. After the installation, go to PIP install XXX to show that the installation is successful.


Welcome to my personal blog: the road of machine learning

【JupyterLab】JavaScript output is disabled in JupyterLab

Error when using Matplotlib to display images in jupyterlab:

JavaScript output is disabled in JupyterLab


terms of settlement:

Just install the plot extension of jupyterlab.

Please refer to https://github.com/jupyterlab/jupyter-renderers/tree/master/packages/plotly-extension


The steps are as follows:

Use the CMD command line to execute the following command

jupyter labextension install @jupyterlab/plotly-extension


…..\Anaconda3\Scripts\jupyter- labextension.exe install @jupyterlab/plotly-extension


The installation is successful.


Then restart the following jupyterlab!

Attributeerror: ‘bytes’ object has no attribute’ encode ‘

When training the model, the following error is reported:
attributeerror: ‘bytes’ object has no attribute’ encode ‘
solution: click error report to enter the corresponding error report document, and change encode to decode.
This is because in python3, the encoding distinguishes between string and binary https://www.jianshu.com/p/a4cf632d97f1

cannot import name ‘_validate_lengths’ from ‘numpy.lib.arraypad’

Error in importing skimage:

>>> import skimage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/AI/AN/lib/python3.7/site-packages/skimage/__init__.py", line 157, in <module>
    from .util.dtype import *
  File "/opt/AI/AN/lib/python3.7/site-packages/skimage/util/__init__.py", line 8, in <module>
    from .arraycrop import crop
  File "/opt/AI/AN/lib/python3.7/site-packages/skimage/util/arraycrop.py", line 8, in <module>
    from numpy.lib.arraypad import _validate_lengths
ImportError: cannot import name '_validate_lengths' from 'numpy.lib.arraypad' (/opt/AI/AN/lib/python3.7/site-packages/numpy/lib

Because it doesn’t match the numpy version, my numpy is 1.16

The version of numpy can be reduced or the version of skimage can be improved. When I finally use the latter, the former will report an error

ValueError: numpy.ufunc has the wrong size, try recompiling. Expected 192, got 216


1) View version:

[[email protected] datasets]# pip install scikit-image==9999
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement scikit-image==9999 (from versions: 0.7.2, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.3, 0.10.0, 0.10.1, 0.11.2, 0.11.3, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.13.0, 0.13.1, 0.14.0, 0.14.1, 0.14.2, 0.14.3, 0.14.4, 0.14.5, 0.15.0, 0.16.1, 0.16.2)
ERROR: No matching distribution found for scikit-image==9999

2) Install the latest

[[email protected] datasets]# pip install scikit-image==0.16.2
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting scikit-image==0.16.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dc/48/454bf836d302465475e02bc0468b879302145b07a005174c409a5b5869c7/scikit_image-0.16.2-cp37-cp37m-manylinux1_x86_64.whl (26.5MB)
     |████████████████████████████████| 26.5MB 1.8MB/s 
Requirement already satisfied: matplotlib!=3.0.0,>=2.0.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (2.2.3)
Requirement already satisfied: scipy>=0.19.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (1.1.0)
Requirement already satisfied: networkx>=2.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (2.1)
Requirement already satisfied: imageio>=2.3.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (2.4.1)
Requirement already satisfied: PyWavelets>=0.4.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (1.0.0)
Requirement already satisfied: pillow>=4.3.0 in /opt/AI/AN/lib/python3.7/site-packages (from scikit-image==0.16.2) (5.2.0)
Requirement already satisfied: numpy>=1.7.1 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (1.16.0)
Requirement already satisfied: cycler>=0.10 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (2.4.5)
Requirement already satisfied: python-dateutil>=2.1 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (2.7.3)
Requirement already satisfied: pytz in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (2018.5)
Requirement already satisfied: six>=1.10 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (1.13.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/AI/AN/lib/python3.7/site-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (1.0.1)
Requirement already satisfied: decorator>=4.1.0 in /opt/AI/AN/lib/python3.7/site-packages (from networkx>=2.0->scikit-image==0.16.2) (4.3.0)
Requirement already satisfied: setuptools in /opt/AI/AN/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib!=3.0.0,>=2.0.0->scikit-image==0.16.2) (41.0.0)
Installing collected packages: scikit-image
  Found existing installation: scikit-image 0.14.0
    Uninstalling scikit-image-0.14.0:
      Successfully uninstalled scikit-image-0.14.0
Successfully installed scikit-image-0.16.2

3) Try:

[[email protected] datasets]# python
Python 3.7.0 (default, Jun 28 2018, 13:15:42) 
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import skimage

Absolutely OK

[problem solving] target is multiclass but average =’binary ‘. Please choose another average setting

Today, when compiling Python code, we encountered the following error:
target is multiclass but average =’binary ‘. Please choose another average setting, one of [none,’ micro ‘,’ macro ‘,’ weighted ‘]

The original code is as follows, is to require a data set of F1 (precision and recall combined into a single index).

from sklearn.metrics import precision_score, recall_score

precision_score(y_train, y_train_pred)

terms of settlement

Average =’micro’is added to the original code.

from sklearn.metrics import precision_score, recall_score

precision_score(y_train, y_train_pred, average='micro')

The average parameter defines the calculation method of the index. In binary classification, the average parameter is binary by default; in multi classification, the optional parameters are micro, macro, weighted and samples.

None: returns the score of each class. Otherwise, this determines the average type of execution on the data.

Binary: only report the result POS of the specified class_ label。 Only if targets (Y_ Only when {true, PRED}) is binary.

Micro: global indicators are calculated by calculating total true positives, false negatives and false positives. That is to say, all classes are calculated together (to be specific to precision), and then the TP sum of all classes is divided by the sum of TP and FN of all classes. Therefore, precision and recall in micro method are equal to accuracy.

Macro: calculate the indicators of each tag to find their unweighted average. This does not take into account label imbalance. In other words, the precision of each class is calculated first, and then the arithmetic average is calculated.

Weighted: calculate the indicators of each tag, find their average value, and weight by support (the number of real instances of each tag). This would change the “macro” to address the label imbalance; it could result in an F-score that is not between precision and recall.

Samples: calculate the indicators of each instance and find their average value (only meaningful for different multi label classification)_ score)。

reference resources: https://blog.csdn.net/datongmu_ yile/article/details/81750737

Error occurred when Python called cv2.findcontours: valueerror: not enough values to unpack (expected 3, got 2)


Opencv old version, return three parameters:

im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

To return three parameters:

Just demote OpenCV to, and input PIP install opencv python = = in the terminal

Opencv new version call, return two parameters:

 contours, hierarchy = cv2.findContours(mask, cv2.RETR_ TREE, cv2.CHAIN_ APPROX_ SIMPLE)

Runtimeerror using Python training model: CUDA out of memory error resolution

RuntimeError: CUDA out of memory occurs using the PyTorch training model
Training: Due to the limited GPU video memory resources, the batchsize of training input should not be too large, which will lead to Out of Memory errors.
Solution: Reduce the batchSize to even 1
Use with torch.no_grad():fore testing the code