gpt4 book ai didi

python - Recurrentshop 和 Keras : multi-dimensional RNN results in a dimensions mismatch error

转载 作者:太空狗 更新时间:2023-10-29 20:28:56 25 4
gpt4 key购买 nike

我对 Recurrentshop 和 Keras 有疑问。我正在尝试在循环模型中使用连接和多维张量,但无论我如何安排输入、形状和 batch_shape,我都会遇到维度问题。

最少的代码:

from keras.layers import *
from keras.models import *
from recurrentshop import *
from keras.layers import Concatenate

input_shape=(128,128,3)

x_t = Input(shape=(128,128,3,))
h_tm1 = Input(shape=(128,128,3, ))

h_t1 = Concatenate()([x_t, h_tm1])
last = Conv2D(3, kernel_size=(3,3), strides=(1,1), padding='same', name='conv2')(h_t1)

# Build the RNN
rnn = RecurrentModel(input=x_t, initial_states=[h_tm1], output=last, final_states=[last], state_initializer=['zeros'])

x = Input(shape=(128,128,3, ))
y = rnn(x)

model = Model(x, y)

model.predict(np.random.random((1, 128, 128, 3)))

错误代码:

ValueError: Shape must be rank 3 but it is rank 4 for 'recurrent_model_1/concatenate_1/concat' (op:ConcatV2) with input shapes: [?,128,3], [?,128,128,3], [].

请帮忙。

最佳答案

试试这个(更改的行有注释):

from recurrentshop import *
from keras.layers import Concatenate

x_t = Input(shape=(128, 128, 3,))
h_tm1 = Input(shape=(128, 128, 3,))

h_t1 = Concatenate()([x_t, h_tm1])
last = Conv2D(3, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv2')(h_t1)

rnn = RecurrentModel(input=x_t,
initial_states=[h_tm1],
output=last,
final_states=[last],
state_initializer=['zeros'])

x = Input(shape=(1, 128, 128, 3,)) # a series of 3D tensors -> 4D
y = rnn(x)

model = Model(x, y)
model.predict(np.random.random((1, 1, 128, 128, 3))) # a batch of x -> 5D

关于python - Recurrentshop 和 Keras : multi-dimensional RNN results in a dimensions mismatch error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com