简体   繁体   English

如何在tensorflow keras中定义model输入和输出?

[英]How to define model input and outputs in tensorflow keras?

I'm trying to create a model what should have nx8x8 input and 8x8 output or like below 64 units output, but don't know how to create it to make it work.我正在尝试创建一个 model,它应该具有 nx8x8 输入和 8x8 output 或低于 64 个单元 output,但不知道如何创建它以使其工作。 I'm trying with the below code:我正在尝试使用以下代码:

model = tf.keras.Sequential()

input = tf.keras.layers.Flatten(input_shape=(8,8), name='input')
model.add(input)

middle = tf.keras.layers.Dense(256, activation='sigmoid',  name='a')
model.add(middle)

output = tf.keras.layers.Dense(64, activation='softmax',  name='b')
model.add(output)
print(model.input_shape)

model.compile(optimizer='adam',
            loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
            metrics=['accuracy'])

train_input = np.array(
    [
        [0, 1, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 1, 0],
        [0, 1, 0, 1, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 3],
        [3, 0, 3, 0, 3, 0, 0, 0],
        [0, 3, 0, 3, 0, 3, 0, 3],
        [3, 0, 3, 0, 3, 0, 3, 0]
    ],
    [
        [0, 1, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 3],
        [3, 0, 3, 0, 3, 0, 0, 0],
        [0, 3, 0, 3, 0, 3, 0, 3],
        [3, 0, 3, 0, 3, 0, 3, 0]
    ]
)
train_output = np.array([
    0, 1, 0, 1, 0, 1, 0, 1,
    1, 0, 1, 0, 1, 0, 1, 0,
    0, 0, 0, 1, 0, 1, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 3,
    3, 0, 3, 0, 3, 0, 0, 0,
    0, 3, 0, 3, 0, 3, 0, 3,
    3, 0, 3, 0, 3, 0, 3, 0,
])
model.fit(train_input, train_output, epochs=10)

What I'm doing bad?我做错了什么? How to define input and outputs shape?如何定义输入和输出形状?

You need to add one more instance to your train_output .您需要向您的train_output添加一个实例。 You have two samples on your train_input but only one label. You need the same amount of labels as instances of input.您的train_input上有两个样本,但只有一个 label。您需要与输入实例相同数量的标签。 This solves your cardinality issue.这解决了您的基数问题。

However your data is formatted in a very strange way, I'm pretty sure you will have more issues when training.但是,您的数据格式非常奇怪,我敢肯定您在训练时会遇到更多问题。 You're doing a classification task but your labels are not enconded as they should.您正在执行分类任务,但您的标签没有按应有的方式进行处理。 You should enconde your labels into classes (eg 0,1,2) and then your model will only output a classification.你应该将你的标签编码成类(例如 0,1,2)然后你的 model 将只有 output 一个分类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()? - In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()? 如何使用单个数据集在 tensorflow keras 中训练多个输入 model - how to use a single dataset to train multiple input model in tensorflow keras 在 tensorflow keras 中使用中间 model 输出时出现 _SymbolicException - _SymbolicException when using intermediate model outputs in tensorflow keras 如何集成 2 个 keras 模型输出? - How 2 keras model outputs can be integrated? TensorFlow 数据不适用于多输入 keras model - TensorFlow Data not working with multiple input keras model 如何在Keras中强制(回归)模型输出的单调性? - How to enforce monotonicity for (regression) model outputs in Keras? 在训练TensorFlow模型(,?不是Keras模型)时,如何获取模型中间层(op)的输入输出? - During training the TensorFlow model(!!Not the Keras model), How to get the input and output of the intermediate layer(op) of the model? 如何为导出的 tensorflow 2.0 keras 模型的输入层设计/预处理特征以用于 tensorflow 服务 - How to engineer/preprocess features for the input layer of a exported tensorflow 2.0 keras model for tensorflow serving Keras多输出模型 - Keras Multiple outputs model 如何使用Keras API在TensorFlow Eager中将压差应用于RNN的输出? - How to apply dropout to the outputs of an RNN in TensorFlow Eager using the Keras API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM