[Experience and Bug] tensorflow sketch

1 Common knowledge

  • conda activate tf

    Used in anaconda prompt, enter the virtual environment named tf.

  • pip install <包名>==

    You can view all versions in which a given package can be found.

  • pip install <包名> -i https://pypi.org/simple

    Download the package from the official source.

  • Plot multiple plots :

    plt.figure(figsize=(10,10))
    plt.subplot(5,5,1)
    
  • Colab mounts Google Drive .

    from google.colab import drive
    import os
    # 挂载网盘
    drive.mount('/content/drive/')  
    # 切换路径
    os.chdir('/content/drive/MyDrive/rnn') 
    # 查看目录下文件
    !ls -ll # 和linux终端用法差不多 但是!(感叹号)不能少
    
  • List of python connections .

    a = [1, 2, 3]
    b = [4, 5, 6]
    c = a + b
    print(c)
    
    '''
    [1, 2, 3, 4, 5, 6]
    '''
    

    Similarly, tuples can also use "+" for concatenation.

2 Learn

  • Max Pooling enhances features and reduces data.

  • Convolutional Neural Network Theory and Algorithms https://bit.ly/2UGa7uH (Youtube)

  • In the convolutional layer, each filter corresponds to a bia

  • The automatic parameter search function of tensorflow is really a little expectation.

    I'm a little disappointed that the model I just searched for didn't work well. The number of layers is fixed in advance, and the number of convolution kernels, the size of convolution kernels, and the number of neurons in the fully connected layer are searched. Although two layers of convolution are missing in advance, the accuracy gap is really a bit big. Moreover, the size of the searched model is not easy to control. For example, the size of the model just searched is 30 times that of the model in the original code.

    • The model provided by the original code has 5 layers of convolution, and the accuracy of the verification set can maintain 100% in multiple epochs.

    • Search out the model, 3-layer convolution, the accuracy of the verification set is only 84%.

    I don't know if I can search for learning_rate and batch_size.

1) Unsolved doubts

1. How is the boundary problem of convolution or pooling handled, and what are the effects of different processing methods? For example, the (5, 5) feature map is subjected to (2, 2) kernel pooling or convolution.

2. The feature map in the convolution is visualized, but why are there some black grids in the middle? Is the feature map really black, or the logic problem of the display (for example, there is a step of post-processing of the feature map to cut to [0,255]).

insert image description here

2) Why is there a "bias"?

We know that for a one-variable linear function, only y = wxy=wxy=The expressive ability of w x is not enough. In order to express any straight line in the two-dimensional plane, we usey = wx + by=wx+by=wx+b . But can this b be replaced by a wx? Or:

y = w x + b y=wx+b y=wx+by = w 1 x 1 + w 2 x 2 y=w_1x_1+w_2x_2y=w1x1+w2x2A subset of ?

The answer should be no. Although for any b, some set (w, x) can be found such that wx = b wx=bwx=b . But what we want is fixed, that is, it is not affected by the input x.

3 Problem handling

1) jupyter environment

How to use jupyter notebook in different virtual environments of conda? After I used conda install nb_conda_kernels, I still can't switch the desired environment.

In the Conda tab of jupyter notebook, only two ticked environments can be used.

insert image description here

I plan to give up jupyter notebook temporarily, after all, it is not necessary, I can continue to use my pycharm.

It seems that jupyter notebook is still very useful, especially when I haven't learned to save the model, I have to retrain every time I run the code.

Regarding running jupyter notebook in a specified virtual environment, this tutorial can work: open jupyter notebook in Anaconda virtual environment .

Specify the directory to run jupyter

Divided into the following steps:

  • Start cmd in the target directory;
  • conda activate <env_name>, activate the environment where you installed jupyter notebook;
  • jupyter notebook, start the notebook.

2) Keras version

Running the following piece of code reports an error,

import keras
import numpy as np

# 构建模型
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
# 准备训练数据
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
# 训练模型
model.fit(xs, ys, epochs=500)
# 使用模型
print(model.predict([10.0]))

When installing keras, I directly pip install keras, but there is actually a corresponding relationship tensorflowwith the previous version, just uninstall and re-download the corresponding version. For version relationship, please refer to: Pro-test to solve the error of importing Keras .keraskeras

3) Why the accuracy is 100%, and the parameters are still being updated during iteration?

For this question, you can look back at the formula of the cross-entropy loss function (how you use it).

The key is, loss = S um ( − 1 ∗ log (predicted probability of correctly classified samples) ) loss=Sum(-1*log(predicted probability of correctly classified samples))loss=Sum(1l o g ( predicted probability of correctly classified samples ) ) As long as the predicted probability of the model output has not become 100%, the parameters will continue to be updated .


Guess you like

Origin blog.csdn.net/m0_63238256/article/details/130312692