我不太確定我的問題出在哪里。嘗試運行下面的模型時,我收到一條錯誤訊息:
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 250, 319, 3), found shape=(250, 319, 3)
從 state() 回傳的影像大小為 (250, 319, 3)。我在下面的 get_actor() 模型輸入中明確定義了這一點。我嘗試過的一些事情:
我讀過的一件事是,Keras 可能需要一批影像,而不僅僅是一個影像,因此您可能需要將維度擴展到 (1, 250, 319, 3)。我嘗試使用 tf.expand_dims 執行此操作,但我只是得到另一個錯誤,說它正在預期(None、1、250、319、3)。
我嘗試的另一件事是將輸入大小簡單地更改為 (None, 250, 319, 3) 并且它似乎作業,直到它產生與 layer3 = layers.Flatten()(layer2) 關聯的另一個錯誤說:
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)
這可能是一個完全不同的問題。任何關于進行的最佳方式的建議。我不確定為什么它堅持在輸入形狀中包含 None ?
def state():
# can set to N number of captures per step
image = tf.convert_to_tensor(screen_capture())
img = tf.image.resize(image, [round(image.shape[0] / 2), round(image.shape[1] / 2)], preserve_aspect_ratio=True)
img_norm = tf.keras.utils.normalize(img, axis=0)
return img_norm
def get_actor():
# Initialize weights between -3e-3 and 3-e3
last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
# Convolutions
inputs = layers.Input(shape=(250, 319, 3))
layer1 = layers.Conv2D(32, 3, strides=2, activation="relu")(inputs)
layer2 = layers.Conv2D(64, 3, strides=4, activation="relu")(layer1)
layer3 = layers.Flatten()(layer2)
# Fully connected layers
layer4 = layers.Dense(256, activation="relu")(layer3)
layer5 = layers.Dense(256, activation="relu")(layer4)
action = layers.Dense(3, activation="tanh", kernel_initializer=last_init)(layer5)
outputs = action
model = tf.keras.Model(inputs, outputs)
return model
actor_model = get_actor()
sampled_actions = tf.squeeze(actor_model(state()))
uj5u.com熱心網友回復:
運行您的代碼tf.expand_dims似乎沒有任何問題,因此錯誤必須在其他地方:
import tensorflow as tf
def state():
return tf.expand_dims(tf.keras.utils.normalize(tf.random.normal((250, 319, 3))), axis=0)
def get_actor():
# Initialize weights between -3e-3 and 3-e3
last_init = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
# Convolutions
inputs = tf.keras.layers.Input(shape=(250, 319, 3))
layer1 = tf.keras.layers.Conv2D(32, 3, strides=2, activation="relu")(inputs)
layer2 = tf.keras.layers.Conv2D(64, 3, strides=4, activation="relu")(layer1)
layer3 = tf.keras.layers.Flatten()(layer2)
# Fully connected layers
layer4 = tf.keras.layers.Dense(256, activation="relu")(layer3)
layer5 = tf.keras.layers.Dense(256, activation="relu")(layer4)
action = tf.keras.layers.Dense(3, activation="tanh", kernel_initializer=last_init)(layer5)
outputs = action
model = tf.keras.Model(inputs, outputs)
return model
actor_model = get_actor()
sampled_actions = tf.squeeze(actor_model(state()))
print(sampled_actions)
tf.Tensor([ 0.00166097 0.00444322 -0.00574574], shape=(3,), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417928.html
標籤:
