[英]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.