东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2334|回复: 1
打印 上一主题 下一主题

[TensorFlow2.0] 06、Keras如何实现多输入和多输出_代码示例

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14435
QQ
跳转到指定楼层
楼主
发表于 2019-10-30 11:29:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
06、Keras如何实现多输入和多输出_代码示例


Wide与Deep模型实战


子类API
功能API(函数式API)
多输入与多输出


东方老师AI官网:http://www.ai111.vip
有任何问题可联系东方老师微信:dfy_88888
【微信二维码图片】

多输出:主要针对多任务学习模型(分类与回归都需要预测输出)


Multi-Input实现Demo:

  1. import numpy as np
  2. import matplotlib as mpl
  3. import matplotlib.pyplot as plt
  4. import sklearn
  5. import pandas as pd
  6. import os
  7. import sys
  8. import time
  9. import tensorflow as tf
  10. from tensorflow import keras

  11. print(sys.version_info)
  12. for module in mpl, np, pd, sklearn, tf, keras:
  13.     print(module.__name__, module.__version__)

  14. from sklearn.datasets import fetch_california_housing

  15. housing = fetch_california_housing()
  16. # print(housing.DESCR)
  17. print(housing.data.shape)
  18. print(housing.target.shape)

  19. from sklearn.model_selection import train_test_split

  20. x_train_all, x_test, y_train_all, y_test = train_test_split(
  21.     housing.data, housing.target, random_state=7)
  22. x_train, x_valid, y_train, y_valid = train_test_split(
  23.     x_train_all, y_train_all, random_state=11)
  24. print(x_train.shape, y_train.shape)
  25. print(x_valid.shape, y_valid.shape)
  26. print(x_test.shape, y_test.shape)

  27. from sklearn.preprocessing import StandardScaler

  28. scaler = StandardScaler()
  29. x_train_scaled = scaler.fit_transform(x_train)
  30. x_valid_scaled = scaler.transform(x_valid)
  31. x_test_scaled = scaler.transform(x_test)


  32. def create_model_function_multi_input():
  33.     # 第一种方法:函数式api实现wide&deep模型 像使用函数一样
  34.     print('共%d个特征,前5个特征给wide模型,后6个特征给deep模型' % x_train.shape[1])
  35.     input_wide = keras.layers.Input(shape=(5, ))
  36.     input_deep = keras.layers.Input(shape=(6, ))
  37.     # 复合函数的形式
  38.     hidden_1 = keras.layers.Dense(30, activation='relu')(input_deep)
  39.     hidden_2 = keras.layers.Dense(30, activation='relu')(hidden_1)
  40.     # wide model
  41.     concat = keras.layers.concatenate(inputs=[input_wide, hidden_2])
  42.     output = keras.layers.Dense(1)(concat)

  43.     model = keras.models.Model(inputs=[input_wide, input_deep], outputs=[output], name='wide_deep_model')
  44.     return model


  45. model = create_model_function_multi_input()

  46. model.summary()

  47. model.compile(optimizer=keras.optimizers.Adam(0.001), loss='mean_squared_error')
  48. callbacks = [
  49.     keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)
  50. ]
  51. # multi_input  a list of arrays (in case the model has multiple inputs
  52. inputs_train = [x_train_scaled[:, :5], x_train_scaled[:, -6:]]
  53. inputs_valid = [x_valid_scaled[:, :5], x_valid_scaled[:, -6:]]
  54. history = model.fit(inputs_train, y_train, validation_data=(inputs_valid, y_valid), epochs=50,
  55.                     callbacks=callbacks)
  56. print(type(history))
  57. print(history.history)


  58. def plot_learning_curve(history):
  59.     # ValueError: arrays must all be same length
  60.     # 表格型数据 要求每一列的len一致 这里即:history.history字典里每个key对应的value长度一致
  61.     df_history = pd.DataFrame(data=history.history)
  62.     print(df_history)
  63.     print(df_history.index)
  64.     print(df_history.columns)
  65.     print(df_history.dtypes)
  66.     df_history.plot(figsize=(8, 5))
  67.     plt.grid(True)
  68.     # x就是DataFrame的索引
  69.     plt.ylim(0, 1)
  70.     plt.show()


  71. plot_learning_curve(history)
  72. # Returns the loss value & metrics values for the model in test mode.
  73. inputs_test = [x_test_scaled[:, :5], x_test_scaled[:, -6:]]
  74. print(model.evaluate(inputs_test, y_test))
