keras筆記 mnist資料集上的簡單訓練

2021-09-05 09:10:52 字數 3054 閱讀 3668

學習了keras已經好幾天了,之前一直拒絕使用keras,但是現在感覺keras是真的好用啊,可以去嘗試一下啊。

首先展示的第乙個**,還是mnist資料集的訓練和測試,以下是**:

from keras.models import sequential

from keras.layers.core import activation, dropout

from keras.layers.core import dense

from keras.optimizers import sgd

from keras.datasets import mnist

from keras.utils import np_utils

import numpy as np

np.random.seed(1671)

#網路和訓練

nb_epoch = 20

batch_size = 128

verbose = 1

nb_classes = 10

optimizer = sgd()

n_hidden = 128

validation_split = 0.2

dropout = 0.3

#資料(x_train, y_train), (x_test, y_test) = mnist.load_data()

reshaped = 784

x_train = x_train.reshape(60000, reshaped)

x_test = x_test.reshape(10000, reshaped)

x_train = x_train.astype('float32')

x_test = x_test.astype('float32')

#歸一化

x_train /= 255

x_test /= 255

print(x_train.shape[0], 'train samples')

print(x_test.shape[0], 'test samples')

#將標籤變成二進位制類別矩陣

y_train = np_utils.to_categorical(y_train, nb_classes)

y_test = np_utils.to_categorical(y_test, nb_classes)

#模型的構建

model = sequential()

model.add(dense(n_hidden, input_shape=(reshaped,)))

model.add(activation('relu'))

model.add(dropout(dropout))

model.add(dense(n_hidden))

model.add(activation('relu'))

model.add(dense(nb_classes))

model.add(activation('softmax'))

model.summary()

#顯示模型結構

print(model.summary())

#配置訓練模型

model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

#模型訓練

history = model.fit(x_train, y_train,

batch_size=batch_size, epochs=nb_epoch,

verbose=verbose, validation_split=validation_split)

#模型測試

score = model.evaluate(x_test, y_test, verbose=verbose)

print('test score:', score[0])

print('test accuracy:', score[1])

感覺重要的**是model.compile,model.fit,model.evaluate具體可以去看keras中文網,當然可以看下面的。

compile(self, optimizer, loss=none, metrics=none, loss_weights=none, sample_weight_mode=none, weighted_metrics=none, target_tensors=none)
用於配置訓練模型。

引數

fit(self, x=none, y=none, batch_size=none, epochs=1, verbose=1, callbacks=none, validation_split=0.0, validation_data=none, shuffle=true, class_weight=none, sample_weight=none, initial_epoch=0, steps_per_epoch=none, validation_steps=none)
以固定數量的輪次(資料集上的迭代)訓練模型。

引數

返回

乙個history物件。其history.history屬性是連續 epoch 訓練損失和評估值,以及驗證集損失和評估值的記錄(如果適用)。

evaluate(self, x=none, y=none, batch_size=none, verbose=1, sample_weight=none, steps=none)
在測試模式,返回誤差值和評估標準值。

計算逐批次進行。

引數

返回

標量測試誤差(如果模型沒有評估指標)或標量列表(如果模型計算其他指標)。 屬性model.metrics_names將提供標量輸出的顯示標籤。

Keras匯入Mnist資料集出錯解決方案

exception url fetch failure on none winerror 10060 由於連線方在一段時間後沒有正確答覆或連線的主機沒有反應,連線嘗試失敗。def load data loads the mnist dataset.arguments path path where ...

Keras多層感知機識別MNIST資料集

windows 10,python 3.5 import numpy as np import pandas as pd from keras.utils import np utils from keras.datasets import mnist import matplotlib.pyplo...

MNIST資料集介紹

mnist資料集包含了6w張作為訓練資料,1w作為測試資料。在mnist資料集中,每一張都代表了0 9中的乙個數字,的大小都是28 28,且數字都會出現在的正中間。資料集包含了四個檔案 t10k images idx3 ubyte.gz 測試資料 t10k labels idx1 ubyte.gz ...