代码之家  ›  专栏  ›  技术社区  ›  akozel

分类任务的tensorflow输出层配置

  •  0
  • akozel  · 技术社区  · 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)
    

    后果

    的结果 预测 方法总是 [[1.]] .

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

    哪里 1 and 4 classifications 0.4 and 0.88 probability

    1 回复  |  直到 1 年前
        1
  •  0
  •   Timbus Calin    1 年前

    配置中存在两个问题:

    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格式化数据集的方式,您永远不会有如您所描述的输出:

    但我认为这应该是smth类似的:[1:04][2:0.88]

    概率的总和总是 1 在您的环境中。