Ceci est la partie 1 du mémo d'apprentissage pour "Deep-Learning from scratch".
tranche
x=[1,2,3,4,5]
#Affichage complet
x[:]
#1 à 2
x[0:2]
#Passer deux des 1, 3 et 5
x[::2]
#Ordre inverse,-Si défini sur 2, sautez deux de l'opposé
x[::-1]
numpy
#Forme 3x2
A = np.array([[1,2],
[3,4],
[5,6]])
#Forme 2 x 1
B = np.array([7,
8])
#Calcul du produit intérieur
np.dot(A,B)
>>>array([23,53,83])
monter
from google.colab import drive
drive.mount('/content/drive')
cd drive/My Drive/deep-learning-from-scratch-master/ch03
neuralnet
# coding: utf-8
import sys, os
sys.path.append(os.pardir) #Paramètres d'importation des fichiers dans le répertoire parent
import numpy as np
import pickle
from dataset.mnist import load_mnist
from common.functions import sigmoid, softmax
neuralnet
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
return x_test, t_test
neuralnet
def init_network():
with open("sample_weight.pkl", 'rb') as f:
network = pickle.load(f)
return network
neuralnet
def predict(network, x):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
a1 = np.dot(x, W1) + b1
z1 = sigmoid(a1)
a2 = np.dot(z1, W2) + b2
z2 = sigmoid(a2)
a3 = np.dot(z2, W3) + b3
y = softmax(a3)
return y
neuralnet
x, t = get_data()
network = init_network()
accuracy_cnt = 0
for i in range(len(x)):
y = predict(network, x[i])
p= np.argmax(y) #Obtenez l'index de l'élément le plus probable
if p == t[i]:
accuracy_cnt += 1
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
Le traitement par lots
x, t = get_data()
network = init_network()
batch_size = 100 #Nombre de lots
accuracy_cnt = 0
#range(start,stop,step)
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]
y_batch = predict(network, x_batch)
p = np.argmax(y_batch, axis=1)
accuracy_cnt += np.sum(p == t[i:i+batch_size])
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
Apprentissage profond à partir de zéro
Recommended Posts