我正在使用具有三層的簡單模型:
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(6,), name="flatten"),
tf.keras.layers.Dense(128, activation="relu", name="dense1"),
tf.keras.layers.Dense(1, name="dense2")
])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.MeanAbsoluteError()
)
好的,這樣編譯成功。我已經為它準備了一些資料,讓我們檢查一下:
print(features)
print(labels)
這將列印兩個串列:
[[1.0, 0.6747252747252748, 0.5652173913043478, 0.6817120622568094, 0.48387096774193544, 0.8536585365853658], [1.0, 0.7692307692307693, 0.717391304347826, 0.7184824902723735, 0.4637096774193548, 0.8536585365853658], (many more features...)]
[18.0, 15.0, (many more labels, same amount as features...)]
偉大的。現在我將訓練模型并列印損失歷史:
print(
model.fit(
features,
labels,
verbose=0,
epochs=100,
validation_data=(features, labels)
).history["val_loss"]
)
這列印:
[22.92747688293457, 22.328025817871094, (many more epochs...), 3.36980938911438, 3.3660128116607666]
太好了,培訓成功了,損失隨著時間的推移而下降。現在我想手動呼叫模型:
print(
model(
features[0]
)
)
但這抱怨:
ValueError: Layer "sequential" expects 1 input(s), but it received 6 input tensors. Inputs received: [<tf.Tensor: shape=(), dtype=float32, numpy=1.0>, <tf.Tensor: shape=(), dtype=float32, numpy=0.6747253>, <tf.Tensor: shape=(), dtype=float32, numpy=0.5652174>, <tf.Tensor: shape=(), dtype=float32, numpy=0.6817121>, <tf.Tensor: shape=(), dtype=float32, numpy=0.48387095>, <tf.Tensor: shape=(), dtype=float32, numpy=0.85365856>]
我不明白為什么我不能將它作為一個串列傳遞,因為這在.fit通話中是可以的,但是通過一些閱讀和反復試驗,我找到了解決方案tf.constant:
print(
model(
tf.constant(features[0])
)
)
但現在它遇到了另一個錯誤!
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
Input 0 of layer "dense1" is incompatible with the layer: expected axis -1 of input shape to have value 6, but received input with shape (6, 1)
Call arguments received:
? inputs=tf.Tensor(shape=(6,), dtype=float32)
? training=None
? mask=None
似乎第二層與第一層不兼容!那是什么意思?我絕對不明白的是,如果這些層不兼容,那么它是如何開始編譯的?更糟糕的是,為什么訓練成功了?顯然,如果模型編譯沒有抱怨,并且我將輸入很好地傳遞給第一層,那么第二層不可能有問題嗎?
這里出了什么問題?在我看來,這似乎不合邏輯。一定有什么我錯過了。
uj5u.com熱心網友回復:
首先,為什么要使用 Flatten 圖層?您可以取出展平層并僅使用
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation="relu", name="dense1", input_shape=(6,)),
tf.keras.layers.Dense(1, name="dense2")
])
看看我們已經作為 input_shape 傳遞(6,)了一個與(6, None). 如果我們運行model.summary(),我們會得到
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense1 (Dense) (None, 128) 896
dense2 (Dense) (None, 1) 129
=================================================================
Total params: 1,025
Trainable params: 1,025
Non-trainable params: 0
_________________________________________________________________
現在我們可以正常適應
features = [[1.0, 0.6747252747252748, 0.5652173913043478, 0.6817120622568094, 0.48387096774193544, 0.8536585365853658], [1.0, 0.7692307692307693, 0.717391304347826, 0.7184824902723735, 0.4637096774193548, 0.8536585365853658]]
labels = [18.0, 15.0]
history = model.fit(
features,
labels,
verbose=0,
epochs=100,
validation_data=(features, labels)
)
現在,如果你想做一個預測,你可以做
# Using .predict
model.predict(features)
>>> array([[15.692635], [16.437447]], dtype=float32)
model.predict([features[0]])
>>> array([[15.692635]], dtype=float32)
# Using functional way
model(np.array(features))
>>><tf.Tensor: shape=(2, 1), dtype=float32, numpy=array([[15.692635],[16.437447]],dtype=float32)>
model(np.array(features[0]).reshape(1,-1))
>>> <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[15.692635]], dtype=float32)>
This different between the .predict and using the model itself as a function must be due to different implementation on the predict method and the __call__ from the Model class.
It seems that the predict method is more flexible and might do some modifications on the input to use it to make the prediction whereas the functional method might try to use the input as it is, thus we need to pass it as a 2D array
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453218.html
上一篇:Excel或SQL垂直資料到水平
