一、Transforms
1.Transforms的结构与用法
from PIL import Image
from torchvision import transforms
img = image.open(img.path)
tensor_trans = transforms.ToTensor()
tensor_img = tensor.trans(img)
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img = image.open(img.path)
writer = SummaryWriter("logs")
tensor_trans = transforms.ToTensor()
tensor_img = tensor.trans(img)
writer.add_image("Tensor_img",tensor_img)
writer.close()
2.常见的Transforms
(1)关注点
- 输入
- 输出
- 作用
(2)代码
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img = image.open(img.path)
writer = SummaryWriter("logs")
# ToTensor
tensor_trans = transforms.ToTensor()
tensor_img = tensor.trans(img)
writer.add_image("Tensor_img",tensor_img)
# Normalize
trans_more = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm,2)
# Resize
print(img.size)
trans_resize = transforms.Resize((512,512))
# img PIL -> resize -> img_resize PIL
img_resize = trans_reszie(img)
# img_resize PIL -> totensor ->img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize,0)
print(img_resize)
# Compose -resize -2
trans_reszie_2 = transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize",img_resize_2,1)
#RandomCrop
trans_random = transforms.RandomCrop(500,1000)
trans_compose_2 = transforms_Compose([trans_random,trans_totensor])
for i in range(10):
img_crop = trans_Compose_2(img)
writer.add_image("RandomCrop",img_crop,i)
writer.close()
class Person:
def __call__(self,name):
print("__call__" + "hello" + name)
def hello(self,name):
print("hello" + name)
person = Person()
person("zhangsan")
person.hello("lisi")
二、Google Colab
1.What is Colab?
Colab, or “Colaboratory”, allows you to write and execute Python in your
browser, with
- Zero configuration required
- Free access to GPUs
- Easy sharing
2.Useful Linux Commands (in Colab)
- ls : List all files in the current directory
- ls -l : List all files in the current directory with more detail
- pwd : Output the working directory
- mkdir <dirname>: Create a directory <dirname>
- cd <dirname> : Move to directory <dirname>
- gdown : Download files from google drive
- wget : Download files from the internet
- python <python_file>: Executes a python file
三、深度学习
1.Three Steps for Deep Learning
- Step 1: Neural Network
- Step 2: goodness of function
- Step 3: pick the best function
2.Neural Network
Different connection leads to different network structures.
Network parameter 𝜃: all the weights and biases in the “neurons”
3.goodness of function
4.pick the best function
四、Backpropagation
To compute the gradients efficiently, we use backpropagation.
1.Forward pass
Compute 𝜕𝑧 / 𝜕𝑤 for all parameters
2.Backward pass
Compute 𝜕𝐶 / 𝜕𝑧 for all activation function inputs z
𝜎′ (z) is a constant because z is
already determined in the forward pass.
3.Summary
五、预测神奇宝贝Pokemon
1.Step 1: Model
y = b +xcp
(w and b are parameters (can be any value))
Linear model: y = b + Σwixi
2.Step 2: Goodness of Function
3.Step 3: Best Function
最终形成一下等高线图
4.result
使用训练集和测试集的平均误差来验证模型:
5.Generalization
优化上述模型:
A more complex model does not always lead to better performance on testing data.
This is Overfitting. => Select suitable model
6.考虑其他的因素
我们需要考虑其他的属性是否会影响最终的结果。
7.重新定义function
(1)Back to step 1: Redesign the Model
(2)Back to step 2: Regularization
- Training error: larger𝜆, considering the training error less
- We prefer smooth function, but don’t be too smooth.