【问题标题】:how to train model in which labels is [5,30]?如何训练标签为 [5,30] 的模型?
【发布时间】:2021-07-12 16:05:12
【问题描述】:

如何在具有形状 [5,30] 的每个标签的数据集上进行训练。例如:

[
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 54, 55, 21, 56, 57,  3,
        22, 19, 58,  6, 59,  4, 60,  1, 61, 62, 23, 63, 23, 64],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  1, 65,  7, 66,  2, 67, 68,  3, 69, 70],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0, 11, 12,  5, 13, 14,  9, 10,  5, 15, 16, 17,  2,  8],
       [ 0,  0,  0,  0,  0,  2, 71,  1, 72, 73, 74,  7, 75, 76, 77,  3,
        20, 78, 18, 79,  1, 21, 80, 81,  3, 82, 83, 84,  6, 85],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,
        86, 87,  3, 88, 89,  1, 90, 91, 22, 92, 93,  4,  6, 94]
]

一种方法是将标签重塑为 [150],但这会使 标记化的句子失去意义。请建议我如何安排 keras 层 以及哪些层能够制作模型?我希望以后能够生成句子。

我现在的模型代码是这样的。

model = tf.keras.Sequential([ feature_layer, 
layers.Dense(128, activation='relu'), 
layers.Dense(128, activation='relu'), 
layers.Dropout(.1), 
layers.Dense(5), 
layers.Dense(30, activation='softmax'), ]) 
opt = Adam(learning_rate=0.01) 
model.compile(optimizer=opt, loss='mean_absolute_percentage_error', metrics=['accuracy']) 

实际数据。

state district month rainfall max_temp min_temp max_rh min_rh wind_speed advice
Orissa Kendrapada february 0.0 34.6 19.4 88.2 29.6 12.0 chances of foot rot disease in paddy crop; apply urea at 3 weeks after transplanting at active tillering stage for paddy;......
Jharkhand Saraikela Kharsawan february 0 35.2 16.6 29.4 11.2 3.6 provide straw mulch and go for intercultural operations to avoid moisture losses from soil; chance of leaf blight disease in potato crop; .......

我需要能够生成建议。

【问题讨论】:

  • 我觉得还是加一些代码方便调试比较好,有没有准备好代码?
  • 但是根据您对目标输出的评论,您可以为目标输出创建一个简单的密集层,如 tf.keras.layers.Dense(30, activation='softmax')
  • 返回此错误。不兼容的形状:[10,5,30] 与 [10,30]
  • 我现在已经在我的问题中添加了代码。
  • 你能指定一些信息,比如你的输入是什么样的吗?使用原始格式的示例输入和目标

标签: tensorflow machine-learning keras nlp keras-layer


【解决方案1】:

如果您确实认为输出需要采用这种形状(而不是扁平化),那么最简单(在我看来也是正确的解决方案)是拥有一个多输出网络,每个输出都有一个layers.Dense(30,activation='softmax')

你会有类似的东西:

def create_model():
    base_model = .... (stacked Dense units + other) # you can even create multi-input multi-output if you really want that.
    first_output = Dense(30,activation='softmax',name='output_1')(base_model) 
    second_output = Dense(30,activation='softmax',name='output_2')(base_model)
    ...
    fifth_output = Dense(30,activation='softmax',name='output_5')(base_model)
    model = Model(inputs=input_layer,
                  outputs=[first_output,second_output,third_output,fourth_output,fifth_output])
    return model


optimizer = tf.keras.optimizers.Adam()
model.compile(optimizer=optimizer,
              loss={'output_1': 'sparse_categorical_crossentropy', 
                    'output_2': 'sparse_categorical_crossentropy',
                    'output_3': 'sparse_categorical_crossentropy',
                    'output_4': 'sparse_categorical_crossentropy',
                    'output_5': 'sparse_categorical_crossentropy'},
              metrics={'output_1':tf.keras.metrics.Accuracy(),
                       'output_2':tf.keras.metrics.Accuracy(),
                       'output_3':tf.keras.metrics.Accuracy(),
                       'output_4':tf.keras.metrics.Accuracy(),
                       'output_5':tf.keras.metrics.Accuracy()})

model.fit(X, y,
          epochs=100, batch_size=10, validation_data=(val_X, val_y))

在此,请注意 y(用于 train 和 valid)是一个长度为 5(输出数量)的 numpy 数组,每个元素的长度为 30。

再次,确保您确实需要这样的配置;我发布了答案作为 TensorFlow 和 Keras 中的多输出标签的演示,并为了其他人的利益,但我不是 100% 确定您确实需要这种确切的配置(也许您可以选择更简单的配置)。

注意sparse_categorical_crossentropy 的用法,因为您的标签不是一次性编码的(MAPE 也用于回归,而不是分类)。

【讨论】:

  • 你能举个例子base_model在代码中的样子
  • 是的。我通过一些工作得到了它,现在我也可以稍后更改图层。谢谢大佬。
  • 不客气,如果您还有任何问题,请提出,尽管 StackOverflow 规则确实规定您应该在另一篇文章中提问,而不是作为评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 2017-08-17
  • 2019-10-23
  • 1970-01-01
  • 2020-12-05
  • 2018-07-17
相关资源
最近更新 更多