iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
4
AI & Data

輕鬆掌握 Keras 及相關應用系列 第 6

Day 06:Keras 模型結構

前言

從這篇起,我們就Keras各部分的功能進行研究,包括:

  1. 模型結構
  2. 執行工作記錄(Callback 及 TensorBoard)
  3. Dataset
  4. Estimator
  5. TF Serving 等等。

模型結構

Keras 的模型建立分兩種:

  1. Sequential model:一層層順序執行的簡單模型,只有第一層要寫input的規格,其他層的input就是上一層的output。
  2. Functional API model:可以有多個 input 層或 output 層,結構可以有分叉,適合複雜的模型建立。

以下分別說明 Sequential/Functional API model 語法,檔案名稱為 06_01_Model.ipynb。

Sequential model

建立Sequential model的程式片段如下:

# 建立模型
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

使用下列程式碼,可繪製出結構圖。

# 需安裝 graphviz (https://www.graphviz.org/download/)
# 將安裝路徑 C:\Program Files (x86)\Graphviz2.38\bin 新增至環境變數 path 中
# pip install graphviz
# pip install pydotplus
tf.keras.utils.plot_model(model, to_file='model.png')

https://ithelp.ithome.com.tw/upload/images/20200905/20001976ciIyQFtbgv.png

Functional API model

建立Functional API model的程式片段如下:

# 建立模型
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

InputTensor = tf.keras.Input(shape=(100,))
H1 = Dense(10, activation='relu')(InputTensor)
H2 = Dense(20, activation='relu')(H1)
Output = Dense(1, activation='softmax')(H2)

model_API = Model(inputs=InputTensor, outputs=Output)

model_API.summary()

from keras.utils import plot_model
tf.keras.utils.plot_model(model_API, to_file='Functional_API_model.png')
])

繪製出結構圖如下:
https://ithelp.ithome.com.tw/upload/images/20200905/20001976c6rDybZ1UL.png

舉例說明,以下表示 H1 layer 接在 InputTensor layer 後面,H2 layer 接在 H1 layer 後面。

H1 = Dense(10, activation='relu')(InputTensor)
H2 = Dense(20, activation='relu')(H1)

之後,必須指定Model的input/output,模型建立才算完成,如下:

model_API = Model(inputs=InputTensor, outputs=Output)

有分叉的 Functional API model

看一個LSTM的模型,它具有3個Input及2個Output,中間2個LSTM與1個Input合併():

from tensorflow.keras import layers

num_tags = 12  # Number of unique issue tags
num_words = 10000  # Size of vocabulary obtained when preprocessing text data
num_departments = 4  # Number of departments for predictions

title_input = keras.Input(
    shape=(None,), name="title"
)  # Variable-length sequence of ints
body_input = keras.Input(shape=(None,), name="body")  # Variable-length sequence of ints
tags_input = keras.Input(
    shape=(num_tags,), name="tags"
)  # Binary vectors of size `num_tags`

# Embed each word in the title into a 64-dimensional vector
title_features = layers.Embedding(num_words, 64)(title_input)
# Embed each word in the text into a 64-dimensional vector
body_features = layers.Embedding(num_words, 64)(body_input)

# Reduce sequence of embedded words in the title into a single 128-dimensional vector
title_features = layers.LSTM(128)(title_features)
# Reduce sequence of embedded words in the body into a single 32-dimensional vector
body_features = layers.LSTM(32)(body_features)

# Merge all available features into a single large vector via concatenation
x = layers.concatenate([title_features, body_features, tags_input])

# Stick a logistic regression for priority prediction on top of the features
priority_pred = layers.Dense(1, name="priority")(x)
# Stick a department classifier on top of the features
department_pred = layers.Dense(num_departments, name="department")(x)

# Instantiate an end-to-end model predicting both priority and department
model = keras.Model(
    inputs=[title_input, body_input, tags_input],
    outputs=[priority_pred, department_pred],
)

