我正在尝试用
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%。
我的代码是:
features = df.iloc[1:, 1:]
labels = df.iloc[1:, 0]
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'])
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
您的配置有两个问题:
tf.keras.layers.Dense(1, activation='softmax')
,将其更改为tf.keras.layers.Dense(2, activation='softmax')
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
此外,你的数据集的格式化方式以及通过 softmax 的方式,你永远不会得到像你所描述的那样的输出:
但我认为应该是这样的:[1: 0.4][2: 0.88]
在您的上下文中,概率总和始终为
1
。