最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Keras-多輸入多輸出實例(多任務)

 更新時間:2020年06月22日 09:26:27   作者:happyprince  
這篇文章主要介紹了Keras-多輸入多輸出實例(多任務),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1、模型結(jié)果設(shè)計

2、代碼

from keras import Input, Model
from keras.layers import Dense, Concatenate
import numpy as np
from keras.utils import plot_model
from numpy import random as rd

samples_n = 3000
samples_dim_01 = 2
samples_dim_02 = 2
# 樣本數(shù)據(jù)
x1 = rd.rand(samples_n, samples_dim_01)
x2 = rd.rand(samples_n, samples_dim_02)
y_1 = []
y_2 = []
y_3 = []
for x11, x22 in zip(x1, x2):
  y_1.append(np.sum(x11) + np.sum(x22))
  y_2.append(np.max([np.max(x11), np.max(x22)]))
  y_3.append(np.min([np.min(x11), np.min(x22)]))
y_1 = np.array(y_1)
y_1 = np.expand_dims(y_1, axis=1)
y_2 = np.array(y_2)
y_2 = np.expand_dims(y_2, axis=1)
y_3 = np.array(y_3)
y_3 = np.expand_dims(y_3, axis=1)

# 輸入層
inputs_01 = Input((samples_dim_01,), name='input_1')
inputs_02 = Input((samples_dim_02,), name='input_2')
# 全連接層
dense_01 = Dense(units=3, name="dense_01", activation='softmax')(inputs_01)
dense_011 = Dense(units=3, name="dense_011", activation='softmax')(dense_01)
dense_02 = Dense(units=6, name="dense_02", activation='softmax')(inputs_02)
# 加入合并層
merge = Concatenate()([dense_011, dense_02])
# 分成兩類輸出 --- 輸出01
output_01 = Dense(units=6, activation="relu", name='output01')(merge)
output_011 = Dense(units=1, activation=None, name='output011')(output_01)
# 分成兩類輸出 --- 輸出02
output_02 = Dense(units=1, activation=None, name='output02')(merge)
# 分成兩類輸出 --- 輸出03
output_03 = Dense(units=1, activation=None, name='output03')(merge)
# 構(gòu)造一個新模型
model = Model(inputs=[inputs_01, inputs_02], outputs=[output_011,
                           output_02,
                           output_03
                           ])
# 顯示模型情況
plot_model(model, show_shapes=True)
print(model.summary())
# # 編譯
# model.compile(optimizer="adam", loss='mean_squared_error', loss_weights=[1,
#                                     0.8,
#                                     0.8
#                                     ])
# # 訓練
# model.fit([x1, x2], [y_1,
#           y_2,
#           y_3
#           ], epochs=50, batch_size=32, validation_split=0.1)

# 以下的方法可靈活設(shè)置
model.compile(optimizer='adam',
       loss={'output011': 'mean_squared_error',
          'output02': 'mean_squared_error',
          'output03': 'mean_squared_error'},
       loss_weights={'output011': 1,
              'output02': 0.8,
              'output03': 0.8})
model.fit({'input_1': x1,
      'input_2': x2},
     {'output011': y_1,
      'output02': y_2,
      'output03': y_3},
     epochs=50, batch_size=32, validation_split=0.1)

# 預測
test_x1 = rd.rand(1, 2)
test_x2 = rd.rand(1, 2)
test_y = model.predict(x=[test_x1, test_x2])
# 測試
print("測試結(jié)果:")
print("test_x1:", test_x1, "test_x2:", test_x2, "y:", test_y, np.sum(test_x1) + np.sum(test_x2))

補充知識:Keras多輸出(多任務)如何設(shè)置fit_generator

在使用Keras的時候,因為需要考慮到效率問題,需要修改fit_generator來適應多輸出

# create model
model = Model(inputs=x_inp, outputs=[main_pred, aux_pred])
# complie model
model.compile(
  optimizer=optimizers.Adam(lr=learning_rate),
  loss={"main": weighted_binary_crossentropy(weights), "auxiliary":weighted_binary_crossentropy(weights)},
  loss_weights={"main": 0.5, "auxiliary": 0.5},
  metrics=[metrics.binary_accuracy],
)
# Train model
model.fit_generator(
  train_gen, epochs=num_epochs, verbose=0, shuffle=True
)

Keras官方文檔:

generator: A generator or an instance of Sequence (keras.utils.Sequence) object in order to avoid duplicate data when using multiprocessing. The output of the generator must be either

a tuple (inputs, targets)

a tuple (inputs, targets, sample_weights).

Keras設(shè)計多輸出(多任務)使用fit_generator的步驟如下:

根據(jù)官方文檔,定義一個generator或者一個class繼承Sequence

class Batch_generator(Sequence):
 """
 用于產(chǎn)生batch_1, batch_2(記住是numpy.array格式轉(zhuǎn)換)
 """
 y_batch = {'main':batch_1,'auxiliary':batch_2}
 return X_batch, y_batch

# or in another way
def batch_generator():
 """
 用于產(chǎn)生batch_1, batch_2(記住是numpy.array格式轉(zhuǎn)換)
 """
 yield X_batch, {'main': batch_1,'auxiliary':batch_2}

重要的事情說三遍(親自采坑,搜了一大圈才發(fā)現(xiàn)滴):

如果是多輸出(多任務)的時候,這里的target是字典類型

如果是多輸出(多任務)的時候,這里的target是字典類型

如果是多輸出(多任務)的時候,這里的target是字典類型

以上這篇Keras-多輸入多輸出實例(多任務)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python繪制分類圖的方法

    Python繪制分類圖的方法

    這篇文章主要為大家詳細介紹了Python繪制分類圖的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • python在地圖上畫比例的實例詳解

    python在地圖上畫比例的實例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于如何用python在地圖上畫比例的相關(guān)實例內(nèi)容,有興趣的朋友們可以學習下。
    2020-11-11
  • python的常見矩陣運算(小結(jié))

    python的常見矩陣運算(小結(jié))

    這篇文章主要介紹了python的常見矩陣運算(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Python?實現(xiàn)一個全連接的神經(jīng)網(wǎng)絡

    Python?實現(xiàn)一個全連接的神經(jīng)網(wǎng)絡

    這篇文章主要介紹了Python?實現(xiàn)一個全連接的神經(jīng)網(wǎng)絡,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • Python進行數(shù)據(jù)提取的方法總結(jié)

    Python進行數(shù)據(jù)提取的方法總結(jié)

    數(shù)據(jù)提取是分析師日常工作中經(jīng)常遇到的需求。如某個用戶的貸款金額,某個月或季度的利息總收入,某個特定時間段的貸款金額和筆數(shù),大于5000元的貸款數(shù)量等等。本篇文章介紹如何通過python按特定的維度或條件對數(shù)據(jù)進行提取,完成數(shù)據(jù)提取需求。
    2016-08-08
  • Python時間序列數(shù)據(jù)的預處理方法總結(jié)

    Python時間序列數(shù)據(jù)的預處理方法總結(jié)

    這篇文章主要介紹了Python時間序列數(shù)據(jù)的預處理方法總結(jié),時間序列數(shù)據(jù)隨處可見,要進行時間序列分析,我們必須先對數(shù)據(jù)進行預處理。時間序列預處理技術(shù)對數(shù)據(jù)建模的準確性有重大影響
    2022-07-07
  • scrapy-redis源碼分析之發(fā)送POST請求詳解

    scrapy-redis源碼分析之發(fā)送POST請求詳解

    這篇文章主要給大家介紹了關(guān)于scrapy-redis源碼分析之發(fā)送POST請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用scrapy-redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-05-05
  • 對比Python中__getattr__和 __getattribute__獲取屬性的用法

    對比Python中__getattr__和 __getattribute__獲取屬性的用法

    這篇文章主要介紹了對比Python中__getattr__和 __getattribute__獲取屬性的用法,注意二者間的區(qū)別,__getattr__只作用于不存在的屬性,需要的朋友可以參考下
    2016-06-06
  • 使用Pycharm(Python工具)新建項目及創(chuàng)建Python文件的教程

    使用Pycharm(Python工具)新建項目及創(chuàng)建Python文件的教程

    這篇文章主要介紹了使用Pycharm(Python工具)新建項目及創(chuàng)建Python文件的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Django request.META.get()獲取不到header頭的原因分析

    Django request.META.get()獲取不到header頭的原因分析

    這篇文章主要介紹了Django request.META.get()獲取不到header頭的原因分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論

谢通门县| 漳平市| 息烽县| 大姚县| 洛扎县| 镇坪县| 瑞丽市| 资阳市| 厦门市| 绥化市| 墨竹工卡县| 东方市| 桦南县| 大余县| 东明县| 麻城市| 筠连县| 来安县| 楚雄市| 航空| 双柏县| 怀宁县| 盘锦市| 湘西| 宝鸡市| 内乡县| 察哈| 天镇县| 灵石县| 双峰县| 拜泉县| 舞钢市| 江油市| 新晃| 湟中县| 乳山市| 嵊泗县| 新营市| 大姚县| 毕节市| 兴仁县|