# show_shapes=True:Layer 含 Input/Output 資訊
tf.keras.utils.plot_model(model, "multi_input_and_output_model.png", show_shapes=True)

繪製出結構圖如下:
https://ithelp.ithome.com.tw/upload/images/20200906/20001976DIdAs4GS4t.png

下列指令合併3個layers,他們的output維度大小分別為 128/32/12,故合併後output=128+32+12=172。

layers.concatenate([title_features, body_features, tags_input])

下列指令Model的input/output有3個Input及2個Output:

model = keras.Model(
    inputs=[title_input, body_input, tags_input],
    outputs=[priority_pred, department_pred],
)

結論

總而言之,如果是一層層順序執行的簡單模型,就使用Sequential model,反之,需要彈性就使用Functional API model就對了。

本篇範例包括06_01_Model.ipynb,可自【這裡】下載。


上一篇
Day 05:神經網路的效能調校(續)
下一篇
Day 07:Keras Callback 的使用
系列文
輕鬆掌握 Keras 及相關應用30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
sunnyyao37
iT邦新手 5 級 ‧ 2022-12-23 08:22:59

因為我是新手,請問Functional API model若要完成
輸入為xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
輸出為ys = np.array([-5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)
進行輸入為3.0時的預測程式,請問程式要如何改寫?謝謝!!!
https://github.com/mc6666/Keras_tutorial/blob/master/06_01_Model.ipynb

from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

InputTensor = tf.keras.Input(shape=(100,))
H1 = Dense(10, activation='relu')(InputTensor)
H2 = Dense(20, activation='relu')(H1)
Output = Dense(1, activation='softmax')(H2)

model_API = Model(inputs=InputTensor, outputs=Output)

model_API.summary()

from keras.utils import plot_model
tf.keras.utils.plot_model(model_API, to_file='Functional_API_model.png')

單純的X、Y,模型寫一層即可,當然也可寫多層試試看。

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

# 一層
InputTensor = tf.keras.Input(shape=(1,))
Output = Dense(1)(InputTensor)

model = Model(inputs=InputTensor, outputs=Output)

訓練:

import numpy as np
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
y = np.array([-5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,
              optimizer=tf.keras.optimizers.SGD())

model.fit(x, y, epochs=100)

預測 x=3:

model.predict(np.array([3]))

輸出 7,無誤。

感謝您的回覆,我程式整理如下:
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

一層

InputTensor = tf.keras.Input(shape=(1,))
Output = Dense(1)(InputTensor)

model = Model(inputs=InputTensor, outputs=Output)

訓練:

import numpy as np
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
y = np.array([-5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD())

model.fit(x, y, epochs=300)
model.predict(np.array([3]))
print("model.predict(np.array([3]))=",model.predict(np.array([3])))

執行結果如下:

Epoch 1/300
1/1 [==============================] - 0s 222ms/step - loss: 90.0044
Epoch 2/300
1/1 [==============================] - 0s 10ms/step - loss: 63.0783
.
.
.
.
.
Epoch 297/300
1/1 [==============================] - 0s 968us/step - loss: 0.0150
Epoch 298/300
1/1 [==============================] - 0s 2ms/step - loss: 0.0148
Epoch 299/300
1/1 [==============================] - 0s 1ms/step - loss: 0.0145
Epoch 300/300
1/1 [==============================] - 0s 998us/step - loss: 0.0142
1/1 [==============================] - 0s 58ms/step
1/1 [==============================] - 0s 15ms/step

model.predict(np.array([3]))= [[7.0338926]]

預測結果的確為接近7,非常感謝前輩的指導!!!

0
sunnyyao37
iT邦新手 5 級 ‧ 2022-12-23 15:54:40

前輩您好:
如下圖所示https://ithelp.ithome.com.tw/upload/images/20221223/201562328MFeLDw0ty.jpg

我試著以上圖撰寫有2個輸入,2個輸出的Functional API Multi Input and Output程式,程式碼如下:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model

#Functional API model多輸入多輸出模型(Multi Input and Output Model): 2022/12/23
x1 = tf.keras.Input(shape=(3,))
x2 = tf.keras.Input(shape=(3,))
x3 = tf.keras.Input(shape=(3,))
Z1 = Dense(4, use_bias=True, bias_initializer=1)(x1, x2, x3)
Z2 = Dense(4, use_bias=True, bias_initializer=1)(x1, x2, x3)
O1 = Dense(3, use_bias=True, bias_initializer=1)(Z1, Z2)
O2 = Dense(3, use_bias=True, bias_initializer=1)(Z1, Z2)
model = Model(inputs=(x1, x2, x3), outputs=(O1, O2))

#訓練
x1 =np.array([0.3], dtype=float)
x2 =np.array([0.5], dtype=float)
O1 = np.array([0.753], dtype=float)
O2 = np.array([1.150], dtype=float)
model.compile(loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD())

model.fit(x, y, epochs=100)
model.predict(np.array([0.3]))
print("model.predict(np.array([0.3]))=",model.predict(np.array([0.3])))

但發現上述程式並無法正常執行,請教應如何修改才正確? 感激不盡!!!

看更多先前的回應...收起先前的回應...
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.models import Model

#Functional API model多輸入多輸出模型(Multi Input and Output Model): 2022/12/23
x1 = tf.keras.Input(shape=(3,))
x2 = tf.keras.Input(shape=(3,))
x3 = tf.keras.Input(shape=(3,))
x = concatenate((x1, x2, x3))
Z1 = Dense(4, use_bias=True)(x)

Z2 = Dense(4, use_bias=True)(x)
Z = concatenate((Z1, Z2))
O1 = Dense(3, use_bias=True)(Z)
O2 = Dense(3, use_bias=True)(Z)
model = Model(inputs=(x1, x2, x3), outputs=(O1, O2))

#訓練
x1 =np.array([0.3, 0.2, 0.1], dtype=float).reshape(1, -1)
x2 =np.array([0.5, 0.6, 0.7], dtype=float).reshape(1, -1)
x3 =np.array([0.5, 0.6, 0.7], dtype=float).reshape(1, -1)
y =np.array([0.5], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD())

model.fit([x1, x2, x3], y, epochs=10)

x1 =np.array([0.3, 0.2, 0.1], dtype=float).reshape(1, -1)
x2 =np.array([0.5, 0.6, 0.7], dtype=float).reshape(1, -1)
x3 =np.array([0.5, 0.6, 0.7], dtype=float).reshape(1, -1)

model.predict((x1, x2, x3))

建議參閱Keras文件

感謝前輩的指導!

前輩您好:
如下圖所示,我有11筆資料,每筆資料有3個輸入值及1個輸出結果,訓練完後將有3個待輸入準備進行結果預測的值(-1.63,-0.65,1.26):
https://ithelp.ithome.com.tw/upload/images/20221229/20156232EQG866FL5q.jpg

依上圖我程式撰寫如下:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.models import Model

#Functional API model多輸入多輸出模型(Multi Input and Output Model): 2022/12/29

D_2022_12_1 = tf.keras.Input(shape=(3,), name='input_D1')
D_2022_12_2 = tf.keras.Input(shape=(3,), name='input_D2')
D_2022_12_3 = tf.keras.Input(shape=(3,), name='input_D3')
D_2022_12_4 = tf.keras.Input(shape=(3,), name='input_D4')
D_2022_12_5 = tf.keras.Input(shape=(3,), name='input_D5')
D_2022_12_6 = tf.keras.Input(shape=(3,), name='input_D6')
D_2022_12_7 = tf.keras.Input(shape=(3,), name='input_D7')
D_2022_12_8 = tf.keras.Input(shape=(3,), name='input_D8')
D_2022_12_9 = tf.keras.Input(shape=(3,), name='input_D9')
D_2022_12_10 = tf.keras.Input(shape=(3,), name='input_D10')
D_2022_12_11 = tf.keras.Input(shape=(3,), name='input_D11')

D = concatenate((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11), name='concatenate1')

a1 = Dense(4, use_bias=True, activation=None, name='hidden1_a1')(D)

Output1 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)

model = Model(inputs=(D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11), outputs=(Output1)) # TensorFlow 模型建立

#----------------TensorFlow 模型訓練-----------------------
D_2022_12_1 = np.array(([-1.45, -0.93, -0.93]), dtype=float).reshape(1, -1)
D_2022_12_2 = np.array(([0.17, 0.52, 0.77]), dtype=float).reshape(1, -1)
D_2022_12_3 = np.array(([1.20, 0.86, 1.18]), dtype=float).reshape(1, -1)
D_2022_12_4 = np.array(([-1.03, 1.55, 0.36]), dtype=float).reshape(1, -1)
D_2022_12_5 = np.array(([-0.51, -0.58, -0.53]), dtype=float).reshape(1, -1)
D_2022_12_6 = np.array(([-1.37, -0.31, -0.20]), dtype=float).reshape(1, -1)
D_2022_12_7 = np.array(([1.54, 0.03, 0.20]), dtype=float).reshape(1, -1)
D_2022_12_8 = np.array(([0.51, -1.20, -1.26]), dtype=float).reshape(1, -1)
D_2022_12_9 = np.array(([-0.26, -1.48, -1.58]), dtype=float).reshape(1, -1)
D_2022_12_10 = np.array(([0.77, -0.52, -0.45]), dtype=float).reshape(1, -1)
D_2022_12_11 = np.array(([-1.03, 1.13, 1.50]), dtype=float).reshape(1, -1)

Output1 = np.array([0], dtype=float)
Output2 = np.array([0], dtype=float)
Output3 = np.array([0], dtype=float)
Output4 = np.array([1], dtype=float)
Output5 = np.array([1], dtype=float)
Output6 = np.array([1], dtype=float)
Output7 = np.array([0], dtype=float)
Output8 = np.array([0], dtype=float)
Output9 = np.array([0], dtype=float)
Output10 = np.array([0], dtype=float)
Output11 = np.array([0], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,optimizer=tf.keras.optimizers.SGD())

model.fit([D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11], [Output1], epochs=300)
#----------------TensorFlow 模型訓練-----------------
model.summary()

from keras.utils import plot_model

tf.keras.utils.plot_model(model, to_file='Functional_API_model.png') #繪製出模型結構圖

model.predict((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11)) #輸入测試數據,輸出預測结果

print("輸入D_2022_12_1=", D_2022_12_1) #列印輸入值
print("輸入D_2022_12_2=", D_2022_12_2) #列印輸入值
print("輸入D_2022_12_3=", D_2022_12_3) #列印輸入值
print("輸入D_2022_12_4=", D_2022_12_4) #列印輸入值
print("輸入D_2022_12_5=", D_2022_12_5) #列印輸入值
print("輸入D_2022_12_6=", D_2022_12_6) #列印輸入值
print("輸入D_2022_12_7=", D_2022_12_7) #列印輸入值
print("輸入D_2022_12_8=", D_2022_12_8) #列印輸入值
print("輸入D_2022_12_9=", D_2022_12_9) #列印輸入值
print("輸入D_2022_12_10=", D_2022_12_10) #列印輸入值
print("輸入D_2022_12_11=", D_2022_12_11) #列印輸入值

print("Output1理論值=", Output1) #列印輸入值Output1
print("Output2理論值=", Output2) #列印輸入值Output2
print("Output3理論值=", Output3) #列印輸入值Output3
print("Output4理論值=", Output4) #列印輸入值Output4
print("Output5理論值=", Output5) #列印輸入值Output5
print("Output6理論值=", Output6) #列印輸入值Output6
print("Output7理論值=", Output7) #列印輸入值Output7
print("Output8理論值=", Output8) #列印輸入值Output8
print("Output9理論值=", Output9) #列印輸入值Output9
print("Output10理論值=", Output10) #列印輸入值Output10
print("Output11理論值=", Output11) #列印輸入值Output11

print("Output預測值=", model.predict((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11))) #列印預測结果

model.predict(np.array([-1.63, -0.65, 1.26]))

但輸出下列錯誤訊息:
ValueError: Layer "model" expects 11 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>]

請教程式應如何修改才合理? 感謝您!!!

因為尚不知道以下圖所示的問題其輸入參數與輸出結果在程式上因該如何撰寫安排才正確,故我把輸入值改改另一種方式撰寫程式,程式碼如下
!
https://ithelp.ithome.com.tw/upload/images/20221230/20156232qwpr9igImJ.jpg
https://ithelp.ithome.com.tw/upload/images/20221230/20156232jQlR4q1O7a.jpg
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.models import Model

x1 = tf.keras.Input(shape=(1,), name='input_x1')
x2 = tf.keras.Input(shape=(1,), name='input_x2')
x3 = tf.keras.Input(shape=(1,), name='input_x3')

x = concatenate((x1, x2, x3), name='concatenate1') # 在送往隱藏層前,將x1及x2透過concatenate函式來連結

a1 = Dense(100, use_bias=True, activation=None, name='hidden1_a1')(x) #a1隱藏層
a2 = Dense(100, use_bias=True, activation=None, name='hidden1_a2')(x) #a2隱藏層

a = concatenate((a1, a2), name='concatenate2') # 在送往輸出層前,將a1及a2透過concatenate函式來連結
Output1 = Dense(1, use_bias=True,activation=None, name='Output_Output1')(a)
model = Model(inputs=(x1, x2, x3), outputs=(Output1)) # TensorFlow 模型建立

#----------------TensorFlow 模型訓練-----------------------
x1 = np.array(([-1.45, 0.17, 1.20, -1.03, -0.51, -1.37, 1.54, 0.51, -0.26, 0.77, -1.03]), dtype=float).reshape(11, -1)
x2 = np.array(([-0.93, 0.52, 0.86, 1.55, -0.58, -0.31, 0.03, -1.20, -1.48, -0.52, 1.13]), dtype=float).reshape(11, -1)
x3 = np.array(([-0.93, 0.77, 1.18, 0.36, -0.53, -0.20, 0.20, -1.26, -1.58, -0.45, 1.50]), dtype=float).reshape(11, -1)

Output1 = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD())
model.fit([x1, x2, x3], [Output1], epochs=300)
#----------------TensorFlow 模型訓練-----------------------

