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

淺談keras中l(wèi)oss與val_loss的關(guān)系

 更新時間:2020年06月22日 17:05:29   作者:lgy_keira  
這篇文章主要介紹了淺談keras中l(wèi)oss與val_loss的關(guān)系,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

loss函數(shù)如何接受輸入值

keras封裝的比較厲害,官網(wǎng)給的例子寫的云里霧里,

在stackoverflow找到了答案

You can wrap the loss function as a inner function and pass your input tensor to it (as commonly done when passing additional arguments to the loss function).

def custom_loss_wrapper(input_tensor):
 def custom_loss(y_true, y_pred):
  return K.binary_crossentropy(y_true, y_pred) + K.mean(input_tensor)
 return custom_loss
input_tensor = Input(shape=(10,))
hidden = Dense(100, activation='relu')(input_tensor)
out = Dense(1, activation='sigmoid')(hidden)
model = Model(input_tensor, out)
model.compile(loss=custom_loss_wrapper(input_tensor), optimizer='adam')

You can verify that input_tensor and the loss value will change as different X is passed to the model.

X = np.random.rand(1000, 10)
y = np.random.randint(2, size=1000)
model.test_on_batch(X, y) # => 1.1974642

X *= 1000
model.test_on_batch(X, y) # => 511.15466

fit_generator

fit_generator ultimately calls train_on_batch which allows for x to be a dictionary.

Also, it could be a list, in which casex is expected to map 1:1 to the inputs defined in Model(input=[in1, …], …)

### generator
yield [inputX_1,inputX_2],y
### model
model = Model(inputs=[inputX_1,inputX_2],outputs=...)

補充知識:學(xué)習(xí)keras時對loss函數(shù)不同的選擇,則model.fit里的outputs可以是one_hot向量,也可以是整形標(biāo)簽

我就廢話不多說了,大家還是直接看代碼吧~

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
    'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# plt.figure()
# plt.imshow(train_images[0])
# plt.colorbar()
# plt.grid(False)
# plt.show()

train_images = train_images / 255.0
test_images = test_images / 255.0

# plt.figure(figsize=(10,10))
# for i in range(25):
#  plt.subplot(5,5,i+1)
#  plt.xticks([])
#  plt.yticks([])
#  plt.grid(False)
#  plt.imshow(train_images[i], cmap=plt.cm.binary)
#  plt.xlabel(class_names[train_labels[i]])
# plt.show()

model = keras.Sequential([
 keras.layers.Flatten(input_shape=(28, 28)),
 keras.layers.Dense(128, activation='relu'),
 keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
    loss='categorical_crossentropy', 
    #loss = 'sparse_categorical_crossentropy' 則之后的label不需要變成one_hot向量,直接使用整形標(biāo)簽即可
    metrics=['accuracy'])
one_hot_train_labels = keras.utils.to_categorical(train_labels, num_classes=10)

model.fit(train_images, one_hot_train_labels, epochs=10)

one_hot_test_labels = keras.utils.to_categorical(test_labels, num_classes=10)
test_loss, test_acc = model.evaluate(test_images, one_hot_test_labels)

print('\nTest accuracy:', test_acc)

# predictions = model.predict(test_images)
# predictions[0]
# np.argmax(predictions[0])
# test_labels[0]

loss若為loss=‘categorical_crossentropy', 則fit中的第二個輸出必須是一個one_hot類型,

而若loss為loss = ‘sparse_categorical_crossentropy' 則之后的label不需要變成one_hot向量,直接使用整形標(biāo)簽即可

以上這篇淺談keras中l(wèi)oss與val_loss的關(guān)系就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》

    用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》

    這篇文章主要介紹了用Python 爬取貓眼電影數(shù)據(jù)分析《無名之輩》,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器的過程

    ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器的過程

    這篇文章主要介紹了ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Python和Matlab實現(xiàn)蝙蝠算法的示例代碼

    Python和Matlab實現(xiàn)蝙蝠算法的示例代碼

    蝙蝠算法是一種搜索全局最優(yōu)解的有效方法,本文主要介紹了Python和Matlab實現(xiàn)蝙蝠算法的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python解析并讀取PDF文件內(nèi)容的方法

    Python解析并讀取PDF文件內(nèi)容的方法

    這篇文章主要介紹了Python解析并讀取PDF文件內(nèi)容的方法,結(jié)合實例形式分別描述了Python2.7在win32與win64環(huán)境下實現(xiàn)讀取pdf的相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • django雙下劃線的具體使用

    django雙下劃線的具體使用

    雙下劃線約定通常用于執(zhí)行一些特定的查詢操作,本文主要介紹了django雙下劃線的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Python打包成exe的兩種方法

    Python打包成exe的兩種方法

    python的開發(fā)效率非常的高,但是當(dāng)我們用python寫一些小工具需要給用戶使用的時候,用戶大多數(shù)是沒有安裝python的,本文介紹兩種python的打包方案,使用pyinstaller和nuitka打包成exe,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • python的import?機制是怎么實現(xiàn)的

    python的import?機制是怎么實現(xiàn)的

    這篇文章主要介紹了python的import?機制是怎么實現(xiàn)的,import有Python運行時的全局模塊池的維護(hù)和搜索、解析與搜索模塊路徑的樹狀結(jié)構(gòu)等作用,下文具體相關(guān)介紹需要的小伙伴可以參考一下
    2022-05-05
  • python實現(xiàn)自動打卡小程序

    python實現(xiàn)自動打卡小程序

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)自動打卡小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 詳解python中的Turtle函數(shù)庫

    詳解python中的Turtle函數(shù)庫

    這篇文章主要介紹了python中的Turtle函數(shù)庫,包括函數(shù)庫的引用方式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Python類和實例的屬性機制原理詳解

    Python類和實例的屬性機制原理詳解

    這篇文章主要介紹了Python類和實例的屬性機制原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03

最新評論