preamble
It's actually not hard to learn AI, it's like learning software development, it's just that relatively few people know how to do it, and some of those who do write articles and make videos and don't talk about it properly.
For example, coming up and telling you that you need to learn about tensors, or telling you that tensors are the multidimensionality of vectors, and so on in a pattern of explanations; all with the goal of letting people know that he'll be able to do this technique, but not wanting you to learn it.
For learning, years of learning experience, and countless times to review the learning process, have proved one thing, if an article, a video, a course, I did not learn to understand, then the problem must not be me, but the class of the active or passive do not want me to learn, so the problem must be the learning materials.
For example, English, after the real will, and then go back to look at their previous lessons, you know, that is the English teacher did not teach properly, which really want you to learn English, will tell you [come = come, out = out] ah.
For example, linear algebra, when you know it, you can go back and watch the videos of the course that you couldn't understand before, and you'll know that it's the class instructor's estimation of the key information that's vague.
Learning software development, I believe we all have a similar experience, when you want to learn a knowledge point, a variety of searches, just can not understand, and finally learned only two reasons, 1, you find the article that really teaches you the knowledge, 2, you through the search of the information, their own realization. This is actually proving that the vast majority of articles and videos don't want to actually teach you, including regular school teachers and textbooks.
introductory course
First of all, about the material I studied, I finally found the best study material through a bunch of searches, the video is serious about teaching you how to study, the only drawback might be that the person who takes the class speaks English with a bit of an Indian accent. However, I personally feel that he brings a bit of colloquialism, rather better to understand.
Focus on the following words that will be mentioned several times in the video, so you won't get stuck in English after paying attention.
numpy: the word, it's not a word, but it's a library for python.
vector: a vector, explained below.
tensor: tensor, explained below.
gradient: the gradient, which refers to my reference to taking partial derivatives below.
The address is:/watch?v=exaWOE8jvy8&list=PLqnslRFeH2UrcDBWF5mfPGpqQDSta6VK4&index=3
mounting
Both pytorch and tensorflow do AI, and prtorch's functions are relatively friendlier, so it's more efficient to get started.
pytorch official website address:/get-started/locally/
Before using pytorch, first install the environment, I use vscode here, after installing vscode, in the extension of the python package blind press some on it.
Generally speaking, the cpu version is used for learning. The installation command is as follows:
pip3 install torch torchvision torchaudio
If using gpu, the installation command is as follows:
pip3 install torch torchvision torchaudio --index-url /whl/cu124
After the installation is complete, execute the code and if it executes successfully, the installation is successful.
import torch
x =(1)
print(x)
Introduction of Terms
Matrix: that's our table.
Vector: This matrix with only one row/column is called vector.
1,1,0
Two-dimensional tensor (tensor2D): This matrix with multiple rows and columns is called a two-dimensional tensor.
1,1,0
1,0,1
A three-dimensional tensor (tensor3D), is a three-dimensional array.
A multidimensional tensor is the same as a multidimensional array.
Why are they called vectors or tensors? We put [1,1,0] change imagine on the line, [1,1,0] is from the origin to x = 1, y = 1, z = 0 launched out of the line, that [1,1,0] is not a data, it becomes a tensor. But the essence is still data. This place do not go into detail, understanding can be, after all, we are not the study of mathematics.
Getting Started with Code
Tensor basis using pytorch.
import torch
import numpy as np
x=(1) # Create a vector with only one element, the element value is undefined, and undefined is 0, which prints out as [0.], because the default element type is float32.
print(x)
x=(3) # Create a vector with three elements, the element values are undefined.
print(x)
x=(3,2) # create a matrix with undefined elements
print(x)
x=(3,2) # Create a 3*2 matrix with randomized values.
print(x)
x=(3,2) # Create a 3*2 matrix and assign 0 to it.
print(x)
x=(2,2) # Create a 2*2 matrix and assign the value 1, which prints out as [1.
print(x)
print("print type")
print() # dtype is the data type, will print out the type of the element, the printout is torch.float32
x=(3,3,dtype=) # creates a 3*3 matrix and assigns 1 to it, prints out as [1], which will not carry . This will not have the .
print(x)
x=(3,3,dtype=) # Create a 3*3 matrix and assign 1, prints out as [1.], double type again with . Now it's
print(x)
print(()) # size is a function, this prints the toString() feel, the value is [([3, 3])]
print(().numel()) # number of elements, value is 9
x=([2.2,3.1]) # customize the tensor
print(x)
print("=========== addition ============")
x=(3,3,dtype=) # Create a 3*3 matrix and assign the value 1, which prints out as [1], this will not carry . This will not have the .
print(x)
y =(3,3,dtype=)
print(y)
z=x+y # Matrix addition
print(z)
z=(x,y) #matrix addition
print(z)
print("=========== computing print(y.add_(x))============")
print(y.add_(x)) #add x to y
print("=========== subtraction ============")
z=x-y #Subtract the matrix
print(z)
z=(x,y) #Subtract matrices
print(z)
print("=========== calculate print(y.sub_(x))============")
print(y.sub_(x)) #subtract x from y
print("=========== multiplication ============") #This multiplication is multiplication of elements relative to each other, not linear algebra A23*A32
z=x*y #Matrix multiplication
print(z)
z=(x,y)
print(z)
print(y.mul_(x))
print("=========== division ============")
z=x/y # Matrix multiplication and division
print(z)
z=(x,y)
print(z)
print("=========== list ============")
x=(5,4) # create a 3*2 matrix and assign random values to it
print(x[:,0]) # print all rows, but only take the first column
print(x[0,:]) # print all the columns, but only the first one
print(x[0,0]) #print the elements with i=0 j=0
print(x[1,1].item()) #If you take only one element value, you can take its true value
print(x)
print("===========view can resize tensor============")
x=(5,4)
y=(20) #return a new tensor, this one returns a tensor with 20 elements in 1 row
print(y)
y=(-1,10)
print(y) # This returns 2 rows of 10 elements each, he does auto-adaptation
print(()) # output size
# print((-1,7)) # This doesn't work because it's not divisible by 7.
print("===========numpy numpy can only be used on cpu, not on gpu ============")
a=(5) # row vector, value is 1, elements are 5
b=() #returns a tensor of type numpy, which is equivalent to turning the type for computation, this function has parameters default is false, means use cpu
print(b,type(b))
# Here although a transferred type to b, but b and a are object wrappers, reference address is the same so when we give a +1, b will also +1
a.add_(1)
print(a)
print(b)# although a b is not of the same type, the values are changed
print("=========== from the way it was converted to a tensor tensor ============")
a = (5) # row vector 5 elements value is 1
b =torch.from_numpy(a) #numpy's ndarray to tensor Again, it's boxing and unboxing Modify the value of a b also changes
a+=1
print(b)
print(a)
print("===========gpu============")
if(.is_available()).
#CUDA refers to NVIDIA's parallel computing platform and programming model that utilizes the processing power of graphics processing units (GPUs) to accelerate compute-intensive tasks.
device =("cuda") #get the cuda driver
x=(5,device=device)#Created with the memory specified to use the cpu.
y=(5)#create with memory for cpu
y=(device)#transfer y to gpu
z=x+y #This operation is performed on the gpu's memory.
#()# This can't be executed because z is on the gpu's memory
z = ("cpu") #Go back to cpu
else.
print("this is cpu")
requires_grad example.
Here's the point of high math fundamentals.
First is the derivative, this everyone forgets can Baidu.
Partial derivatives: this one is the derivation of a function like f(x,y)=x+y, except that the derivation for x treats y as a constant c and vice versa.
print("===========requires_grad Example 1 ============")
#Use autograd (automatic differentiation)
x=(5,requires_grad=True) #default requires_grad is false 1,Calculate gradient: requires_grad is a boolean parameter that specifies whether or not a tensor needs to have its gradient calculated 2,Autoderivative: all operations performed on tensors using requires_grad=True are logged for later autoderivation using the backward() method.
print(x)
# Perform some operations on the tensor
y = x + 2
print(y)
# Do some more manipulation
z = y * y * 3
print("====== split line 1 =====")
print(z)
out = () # is a tensor operation that computes the average of all the elements of the tensor z. Think of it as f(x) = (x1+x2+x3+x4)/4 and then take the partial derivative for each x, before bringing the value back in
print("====== split line 2 =====")
print(out)
# Do backpropagation and calculate the gradient
()
print("====== split line 3 =====")
print() # output the gradient of x
print("===========requires_grad example 2 ============")
# Create a tensor and specify that the gradient needs to be computed
x = ([1.0, 2.0, 3.0], requires_grad=True)
# Define a scalar function squared in summation Equivalent to the function f()= x1²+ x2²,+ x3²
y = (2).sum()
# Perform backpropagation to compute the gradient x` of the gradient, which corresponds to the function f(x1,x2,x3)= x1²+ x2²+ x3², the three partial derivatives are 2x1,2x2,2x3, which are taken into the tensor value i.e., '[2x1,2x2,2x3]'.
()
# Output the gradient of x
print() # output tensor([2., 4., 6.])
print("===========requires_grad Example 3 ============")
# Create a tensor and specify that the gradient needs to be computed
x = ([[1.0, 2.0], [4.0, 5.0]], requires_grad=True)
# Define a scalar function square in summation Equivalent to the function f() = x1²+ x2²+ x3²+ x4²
y = (2).sum()
# Perform backpropagation and compute the gradient x` of the gradient, in this case a 2*2 matrix, but the computation is done by counting x by the number of elements, there is no row or column ij.
# It corresponds to the function f(x1,x2,x3,x4)= x1²+ x2²,+ x3²+ x4², the four partial derivatives are 2x1,2x2,2x3,2x4, brought to the value of the tensor i.e. '[2x1,2x2,2x4,2x5]'
()
# Output the gradient of x
print() # Output tensor([[2, 4][8,10]])
That's it for basic learning.
Note: This post is original, please contact the author for authorization and attribution for any form of reproduction!
If you think this article is still good, please click [Recommend] below, thank you very much!
/kiba/p/18346596