model.summary()
from keras.utils import plot_model
tf.keras.utils.plot_model(model, to_file='Functional_API_model.png') #繪製出模型結構圖
model.predict((x1, x2, x3)) #輸入测試數據,輸出預測结果

print("輸入x1=", x1) #列印輸入值
print("輸入x2=", x2) #列印輸入值
print("輸入x3=", x3) #列印輸入值
print("Output1理論值=", Output1) #列印輸入值Output1
print("Output1預測值=", model.predict((x1, x2, x3)))#列印預測结果
但程式執行結果如下圖所示,理論輸出值與實際輸出值差異很大,目前還不曉得問題出在哪裡?可以請前輩指導一下嗎? 感謝!!!
https://ithelp.ithome.com.tw/upload/images/20221230/20156232Velcz42Ky1.jpg
https://ithelp.ithome.com.tw/upload/images/20221230/20156232jN39if5DDN.jpg

但如下圖所示,理論輸出值與實際輸出值差異很大,目前還不曉得問題出在哪裡?可以請前輩指導一下嗎? 感謝!!!
https://ithelp.ithome.com.tw/upload/images/20221230/20156232Velcz42Ky1.jpg

最後一行改為:

X = []
for i in range(11):
    X.append(np.array([-1.63, -0.65, 1.26]).reshape(1, -1))
    
