欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Keras建立模型并訓(xùn)練等一系列操作方式

 更新時(shí)間:2020年07月02日 12:03:24   作者:夏洛的網(wǎng)  
這篇文章主要介紹了使用Keras建立模型并訓(xùn)練等一系列操作方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

由于Keras是一種建立在已有深度學(xué)習(xí)框架上的二次框架,其使用起來非常方便,其后端實(shí)現(xiàn)有兩種方法,theano和tensorflow。由于自己平時(shí)用tensorflow,所以選擇后端用tensorflow的Keras,代碼寫起來更加方便。

1、建立模型

Keras分為兩種不同的建模方式,

Sequential models:這種方法用于實(shí)現(xiàn)一些簡單的模型。你只需要向一些存在的模型中添加層就行了。

Functional API:Keras的API是非常強(qiáng)大的,你可以利用這些API來構(gòu)造更加復(fù)雜的模型,比如多輸出模型,有向無環(huán)圖等等。

這里采用sequential models方法。

構(gòu)建序列模型。

def define_model():

  model = Sequential()

  # setup first conv layer
  model.add(Conv2D(32, (3, 3), activation="relu",
           input_shape=(120, 120, 3), padding='same')) # [10, 120, 120, 32]

  # setup first maxpooling layer
  model.add(MaxPooling2D(pool_size=(2, 2))) # [10, 60, 60, 32]

  # setup second conv layer
  model.add(Conv2D(8, kernel_size=(3, 3), activation="relu",
           padding='same')) # [10, 60, 60, 8]

  # setup second maxpooling layer
  model.add(MaxPooling2D(pool_size=(3, 3))) # [10, 20, 20, 8]

  # add bianping layer, 3200 = 20 * 20 * 8
  model.add(Flatten()) # [10, 3200]

  # add first full connection layer
  model.add(Dense(512, activation='sigmoid')) # [10, 512]

  # add dropout layer
  model.add(Dropout(0.5))

  # add second full connection layer
  model.add(Dense(4, activation='softmax')) # [10, 4]

  return model

可以看到定義模型時(shí)輸出的網(wǎng)絡(luò)結(jié)構(gòu)。

2、準(zhǔn)備數(shù)據(jù)

def load_data(resultpath):
  datapath = os.path.join(resultpath, "data10_4.npz")
  if os.path.exists(datapath):
    data = np.load(datapath)
    X, Y = data["X"], data["Y"]
  else:
    X = np.array(np.arange(432000)).reshape(10, 120, 120, 3)
    Y = [0, 0, 1, 1, 2, 2, 3, 3, 2, 0]
    X = X.astype('float32')
    Y = np_utils.to_categorical(Y, 4)
    np.savez(datapath, X=X, Y=Y)
    print('Saved dataset to dataset.npz.')
  print('X_shape:{}\nY_shape:{}'.format(X.shape, Y.shape))
  return X, Y

3、訓(xùn)練模型

def train_model(resultpath):
  model = define_model()

  # if want to use SGD, first define sgd, then set optimizer=sgd
  sgd = SGD(lr=0.001, decay=1e-6, momentum=0, nesterov=True)

  # select loss\optimizer\
  model.compile(loss=categorical_crossentropy,
         optimizer=Adam(), metrics=['accuracy'])
  model.summary()

  # draw the model structure
  plot_model(model, show_shapes=True,
        to_file=os.path.join(resultpath, 'model.png'))

  # load data
  X, Y = load_data(resultpath)

  # split train and test data
  X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.2, random_state=2)

  # input data to model and train
  history = model.fit(X_train, Y_train, batch_size=2, epochs=10,
            validation_data=(X_test, Y_test), verbose=1, shuffle=True)

  # evaluate the model
  loss, acc = model.evaluate(X_test, Y_test, verbose=0)
  print('Test loss:', loss)
  print('Test accuracy:', acc)

可以看到訓(xùn)練時(shí)輸出的日志。因?yàn)槭请S機(jī)數(shù)據(jù),沒有意義,這里訓(xùn)練的結(jié)果不必計(jì)較,只是練習(xí)而已。

保存下來的模型結(jié)構(gòu):

4、保存與加載模型并測試

有兩種保存方式

4.1 直接保存模型h5

保存:

def my_save_model(resultpath):

  model = train_model(resultpath)

  # the first way to save model
  model.save(os.path.join(resultpath, 'my_model.h5'))

加載:

def my_load_model(resultpath):

  # test data
  X = np.array(np.arange(86400)).reshape(2, 120, 120, 3)
  Y = [0, 1]
  X = X.astype('float32')
  Y = np_utils.to_categorical(Y, 4)

  # the first way of load model
  model2 = load_model(os.path.join(resultpath, 'my_model.h5'))
  model2.compile(loss=categorical_crossentropy,
         optimizer=Adam(), metrics=['accuracy'])

  test_loss, test_acc = model2.evaluate(X, Y, verbose=0)
  print('Test loss:', test_loss)
  print('Test accuracy:', test_acc)

  y = model2.predict_classes(X)
  print("predicct is: ", y)

4.2 分別保存網(wǎng)絡(luò)結(jié)構(gòu)和權(quán)重

保存:

def my_save_model(resultpath):

  model = train_model(resultpath)

  # the secon way : save trained network structure and weights
  model_json = model.to_json()
  open(os.path.join(resultpath, 'my_model_structure.json'), 'w').write(model_json)
  model.save_weights(os.path.join(resultpath, 'my_model_weights.hd5'))

加載:

def my_load_model(resultpath):

  # test data
  X = np.array(np.arange(86400)).reshape(2, 120, 120, 3)
  Y = [0, 1]
  X = X.astype('float32')
  Y = np_utils.to_categorical(Y, 4)

  # the second way : load model structure and weights
  model = model_from_json(open(os.path.join(resultpath, 'my_model_structure.json')).read())
  model.load_weights(os.path.join(resultpath, 'my_model_weights.hd5'))
  model.compile(loss=categorical_crossentropy,
         optimizer=Adam(), metrics=['accuracy']) 

  test_loss, test_acc = model.evaluate(X, Y, verbose=0)
  print('Test loss:', test_loss)
  print('Test accuracy:', test_acc)

  y = model.predict_classes(X)
  print("predicct is: ", y)

可以看到,兩次的結(jié)果是一樣的。

5、完整代碼

from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from keras.losses import categorical_crossentropy
from keras.optimizers import Adam
from keras.utils.vis_utils import plot_model
from keras.optimizers import SGD
from keras.models import model_from_json
from keras.models import load_model
from keras.utils import np_utils
import numpy as np
import os
from sklearn.model_selection import train_test_split

def load_data(resultpath):
  datapath = os.path.join(resultpath, "data10_4.npz")
  if os.path.exists(datapath):
    data = np.load(datapath)
    X, Y = data["X"], data["Y"]
  else:
    X = np.array(np.arange(432000)).reshape(10, 120, 120, 3)
    Y = [0, 0, 1, 1, 2, 2, 3, 3, 2, 0]
    X = X.astype('float32')
    Y = np_utils.to_categorical(Y, 4)
    np.savez(datapath, X=X, Y=Y)
    print('Saved dataset to dataset.npz.')
  print('X_shape:{}\nY_shape:{}'.format(X.shape, Y.shape))
  return X, Y

def define_model():
  model = Sequential()

  # setup first conv layer
  model.add(Conv2D(32, (3, 3), activation="relu",
           input_shape=(120, 120, 3), padding='same')) # [10, 120, 120, 32]

  # setup first maxpooling layer
  model.add(MaxPooling2D(pool_size=(2, 2))) # [10, 60, 60, 32]

  # setup second conv layer
  model.add(Conv2D(8, kernel_size=(3, 3), activation="relu",
           padding='same')) # [10, 60, 60, 8]

  # setup second maxpooling layer
  model.add(MaxPooling2D(pool_size=(3, 3))) # [10, 20, 20, 8]

  # add bianping layer, 3200 = 20 * 20 * 8
  model.add(Flatten()) # [10, 3200]

  # add first full connection layer
  model.add(Dense(512, activation='sigmoid')) # [10, 512]

  # add dropout layer
  model.add(Dropout(0.5))

  # add second full connection layer
  model.add(Dense(4, activation='softmax')) # [10, 4]

  return model

def train_model(resultpath):
  model = define_model()

  # if want to use SGD, first define sgd, then set optimizer=sgd
  sgd = SGD(lr=0.001, decay=1e-6, momentum=0, nesterov=True)

  # select loss\optimizer\
  model.compile(loss=categorical_crossentropy,
         optimizer=Adam(), metrics=['accuracy'])
  model.summary()

  # draw the model structure
  plot_model(model, show_shapes=True,
        to_file=os.path.join(resultpath, 'model.png'))

  # load data
  X, Y = load_data(resultpath)

  # split train and test data
  X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.2, random_state=2)

  # input data to model and train
  history = model.fit(X_train, Y_train, batch_size=2, epochs=10,
            validation_data=(X_test, Y_test), verbose=1, shuffle=True)

  # evaluate the model
  loss, acc = model.evaluate(X_test, Y_test, verbose=0)
  print('Test loss:', loss)
  print('Test accuracy:', acc)

  return model

