分类任务的张量流输出层配置

问题描述 投票:0回答:1

我正在尝试用

google colab
tensorflow
来教我的第一个人工智能。

.predict
我不清楚工作方法。


我有以下

dataset
示例:

分类 输入1 输入2
1 0.1 0.22
1 0.333 0.4
4 0.55 0.6

预期课程为

1
4
。数据集包含每个的 50%。

我的代码是:

  1. 切片数据:
  features = df.iloc[1:, 1:]
  labels =  df.iloc[1:, 0]
  1. 构建模型:
model = tf.keras.Sequential([
    tf.keras.layers.Dense(256, activation='tanh', input_shape=(82,)),
    tf.keras.layers.Dense(2, input_shape=(256,), activation='tanh'),
    tf.keras.layers.Dense(1, activation='softmax')
])

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  1. 火车型号:
model.fit(x=features, y=labels, shuffle=True, epochs=1)

结果

.predict
方法的结果始终是
[[1.]]

但我认为应该是这样的:

[1: 0.4][2: 0.88]

其中:

1 and 4
classifications
0.4 and 0.88
probability

tensorflow keras deep-learning neural-network tensorflow2.0
1个回答
0
投票

您的配置有两个问题:

  1. tf.keras.layers.Dense(1, activation='softmax')
    ,将其更改为
    tf.keras.layers.Dense(2, activation='softmax')
  2. 由于您的标签采用整数格式,因此您需要将损失函数更改为
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

此外,你的数据集的格式化方式以及通过 softmax 的方式,你永远不会得到像你所描述的那样的输出:

但我认为应该是这样的:[1: 0.4][2: 0.88]

在您的上下文中,概率总和始终为

1

© www.soinside.com 2019 - 2024. All rights reserved.