gpt4 book ai didi

python - Keras 文档 : Multi-input and multi-output models Not able to follow

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:59 27 4
gpt4 key购买 nike

我正在按照页面上的示例进行操作:Multi-input and multi-output models

用于预测新闻标题将收到多少转发和点赞的模型设置。那么 ma​​in_output 正在预测有多少转发,而 aux_output 正在预测点赞数?

from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

headline_data=[[i for i in range(100)]]
additional_data=[[100,200]]
labels=[1,2]
# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.

# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)

# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32)(x)


auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)


# This defines a model with two inputs and two outputs:
model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

# We compile the model and assign a weight of 0.2 to the auxiliary loss.
# To specify different loss_weights or loss for each different output,
# you can use a list or a dictionary. Here we pass a single loss as the loss argument,
# so the same loss will be used on all outputs.

# Since our inputs and outputs are named (we passed them a "name" argument), We could also have compiled the model via:
model.compile(optimizer='rmsprop',
loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
loss_weights={'main_output': 1., 'aux_output': 0.2})

# And trained it via:
model.fit({'main_input': headline_data, 'aux_input': additional_data},
{'main_output': labels, 'aux_output': labels},
epochs=50, batch_size=32)

我收到 AttributeError 错误:“list”对象没有属性“ndim”

最佳答案

您的输入/输出必须是 NumPy 数组,其中第一个维度是批量大小。例如:

headline_data = np.random.randint(1, 10000 + 1, size=(32, 100))
additional_data = np.random.randint(1, 10000 + 1, size=(32, 5))
labels = np.random.randint(0, 1 + 1, size=(32, 1))

请注意,这是一个玩具示例,我们随机生成输入。

关于python - Keras 文档 : Multi-input and multi-output models Not able to follow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49176929/

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