复制代码

Multi-Output实现Demo:

  1. import numpy as np
  2. import matplotlib as mpl
  3. import matplotlib.pyplot as plt
  4. import sklearn
  5. import pandas as pd
  6. import os
  7. import sys
  8. import time
  9. import tensorflow as tf
  10. from tensorflow import keras

  11. print(sys.version_info)
  12. for module in mpl, np, pd, sklearn, tf, keras:
  13.     print(module.__name__, module.__version__)

  14. from sklearn.datasets import fetch_california_housing

  15. housing = fetch_california_housing()
  16. # print(housing.DESCR)
  17. print(housing.data.shape)
  18. print(housing.target.shape)

  19. from sklearn.model_selection import train_test_split

  20. x_train_all, x_test, y_train_all, y_test = train_test_split(
  21.     housing.data, housing.target, random_state=7)
  22. x_train, x_valid, y_train, y_valid = train_test_split(
  23.     x_train_all, y_train_all, random_state=11)
  24. print(x_train.shape, y_train.shape)
  25. print(x_valid.shape, y_valid.shape)
  26. print(x_test.shape, y_test.shape)

  27. from sklearn.preprocessing import StandardScaler

  28. scaler = StandardScaler()
  29. x_train_scaled = scaler.fit_transform(x_train)
  30. x_valid_scaled = scaler.transform(x_valid)
  31. x_test_scaled = scaler.transform(x_test)


  32. def create_model_function_multi_input_output():
  33.     # 第一种方法:函数式api实现wide&deep模型 像使用函数一样
  34.     print('共%d个特征,前5个特征给wide模型,后6个特征给deep模型' % x_train.shape[1])
  35.     input_wide = keras.layers.Input(shape=(5, ))
  36.     input_deep = keras.layers.Input(shape=(6, ))
  37.     # 复合函数的形式
  38.     hidden_1 = keras.layers.Dense(30, activation='relu')(input_deep)
  39.     hidden_2 = keras.layers.Dense(30, activation='relu')(hidden_1)
  40.     # wide model
  41.     concat = keras.layers.concatenate(inputs=[input_wide, hidden_2])
  42.     output = keras.layers.Dense(1)(concat)
  43.     output2 = keras.layers.Dense(1)(hidden_2)

  44.     model = keras.models.Model(inputs=[input_wide, input_deep], outputs=[output, output2], name='wide_deep_model')
  45.     return model


  46. model = create_model_function_multi_input_output()

  47. model.summary()

  48. model.compile(optimizer=keras.optimizers.Adam(0.001), loss='mean_squared_error')
  49. callbacks = [
  50.     keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)
  51. ]
  52. # multi_input  a list of arrays (in case the model has multiple inputs
  53. inputs_train = [x_train_scaled[:, :5], x_train_scaled[:, -6:]]
  54. inputs_valid = [x_valid_scaled[:, :5], x_valid_scaled[:, -6:]]
  55. history = model.fit(inputs_train, [y_train, y_train], validation_data=(inputs_valid, [y_valid, y_valid]), epochs=50,
  56.                     callbacks=callbacks)
  57. print(type(history))
  58. print(history.history)
  59. print(history.history.keys())


  60. def plot_learning_curve(history):
  61.     # ValueError: arrays must all be same length
  62.     # 表格型数据 要求每一列的len一致 这里即:history.history字典里每个key对应的value长度一致
  63.     df_history = pd.DataFrame(data=history.history)
  64.     print(df_history)
  65.     print(df_history.index)
  66.     print(df_history.columns)
  67.     print(df_history.dtypes)
  68.     df_history.plot(figsize=(8, 5))
  69.     plt.grid(True)
  70.     # x就是DataFrame的索引
  71.     plt.ylim(0, 1)
  72.     plt.show()


  73. plot_learning_curve(history)
  74. # Returns the loss value & metrics values for the model in test mode.
  75. inputs_test = [x_test_scaled[:, :5], x_test_scaled[:, -6:]]
  76. print(model.evaluate(inputs_test, [y_test, y_test]))
复制代码




让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
沙发
发表于 2020-3-4 22:50:03 | 只看该作者
学习学习,正好有一个项目要做
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-27 01:15 , Processed in 0.170355 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表