本文已参与「新人创作礼」活动,一起开启掘金创作之路。
Trick:纯demo,心在哪里,结果就在那里
# -*- coding: utf-8 -*-
# Author : szy
# Create Date : 2019/10/25
"""
Optimization 优化
牛顿法 (Newton’s method)
最小二乘法(Least Squares method)
梯度下降法 (Gradient Descent)
误差方程 (Cost Function)
平方差 (Mean Squared Error)
全局最优解(Global minima)
局部最优(Local minima)
特征(features)
代表特征(feature representation)
迁移学习(Transfer Learning)
tensor 张量
torch 火炬
变量 (Variable)
计算图, computational graph
激励函数 (Activation Function)
线性方程 (linear function)
非线性方程 (nonlinear function)
卷积神经网络 Convolutional neural networks--------------推荐的激励函数是 relu.
循环神经网络中 recurrent neural networks, 推荐的是 tanh 或者是 relu
"""
# numpy array 和 torch tensor
import torch
import numpy as np
import math
# np_data = np.arange(6).reshape((2, 3))
# torch_data = torch.from_numpy(np_data)
# tensor2array = torch_data.numpy()
# print(
# '\nnumpy array:', np_data, # [[0 1 2], [3 4 5]]
# '\ntorch tensor:', torch_data, # 0 1 2 \n 3 4 5 [torch.LongTensor of size 2x3]
# '\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]
# )
"======================================="
#Torch 中的数学运算
# abs 绝对值计算
"""
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # [1 2 1 2]
)
# sin 三角函数 sin
print(
'\nsin',
'\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
'\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
)
# mean 均值
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # 0.0
)
"""
#在 Torch 中的 Variable 就是一个存放会变化的值的地理位置. 里面的值会不停的变化.
# 就像一个裝鸡蛋的篮子, 鸡蛋数会不停变动. 那谁是里面的鸡蛋呢, 自然就是 Torch 的 Tensor 咯.
# 如果用一个 Variable 进行计算, 那返回的也是一个同类型的 Variable.
"""
import torch
from torch.autograd import Variable # torch 中 Variable 模块
# 先生鸡蛋
tensor = torch.FloatTensor([[1,2],[3,4]])
# 把鸡蛋放到篮子里, requires_grad是参不参与误差反向传播, 要不要计算梯度
variable = Variable(tensor, requires_grad=True)
print(variable)
print(tensor)
#Variable 计算, 梯度
t_out = torch.mean(tensor*tensor) # x^2
v_out = torch.mean(variable*variable) # x^2
print(t_out)
print(v_out) # 7.5
# 获取 Variable 里面的数据
print(variable)
print(variable.data)
print(variable.data.numpy())
"""
# Torch 中的激励函数
import torch
import torch.nn.functional as F # 激励函数都在这
from torch.autograd import Variable
# 做一些假数据来观看图像
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x = Variable(x)
x_np = x.data.numpy() # 换成 numpy array, 出图时用
# 几种常用的 激励函数
y_relu = F.relu(x).data.numpy()
y_sigmoid = F.sigmoid(x).data.numpy()
y_tanh = F.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()
# y_softmax = F.softmax(x) softmax 比较特殊, 不能直接显示, 不过他是关于概率的, 用于分类
import matplotlib.pyplot as plt # python 的可视化模块, 我有教程 (https://morvanzhou.github.io/tutorials/data-manipulation/plt/)
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')
plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')
plt.subplot(223)
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')
plt.subplot(224)
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='best')
plt.show()