model.predict(X)

以上只是作個示範,實際上要把表格資料帶入。

前輩您好,我依照您的指導在原本的程式中加入如下的程式段:

#-------驗證重新載入的模型其輸出結果,Output1預測值=----------
X = []
for i in range(11):
X.append(np.array([-1.63, -0.65, 1.26]).reshape(1, -1))
model.predict(X)
print("驗證重新載入的模型,輸入為[-1.63, -0.65, 1.26]其輸出預測結果,Output1預測值=", model.predict(X)) #列印預測结果

X = []
for i in range(11):
X.append(np.array([-1.45, -0.93, -0.93]).reshape(1, -1))
model.predict(X)
print("驗證重新載入的模型,輸入為[-1.45, -0.93, -0.93]其輸出預測結果,Output1預測值=", model.predict(X)) #列印預測结果
#------驗證重新載入的模型其輸出結果,Output1預測值=-----------

執行輸出結果如下:
https://ithelp.ithome.com.tw/upload/images/20221230/20156232AGRlKzYrKu.jpg

輸入為已知的[-1.45, -0.93, -0.93]時,其輸出預測並非預期的0結果(參考下圖),請教修改方向,謝謝!!!
https://ithelp.ithome.com.tw/upload/images/20221230/20156232z8aXs3Z9RN.jpg