def my_save_model(resultpath):

  model = train_model(resultpath)

  # the first way to save model
  model.save(os.path.join(resultpath, 'my_model.h5'))

  # the secon way : save trained network structure and weights
  model_json = model.to_json()
  open(os.path.join(resultpath, 'my_model_structure.json'), 'w').write(model_json)
  model.save_weights(os.path.join(resultpath, 'my_model_weights.hd5'))

def my_load_model(resultpath):

  # test data
  X = np.array(np.arange(86400)).reshape(2, 120, 120, 3)
  Y = [0, 1]
  X = X.astype('float32')
  Y = np_utils.to_categorical(Y, 4)

  # the first way of load model
  model2 = load_model(os.path.join(resultpath, 'my_model.h5'))
  model2.compile(loss=categorical_crossentropy,
          optimizer=Adam(), metrics=['accuracy'])

  test_loss, test_acc = model2.evaluate(X, Y, verbose=0)
  print('Test loss:', test_loss)
  print('Test accuracy:', test_acc)

  y = model2.predict_classes(X)
  print("predicct is: ", y)

  # the second way : load model structure and weights
  model = model_from_json(open(os.path.join(resultpath, 'my_model_structure.json')).read())
  model.load_weights(os.path.join(resultpath, 'my_model_weights.hd5'))
  model.compile(loss=categorical_crossentropy,
         optimizer=Adam(), metrics=['accuracy'])

  test_loss, test_acc = model.evaluate(X, Y, verbose=0)
  print('Test loss:', test_loss)
  print('Test accuracy:', test_acc)

  y = model.predict_classes(X)
  print("predicct is: ", y)

def main():
  resultpath = "result"
  #train_model(resultpath)
  #my_save_model(resultpath)
  my_load_model(resultpath)


if __name__ == "__main__":
  main()

以上這篇使用Keras建立模型并訓(xùn)練等一系列操作方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條

    WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條

    這篇文章主要介紹了WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python 編程語言詳細(xì)介紹

    Python 編程語言詳細(xì)介紹

    這篇文章主要分享的是 Python 編程語言的詳細(xì)介紹,Python 由 Guido van Rossum 設(shè)計(jì),作為“ABC”編程語言的繼承者,于 1991 年首次發(fā)布。它是一種高級通用語言,其設(shè)計(jì)理念是通過使用縮進(jìn)來強(qiáng)調(diào)代碼的可讀性。文章具有一定的參考價(jià)值,需要的朋友可以參考一下
    2021-11-11
  • 基于Python+Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器

    基于Python+Tkinter實(shí)現(xiàn)一個(gè)簡易計(jì)算器

    Tkinter作為Python的標(biāo)準(zhǔn)庫,是非常流行的Python GUI工具,同時(shí)也是非常容易學(xué)習(xí)的。本文將利用Tkinter繪制一個(gè)簡單的計(jì)算器,感興趣的可以試一試
    2022-01-01
  • python交互模式下輸入換行/輸入多行命令的方法

    python交互模式下輸入換行/輸入多行命令的方法

    這篇文章主要介紹了python交互模式下輸入換行/輸入多行命令的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python?webargs?模塊的簡單使用

    Python?webargs?模塊的簡單使用

    webargs是一個(gè)用于解析和驗(yàn)證HTTP請求對象的Python庫,今天通過本文給大家介紹Python?webargs?模塊的安裝使用,感興趣的朋友一起看看吧
    2022-01-01
  • python3多線程中使用線程睡眠的方法實(shí)現(xiàn)

    python3多線程中使用線程睡眠的方法實(shí)現(xiàn)

    線程睡眠是一個(gè)常見且有用的工具,用于控制線程的執(zhí)行順序和速度,本文主要介紹了python3多線程中使用線程睡眠的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài)

    Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài)

    這篇文章介紹了Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Jupyter Notebook 安裝配置與使用詳解

    Jupyter Notebook 安裝配置與使用詳解

    這篇文章主要介紹了Jupyter Notebook 安裝配置與使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 淺談python連續(xù)賦值可能引發(fā)的錯(cuò)誤

    淺談python連續(xù)賦值可能引發(fā)的錯(cuò)誤

    今天小編就為大家分享一篇淺談python連續(xù)賦值可能引發(fā)的錯(cuò)誤,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python如何讀取、寫入JSON數(shù)據(jù)

    Python如何讀取、寫入JSON數(shù)據(jù)

    這篇文章主要介紹了Python如何讀取、寫入JSON數(shù)據(jù),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論