gpt4 book ai didi

python - 如何在 Keras-Python 中输入二维数组?

转载 作者:行者123 更新时间:2023-11-28 18:06:37 24 4
gpt4 key购买 nike

我是机器学习和 keras 的新手,一直在尝试输入数据;我有这样的数据:

[[[0.01363717 0.        ]
[0.01577874 0. ]
[0.01463021 0. ]]

[[0.01577874 0. ]
[0.01463021 0. ]
[0.01006721 0. ]]

[[0.01463021 0. ]
[0.01006721 0. ]
[0.00762504 0. ]]...]

数据的形状是:(1607, 3, 2)。我怎样才能通过:

 [[0.01363717 0.        ]
[0.01577874 0. ]
[0.01463021 0. ]]

作为 512 个 CuDNNLSTM 单元层的输入?

这是我的整个网络:

def create_model():
model = Sequential()

model.add(CuDNNLSTM(512, input_shape=(3,2), return_sequences=True, name='inputlstm1'))
model.add(Dropout(0.2))

model.add(CuDNNLSTM(512, return_sequences=True,name='lstm2'))
model.add(Dropout(0.2))

model.add(CuDNNLSTM(512, return_sequences=True,name='lstm3'))
model.add(Dropout(0.2))

model.add(Dense(32, activation='relu', name='dense1'))
model.add(Dropout(0.2))

model.add(Dense(1, activation='softmax', name='denseoutput2'))


# Compile model
model.compile(
loss='mse',
optimizer='adam',
metrics=['accuracy'],
)
return model

及其配件:

model=create_model()
history=model.fit(xtrain, ytrain,batch_size=1, epochs=5, validation_data=(xtest, ytest), verbose=1)

最佳答案

keras层的构建包括指定传递数组的形状,这里要训练的数组形状是(3,2),有1607个样本,

input_shape = (3,2)
X = LSTM(124, activation = 'sigmoid', name='layer1', dropout = 0.4) (temp)

如果你想使用stacked LSTM,你可以使用这个

input_shape = (3,2)
X = LSTM(124, activation = 'sigmoid', name='layer1', dropout = 0.4,return_sequences=True) (temp)
X = LSTM(64, activation = 'sigmoid', name='layer2', dropout = 0.4) (X)

编辑

def create_model():
model = keras.models.Sequential()

model.add(keras.layers.CuDNNLSTM(512, input_shape=(3,2), return_sequences=True, name='inputlstm1'))
model.add(keras.layers.Dropout(0.2))

model.add(keras.layers.CuDNNLSTM(512, return_sequences=True,name='lstm2'))
model.add(keras.layers.Dropout(0.2))

# The last layer of Stacked LSTM need not to return the input sequences
model.add(keras.layers.CuDNNLSTM(512,name='lstm3'))
model.add(keras.layers.Dropout(0.2))

model.add(keras.layers.Dense(32, activation='relu', name='dense1'))
model.add(keras.layers.Dropout(0.2))

model.add(keras.layers.Dense(1, activation='softmax', name='denseoutput2'))


# Compile model
model.compile(
loss='mse',
optimizer='adam',
metrics=['accuracy'],
)
return model

关于python - 如何在 Keras-Python 中输入二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53099925/

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