以上討論之完整程式碼整理如下:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.models import Model

#Functional API model多輸入多輸出模型: 2022/12/30
D_2022_12_1 = tf.keras.Input(shape=(3,), name='input_D1')
D_2022_12_2 = tf.keras.Input(shape=(3,), name='input_D2')
D_2022_12_3 = tf.keras.Input(shape=(3,), name='input_D3')
D_2022_12_4 = tf.keras.Input(shape=(3,), name='input_D4')
D_2022_12_5 = tf.keras.Input(shape=(3,), name='input_D5')
D_2022_12_6 = tf.keras.Input(shape=(3,), name='input_D6')
D_2022_12_7 = tf.keras.Input(shape=(3,), name='input_D7')
D_2022_12_8 = tf.keras.Input(shape=(3,), name='input_D8')
D_2022_12_9 = tf.keras.Input(shape=(3,), name='input_D9')
D_2022_12_10 = tf.keras.Input(shape=(3,), name='input_D10')
D_2022_12_11 = tf.keras.Input(shape=(3,), name='input_D11')

D = concatenate((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11), name='concatenate1')

a1 = Dense(4, use_bias=True, activation=None, name='hidden1_a1')(D)
a2 = Dense(4, use_bias=True, activation=None, name='hidden1_a2')(D)

