import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np# torch.manual_seed(1) # reproducible# Hyper Parameters
EPOCH =10
BATCH_SIZE =64
LR =0.005# learning rate
DOWNLOAD_MNIST =False
N_TEST_IMG =5# Mnist digits dataset
train_data = torchvision.datasets.MNIST(root='./mnist/',train=True,# this is training datatransform=torchvision.transforms.ToTensor(),# Converts a PIL.Image or numpy.ndarray to# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]download=DOWNLOAD_MNIST,# download it if you don't have it)# plot one exampleprint(train_data.train_data.size())# (60000, 28, 28)print(train_data.train_labels.size())# (60000)
plt.imshow(train_data.train_data[2].numpy(), cmap='gray')
plt.title('%i'% train_data.train_labels[2])
plt.show()# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)classAutoEncoder(nn.Module):def__init__(self):super(AutoEncoder, self).__init__()self.encoder = nn.Sequential(nn.Linear(28*28,128),nn.Tanh(),nn.Linear(128,64),nn.Tanh(),nn.Linear(64,12),nn.Tanh(),nn.Linear(12,3),# compress to 3 features which can be visualized in plt)self.decoder = nn.Sequential(nn.Linear(3,12),nn.Tanh(),nn.Linear(12,64),nn.Tanh(),nn.Linear(64,128),nn.Tanh(),nn.Linear(128,28*28),nn.Sigmoid(),# compress to a range (0, 1))defforward(self, x):encoded = self.encoder(x)decoded = self.decoder(encoded)return encoded, decodedautoencoder = AutoEncoder()optimizer = torch.optim.Adam(autoencoder.parameters(), lr=LR)
loss_func = nn.MSELoss()# initialize figure
f, a = plt.subplots(2, N_TEST_IMG, figsize=(5,2))
plt.ion()# continuously plot# original data (first row) for viewing
view_data = train_data.train_data[:N_TEST_IMG].view(-1,28*28).type(torch.FloatTensor)/255.for i inrange(N_TEST_IMG):a[0][i].imshow(np.reshape(view_data.data.numpy()[i],(28,28)), cmap='gray'); a[0][i].set_xticks(()); a[0][i].set_yticks(())for epoch inrange(EPOCH):for step,(x, b_label)inenumerate(train_loader):b_x = x.view(-1,28*28)# batch x, shape (batch, 28*28)b_y = x.view(-1,28*28)# batch y, shape (batch, 28*28)encoded, decoded = autoencoder(b_x)loss = loss_func(decoded, b_y)# mean square erroroptimizer.zero_grad()# clear gradients for this training steploss.backward()# backpropagation, compute gradientsoptimizer.step()# apply gradientsif step %100==0:print('Epoch: ', epoch,'| train loss: %.4f'% loss.data.numpy())# plotting decoded image (second row)_, decoded_data = autoencoder(view_data)for i inrange(N_TEST_IMG):a[1][i].clear()a[1][i].imshow(np.reshape(decoded_data.data.numpy()[i],(28,28)), cmap='gray')a[1][i].set_xticks(()); a[1][i].set_yticks(())plt.draw(); plt.pause(0.05)plt.ioff()
plt.show()# visualize in 3D plot
view_data = train_data.train_data[:200].view(-1,28*28).type(torch.FloatTensor)/255.
encoded_data, _ = autoencoder(view_data)
fig = plt.figure(2); ax = Axes3D(fig)
X, Y, Z = encoded_data.data[:,0].numpy(), encoded_data.data[:,1].numpy(), encoded_data.data[:,2].numpy()
values = train_data.train_labels[:200].numpy()for x, y, z, s inzip(X, Y, Z, values):c = cm.rainbow(int(255*s/9)); ax.text(x, y, z, s, backgroundcolor=c)
ax.set_xlim(X.min(), X.max()); ax.set_ylim(Y.min(), Y.max()); ax.set_zlim(Z.min(), Z.max())
plt.show()
4.5DQN强化学习
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import gym# Hyper Parameters
BATCH_SIZE =32
LR =0.01# learning rate
EPSILON =0.9# greedy policy
GAMMA =0.9# reward discount
TARGET_REPLACE_ITER =100# target update frequency
MEMORY_CAPACITY =2000
env = gym.make('CartPole-v0')
env = env.unwrapped
N_ACTIONS = env.action_space.n
N_STATES = env.observation_space.shape[0]
ENV_A_SHAPE =0ifisinstance(env.action_space.sample(),int)else env.action_space.sample().shape # to confirm the shapeclassNet(nn.Module):def__init__(self,):super(Net, self).__init__()self.fc1 = nn.Linear(N_STATES,50)self.fc1.weight.data.normal_(0,0.1)#正态分布初始权重,可能会有更好的效果self.out = nn.Linear(50, N_ACTIONS)self.out.weight.data.normal_(0,0.1)defforward(self, x):x = self.fc1(x)x = F.relu(x)actions_value = self.out(x)return actions_valueclassDQN(object):def__init__(self):self.eval_net, self.target_net = Net(), Net()self.learn_step_counter =0# for target updatingself.memory_counter =0# for storing memoryself.memory = np.zeros((MEMORY_CAPACITY, N_STATES *2+2))# initialize memoryself.optimizer = torch.optim.Adam(self.eval_net.parameters(), lr=LR)self.loss_func = nn.MSELoss()defchoose_action(self, x):x = torch.unsqueeze(torch.FloatTensor(x),0)# input only one sampleif np.random.uniform()< EPSILON:# greedyactions_value = self.eval_net.forward(x)action = torch.max(actions_value,1)[1].data.numpy()action = action[0]if ENV_A_SHAPE ==0else action.reshape(ENV_A_SHAPE)# return the argmax indexelse:# randomaction = np.random.randint(0, N_ACTIONS)action = action if ENV_A_SHAPE ==0else action.reshape(ENV_A_SHAPE)return actiondefstore_transition(self, s, a, r, s_):transition = np.hstack((s,[a, r], s_))# replace the old memory with new memoryindex = self.memory_counter % MEMORY_CAPACITYself.memory[index,:]= transitionself.memory_counter +=1deflearn(self):# target parameter updateif self.learn_step_counter % TARGET_REPLACE_ITER ==0:self.target_net.load_state_dict(self.eval_net.state_dict())self.learn_step_counter +=1# sample batch transitionssample_index = np.random.choice(MEMORY_CAPACITY, BATCH_SIZE)b_memory = self.memory[sample_index,:]b_s = torch.FloatTensor(b_memory[:,:N_STATES])b_a = torch.LongTensor(b_memory[:, N_STATES:N_STATES+1].astype(int))b_r = torch.FloatTensor(b_memory[:, N_STATES+1:N_STATES+2])b_s_ = torch.FloatTensor(b_memory[:,-N_STATES:])# q_eval w.r.t the action in experienceq_eval = self.eval_net(b_s).gather(1, b_a)# shape (batch, 1)q_next = self.target_net(b_s_).detach()# detach from graph, don't backpropagateq_target = b_r + GAMMA * q_next.max(1)[0].view(BATCH_SIZE,1)# shape (batch, 1)loss = self.loss_func(q_eval, q_target)self.optimizer.zero_grad()loss.backward()self.optimizer.step()dqn = DQN()print('\nCollecting experience...')for i_episode inrange(400):s = env.reset()ep_r =0whileTrue:env.render()a = dqn.choose_action(s)# take actions_, r, done, info = env.step(a)# modify the rewardx, x_dot, theta, theta_dot = s_r1 =(env.x_threshold -abs(x))/ env.x_threshold -0.8r2 =(env.theta_threshold_radians -abs(theta))/ env.theta_threshold_radians -0.5r = r1 + r2dqn.store_transition(s, a, r, s_)ep_r += rif dqn.memory_counter > MEMORY_CAPACITY:dqn.learn()if done:print('Ep: ', i_episode,'| Ep_r: ',round(ep_r,2))if done:breaks = s_
4.6GAN生成对抗网络
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt# torch.manual_seed(1) # reproducible# np.random.seed(1)# Hyper Parameters
BATCH_SIZE =64
LR_G =0.0001# learning rate for generator
LR_D =0.0001# learning rate for discriminator
N_IDEAS =5# think of this as number of ideas for generating an art work (Generator)
ART_COMPONENTS =15# it could be total point G can draw in the canvas
PAINT_POINTS = np.vstack([np.linspace(-1,1, ART_COMPONENTS)for _ inrange(BATCH_SIZE)])# show our beautiful painting range# plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound')# plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound')# plt.legend(loc='upper right')# plt.show()defartist_works():# painting from the famous artist (real target)a = np.random.uniform(1,2, size=BATCH_SIZE)[:, np.newaxis]paintings = a * np.power(PAINT_POINTS,2)+(a -1)paintings = torch.from_numpy(paintings).float()return paintingsG = nn.Sequential(# Generatornn.Linear(N_IDEAS,128),# random ideas (could from normal distribution)nn.ReLU(),nn.Linear(128, ART_COMPONENTS),# making a painting from these random ideas)D = nn.Sequential(# Discriminatornn.Linear(ART_COMPONENTS,128),# receive art work either from the famous artist or a newbie like Gnn.ReLU(),nn.Linear(128,1),nn.Sigmoid(),# tell the probability that the art work is made by artist)opt_D = torch.optim.Adam(D.parameters(), lr=LR_D)
opt_G = torch.optim.Adam(G.parameters(), lr=LR_G)plt.ion()# something about continuous plottingfor step inrange(10000):artist_paintings = artist_works()# real painting from artistG_ideas = torch.randn(BATCH_SIZE, N_IDEAS, requires_grad=True)# random ideas\nG_paintings = G(G_ideas)# fake painting from G (random ideas)prob_artist1 = D(G_paintings)# D try to reduce this probG_loss = torch.mean(torch.log(1.- prob_artist1))opt_G.zero_grad()G_loss.backward()opt_G.step()prob_artist0 = D(artist_paintings)# D try to increase this probprob_artist1 = D(G_paintings.detach())# D try to reduce this probD_loss =- torch.mean(torch.log(prob_artist0)+ torch.log(1.- prob_artist1))opt_D.zero_grad()D_loss.backward(retain_graph=True)# reusing computational graphopt_D.step()if step %50==0:# plottingplt.cla()plt.plot(PAINT_POINTS[0], G_paintings.data.numpy()[0], c='#4AD631', lw=3, label='Generated painting',)plt.plot(PAINT_POINTS[0],2* np.power(PAINT_POINTS[0],2)+1, c='#74BCFF', lw=3, label='upper bound')plt.plot(PAINT_POINTS[0],1* np.power(PAINT_POINTS[0],2)+0, c='#FF9359', lw=3, label='lower bound')plt.text(-.5,2.3,'D accuracy=%.2f (0.5 for D to converge)'% prob_artist0.data.numpy().mean(),fontdict={'size':13})plt.text(-.5,2,'D score= %.2f (-1.38 for G to converge)'%-D_loss.data.numpy(), fontdict={'size':13})plt.ylim((0,3));plt.legend(loc='upper right', fontsize=10);plt.draw();plt.pause(0.01)plt.ioff()
plt.show()
If you’ve been going through Medium, looking at technical articles, you’ve undoubtedly seen little windows that look like the below:如果您一直在阅读Medium,并查看技术文章,那么您无疑会看到类似于以下内容的小窗口: def hello_…
鲜为人知的6个黑科技网站Pandas is the go-to Python library for data analysis and manipulation. It provides numerous functions and methods that expedice the data analysis process.Pandas是用于数据分析和处理的Python库。 它提供了加速数据分析过程的众多功能和方法…
大熊猫卸妆后数据科学 (Data Science) Pandas is used mainly for reading, cleaning, and extracting insights from data. We will see an advanced use of Pandas which are very important to a Data Scientist. These operations are used to analyze data and manipulate…
数据eda数据科学和机器学习统计 (STATISTICS FOR DATA SCIENCE AND MACHINE LEARNING) Categorical variables are the ones where the possible values are provided as a set of options, it can be pre-defined or open. An example can be the gender of a person. In the …
jdk重启后步行“永远不要做出预测,尤其是关于未来的预测。” (KK Steincke) (“Never Make Predictions, Especially About the Future.” (K. K. Steincke)) Does this picture portray a horse or a car? 这张照片描绘的是马还是汽车? How likely is …
mongodb仲裁者Coming out of college with a background in mathematics, I fell upward into the rapidly growing field of data analytics. It wasn’t until years later that I realized the incredible power that comes with the position. As Uncle Ben told Peter Par…
优化 回归应用数据科学 (Applied data science) Price and quantity are two fundamental measures that determine the bottom line of every business, and setting the right price is one of the most important decisions a company can make. Under-pricing hurts the co…