iT邦幫忙

1

Day24 參加職訓(機器學習與資料分析工程師培訓班),Python程式設計 & Pytorch

  • 分享至 

  • xImage
  •  

上午: Python程式設計
今日教學keras使用內建mnist dataset練習

from tensorflow.keras.datasets import mnist

(train_x, train_y), (test_x, test_y) = mnist.load_data()
# 檢視內容
import matplotlib.pyplot as plt

plt.gcf().set_size_inches(15,4)

for i in range(5):
    axe = plt.subplot(1, 5, 1+i)
    axe.imshow(train_x[i], cmap = 'gray')
    axe.set_title(f'label = {train_y[i]}', fontsize = 18)
plt.show()
from tensorflow.keras.utils import to_categorical

X_train = train_x.reshape(60000, 28*28)
X_test = test_x.reshape(10000, 28*28)

X_train = X_train.astype('float')/255 #資料型態從 unit8 轉成浮點數,並做正規化
X_test = X_test.astype('float')/255

Y_train = to_categorical(train_y)  # one-hot encoding
Y_test = to_categorical(test_y)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(512, activation = 'relu', input_dim = 28*28))
model.add(Dense(10, activation = 'softmax'))

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy
model.fit(X_train, Y_train, epochs=10, batch_size=128)
test_loss, test_accuracy = model.evaluate(X_test, Y_test)
print(f'Loss = {test_loss}, Accuracy = {test_accuracy}')
#儲存模型
model.save('mnist_model.h5')

# Load model
from tensorflow.keras.models import load_model
model = load_model('mnist_model.h5')
model.summary()

下午:Pytorch 與深度學習初探

使用Google Colab撰寫程式,依照步驟執行

!pip install torch
# step1: load data (generate)
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np



def plot_fit(option=0):
  plt.scatter(X,Y)
  mx=np.array([-20,20])
  if option==0: 
    [w,b]=[-7,5]
  else:
    [w,b]=model.parameters()
    w=w[0][0].detach().item()
    b=b[0].detach().item()
  my=w*mx+b  
  plt.plot(mx,my,'r')
  plt.show()


X=torch.randn(200,1)*10
Y=3*X+torch.randn(200,1)*5
plot_fit()

# step2: preprocessing X,Y
print("X,Y ready")

# step3: build model
model=nn.Linear(1,1)
torch.nn.init.xavier_uniform_(model.weight)
[w,b]=model.parameters()
print(w,b)
criterion= nn.MSELoss()
optimizer= torch.optim.SGD(model.parameters(),lr=0.0001)

# step4: traing model
torch.manual_seed(1)
epochs=10000
losses=[]

for e in range(epochs):
  preY=model.forward(X)
  loss=criterion(preY,Y)
  losses.append(loss)

  optimizer.zero_grad()
  loss.backward()
  optimizer.step()

plt.plot(range(epochs),losses)
plt.show()

plot_fit(1)
plt.show()
# step5: evaluate model (draw)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言