a = concatenate((a1, a2), name='concatenate2')

Output1 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output2 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output3 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output4 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output5 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output6 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output7 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output8 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output9 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output10 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)
Output11 = Dense(1, use_bias=True, activation=None, name='Output_Output1')(a)

model = Model(inputs=(D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11), outputs=(Output1)) # TensorFlow 模型建立

#--------TensorFlow 模型訓練-----------------------------
D_2022_12_1 = np.array(([-1.45, -0.93, -0.93]), dtype=float).reshape(1, -1)
D_2022_12_2 = np.array(([0.17, 0.52, 0.77]), dtype=float).reshape(1, -1)
D_2022_12_3 = np.array(([1.20, 0.86, 1.18]), dtype=float).reshape(1, -1)
D_2022_12_4 = np.array(([-1.03, 1.55, 0.36]), dtype=float).reshape(1, -1)
D_2022_12_5 = np.array(([-0.51, -0.58, -0.53]), dtype=float).reshape(1, -1)
D_2022_12_6 = np.array(([-1.37, -0.31, -0.20]), dtype=float).reshape(1, -1)
D_2022_12_7 = np.array(([1.54, 0.03, 0.20]), dtype=float).reshape(1, -1)
D_2022_12_8 = np.array(([0.51, -1.20, -1.26]), dtype=float).reshape(1, -1)
D_2022_12_9 = np.array(([-0.26, -1.48, -1.58]), dtype=float).reshape(1, -1)
D_2022_12_10 = np.array(([0.77, -0.52, -0.45]), dtype=float).reshape(1, -1)
D_2022_12_11 = np.array(([-1.03, 1.13, 1.50]), dtype=float).reshape(1, -1)

