Keras學習一(翻譯)

2021-10-01 13:41:21 字數 3485 閱讀 2877

本來做完模式識別的作業後,感覺現在讀研完全避不開深度學習的坑,想系統的學一下keras,結果查詢中文文件後發現翻譯的不全,學也學不系統,乾脆我自己來看英文文件,一邊學一邊翻譯,以後自己要查詢時也方便。(怨念~~~~)

前言:乙個簡單地keras模型步驟為:

sequential -> compile ->trian

getting started with the keras sequential model

keras 的核心資料結構是 model,一種組織網路層的方式。最簡單的模型是 sequential 順序模型,它由多個網路層線性堆疊。對於更複雜的結構,你應該使用 keras 函式式 api,它允許構建任意的神經網路圖。

sequential模型可以直接在乙個模組裡寫好:

from keras.models import sequential

from keras.layers import dense, activation

model = sequential([

dense(32, input_shape=(784,)),

activation('relu'),

dense(10),

activation('softmax'),

])

也可以通過.add()一層一層的往上加:

model = sequential()

model.add(dense(32, input_dim=784))

model.add(activation('relu'))

指定輸入資料的shape

模型需要知道它要接受何種shape的input(輸入的資料),因此,第一層layer(也只有第一層需要指定,因為接下來的layers可以自動推斷這一層的shape)需要接收input的shape。有以下幾種方式:

model = sequential()

model.add(dense(32, input_shape=(784,)))

model = sequential()

model.add(dense(32, input_dim=784))

compilation

在訓練乙個模型之前,你需要首先配置學習過程,通過compile方法,它接收三個引數:

#多分類問題

model.compile(optimizer='rmsprop',

loss='categorical_crossentropy',

metrics=['accuracy'])

# 二分類問題

model.compile(optimizer='rmsprop',

loss='binary_crossentropy',

metrics=['accuracy'])

# 均方誤差回歸問題

model.compile(optimizer='rmsprop',

loss='mse')

# 自定義指標

import keras.backend as k

def mean_pred(y_true, y_pred):

return k.mean(y_pred)

model.compile(optimizer='rmsprop',

loss='binary_crossentropy',

metrics=['accuracy', mean_pred])

training

keras模型是在輸入資料和標籤的numpy陣列上訓練的。對於訓練模型,您通常會使用fit函式。

# for a single-input model with 2 classes (二分類問題):

model = sequential() #sequential

model.add(dense(32, activation='relu', input_dim=100))

model.add(dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', #conpile

loss='binary_crossentropy',

metrics=['accuracy'])

# generate dummy data #train

import numpy as np

data = np.random.random((1000, 100))

labels = np.random.randint(2, size=(1000, 1))

# train the model, iterating on the data in batches of 32 samples

model.fit(data, labels, epochs=10, batch_size=32)

# for a single-input model with 10 classes (多分類問題):

model = sequential()

model.add(dense(32, activation='relu', input_dim=100))

model.add(dense(10, activation='softmax'))

model.compile(optimizer='rmsprop',

loss='categorical_crossentropy',

metrics=['accuracy'])

# generate dummy data

import numpy as np

data = np.random.random((1000, 100))

labels = np.random.randint(10, size=(1000, 1))

# convert labels to categorical one-hot encoding

one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

# train the model, iterating on the data in batches of 32 samples

model.fit(data, one_hot_labels, epochs=10, batch_size=32)

所有的基本步驟就是這些,本章翻譯自原文 guide to the sequential model章節,此章後面有幾個example來熟練,感興趣的可以去看看:

多層感知器(mlp)多類軟最大分類

用於二分類的mlp

使用lstm進行序列分類

用一維卷積進行序列分類

堆疊lstm序列分類

等…keras官方英文文件

跳躍NLP曲線 自然語言處理研究綜述(一)(翻譯)

自然語言處理 nlp 是一種在計算機領域中以理論為驅動,用於人類語言的自動分析和表示的技術。nlp研究從打卡和批量處理的時代發展而來 其中一句話的分析可能就需要7分鐘 到現在的谷歌時代和它的推薦系統 可在不到一秒的時間內處理數百萬個網頁 本綜述文章借鑑了nlp研究的最新進展,以全新的視角審視nlp技...

keras初步學習

import os os.environ keras backend theano import keras 這樣就可以將keras依賴於theano using theano backend.2.曲線擬合 importos os.environ keras backend theano impor...

Keras學習筆記

手冊 keras中文文件 1.張量 一階張量是向量,二階張量是矩陣。2.batch batch gradient descent,遍歷全部資料集算一次損失函式,然後算函式對各個引數的梯度,更新梯度。太慢。stochastic gradient descent,每看乙個資料就算一下損失函式,然後求梯度...