Output1 = np.array([0], dtype=float)
Output2 = np.array([0], dtype=float)
Output3 = np.array([0], dtype=float)
Output4 = np.array([1], dtype=float)
Output5 = np.array([1], dtype=float)
Output6 = np.array([1], dtype=float)
Output7 = np.array([0], dtype=float)
Output8 = np.array([0], dtype=float)
Output9 = np.array([0], dtype=float)
Output10 = np.array([0], dtype=float)
Output11 = np.array([0], dtype=float)

model.compile(loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD()) # TensorFlow 模型訓練(在配置訓練方法時,告知訓練時用的優化器、損失函數和準確率評測標準)

model.fit([D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11], [Output1], epochs=300) # 開始進行模型訓練,epochs=訓練週期
#-------------------TensorFlow 模型訓練--------------------

model.summary()

from keras.utils import plot_model

tf.keras.utils.plot_model(model, to_file='Functional_API_model.png') #繪製出模型結構圖

model.predict((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11)) #輸入测試數據,輸出預測结果

print("輸入D_2022_12_1=", D_2022_12_1) #列印輸入值
print("輸入D_2022_12_2=", D_2022_12_2) #列印輸入值
print("輸入D_2022_12_3=", D_2022_12_3) #列印輸入值
print("輸入D_2022_12_4=", D_2022_12_4) #列印輸入值
print("輸入D_2022_12_5=", D_2022_12_5) #列印輸入值
print("輸入D_2022_12_6=", D_2022_12_6) #列印輸入值
print("輸入D_2022_12_7=", D_2022_12_7) #列印輸入值
print("輸入D_2022_12_8=", D_2022_12_8) #列印輸入值
print("輸入D_2022_12_9=", D_2022_12_9) #列印輸入值
print("輸入D_2022_12_10=", D_2022_12_10) #列印輸入值
print("輸入D_2022_12_11=", D_2022_12_11) #列印輸入值

print("Output1理論值=", Output1) #列印輸入值Output1
print("Output2理論值=", Output2) #列印輸入值Output2
print("Output3理論值=", Output3) #列印輸入值Output3
print("Output4理論值=", Output4) #列印輸入值Output4
print("Output5理論值=", Output5) #列印輸入值Output5
print("Output6理論值=", Output6) #列印輸入值Output6
print("Output7理論值=", Output7) #列印輸入值Output7
print("Output8理論值=", Output8) #列印輸入值Output8
print("Output9理論值=", Output9) #列印輸入值Output9
print("Output10理論值=", Output10) #列印輸入值Output10
print("Output11理論值=", Output11) #列印輸入值Output11

print("Output1預測值=", model.predict((D_2022_12_1, D_2022_12_2, D_2022_12_3, D_2022_12_4, D_2022_12_5, D_2022_12_6, D_2022_12_7, D_2022_12_8, D_2022_12_9, D_2022_12_10, D_2022_12_11))) #列印預測结果

#-----驗證重新載入的模型其輸出結果,Output1預測值=------------
X = []
for i in range(11):
X.append(np.array([-1.63, -0.65, 1.26]).reshape(1, -1))
model.predict(X)
print("驗證重新載入的模型,輸入為[-1.63, -0.65, 1.26]其輸出預測結果,Output1預測值=", model.predict(X)) #列印預測结果

X = []
for i in range(11):
X.append(np.array([-1.45, -0.93, -0.93]).reshape(1, -1))
model.predict(X)
print("驗證重新載入的模型,輸入為[-1.45, -0.93, -0.93]其輸出預測結果,Output1預測值=", model.predict(X)) #列印預測结果
#------驗證重新載入的模型其輸出結果,Output1預測值=-----------

模型預測的input需要(11,3)個值,不是3個。

0
sunnyyao37
iT邦新手 5 級 ‧ 2023-01-11 12:55:33

老師您好,請教如下圖的問題,應該用Functional API或Sequential Model來處理比較合適呢?https://ithelp.ithome.com.tw/upload/images/20230111/20156232RqcN7eOaC2.jpg

題目為:
當第0筆X輸入為0,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,1這20個值時,輸出Y為1,0,0,0
當第1筆X輸入為0,1,1,0,1,0,0,1,1,1,1,0,1,0,0,0,1,0,0,0這20個值時,輸出Y為0,1,0,0
當第2筆X輸入為1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1這20個值時,輸出Y為0,0,1,0
當第3筆X輸入為0,1,1,1,1,0,0,0,1,1,1,0,1,0,0,0,1,1,1,1這20個值時,輸出Y為0,0,0,1
若以上述之輸入與輸出進行訓練,目前程式不知如何下手,請教此程式應該要如何撰寫?感謝前輩!!!

看更多先前的回應...收起先前的回應...

目前我先以初淺的想法將程式撰寫如下:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.models import Model

#Functional API model:
X = tf.keras.Input(shape=(20,), name='input_X')
a1 = Dense(100, use_bias=True, activation='sigmoid', name='hidden1_a1')(X)
a2 = Dense(100, use_bias=True, activation='sigmoid', name='hidden1_a2')(X)
a3 = Dense(100, use_bias=True, activation='sigmoid', name='hidden1_a3')(X)
a = concatenate((a1, a2, a3), name='concatenate2')
Output1 = Dense(1, use_bias=True,activation='sigmoid', name='Output_Output1')(a)
model = Model(inputs=(X), outputs=(Output1))

#------------TensorFlow 模型訓練--------------
X=np.array(([0,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,1],[0,1,1,0,1,0,0,1,1,1,1,0,1,0,0,0,1,0,0,0],[1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1],[0,1,1,1,1,0,0,0,1,1,1,0,1,0,0,0,1,1,1,1]), dtype=int).reshape(4, -1)
Output1 = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], dtype=int)
model.compile(loss=tf.keras.losses.mean_squared_error, optimizer=tf.keras.optimizers.SGD())
model.fit([X], [Output1], epochs=300)
#----------TensorFlow 模型訓練-----------------

model.summary()
from keras.utils import plot_model
tf.keras.utils.plot_model(model, to_file='Functional_API_model.png')
model.predict(X)
print("輸入X=", X) #列印輸入值
print("Output1理論值=", Output1)
print("Output1預測值=", model.predict(X))
https://ithelp.ithome.com.tw/upload/images/20230111/20156232iiTDj3a2aR.jpg
但輸出的結果並非預期,請教應該如何修改?謝謝!!!
希望程式完成的目標為:
當第0筆X輸入為0,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,1這20個值時,輸出Y為1,0,0,0
當第1筆X輸入為0,1,1,0,1,0,0,1,1,1,1,0,1,0,0,0,1,0,0,0這20個值時,輸出Y為0,1,0,0
當第2筆X輸入為1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1這20個值時,輸出Y為0,0,1,0
當第3筆X輸入為0,1,1,1,1,0,0,0,1,1,1,0,1,0,0,0,1,1,1,1這20個值時,輸出Y為0,0,0,1

這樣的題目到底算單輸入單輸出?還是多輸入多輸出?還是其他形式?謝謝!!!

多輸出(Multi-output)是指多個Y,與筆數無關,你想作的是應該是【單輸入、單輸出】即可。

老師您好:感謝解開我的疑問,謝謝!!!

我要留言

立即登入留言