英文:
How to make a prediction using karas TensorFlow?
问题
我编写了这个机器学习算法,但它返回给我一个奇怪的数组。我想输入两个数字,然后将这些数字分类为在Y中找到的相似结果,我如何使用这个模型进行预测?
import numpy as np # 多变量分类
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
X = np.array(
[[3, 7],
[3, 6],
[3, 7.2],
[6, 8],
[7, 7.5],
[7.9, 7.5]]
)
Y = np.array([1, 1, 1, 2, 3, 3])
model = Sequential([
Dense(units=25, activation="relu"),
Dense(units=15, activation="relu"),
Dense(units=10, activation="softmax"),
])
from keras.losses import SparseCategoricalCrossentropy
model.compile(loss=SparseCategoricalCrossentropy())
model.fit(X, Y, epochs=100)
我尝试了这段代码:
Xpred = [[3, 7.8]]
prediction = model.predict(Xpred, verbose=1)
print(prediction)
它返回:
[[3.4789115e-02 8.4235787e-01 7.6775238e-02 1.9370530e-02 1.0821970e-02
4.8491983e-03 4.7121649e-03 7.4993627e-04 2.9366722e-04 5.2804169e-03]]
我对堆栈和机器学习都很新,所以请告诉我如何改进,或者如果你有任何关于机器学习的阅读材料或资源可以分享的话!
英文:
Ive coded this machine learning algoritm but it retured to me a wierd array. I want to input 2 numbers and then those numbers be clasified into similar results found in Y, How do I make a prediction using this model?
import numpy as np # mutivariate clasification
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
X =np.array(
[[3, 7],
[3, 6],
[3, 7.2],
[6, 8],
[7, 7.5],
[7.9, 7.5]])
Y =np.array([1, 1, 1, 2, 3, 3])
model = Sequential([
Dense(units = 25, activation = "relu"),
Dense(units = 15, activation = "relu"),
Dense(units = 10, activation = "softmax"),])
from keras.losses import SparseCategoricalCrossentropy
model.compile(loss = SparseCategoricalCrossentropy())
model.fit(X, Y, epochs = 100)
I tried this code:
Xpred = [[3,7.8]]
prediction = model.predict(Xpred, verbose = 1)
print(prediction)
and it returned:
[[3.4789115e-02 8.4235787e-01 7.6775238e-02 1.9370530e-02 1.0821970e-02
4.8491983e-03 4.7121649e-03 7.4993627e-04 2.9366722e-04 5.2804169e-03]]
Im new to stack and ML so please let me know how I could improve or if you have any reading materal or resources for ML you could share!
答案1
得分: 0
这里有很多需要理解的内容,我建议你通过更多关于分类的教程并严格按照步骤操作(keras文档对此非常有帮助),但我将尝试为你解释足够多的内容,以便你理解你所看到的并使你的基本示例正常工作。
在最后得到的浮点数数组是每个类别的概率数组。由于你设置了输出层的单元数为10,尽管你的数据中只有3个类别,所以有10个概率值。我猜测你只是想对你的新特征集[3, 7.8]
进行分类,所以你取最高概率。在这种情况下,你可以通过检查得到的概率数组来看出,预测的类别是1
,因为最高概率为8.4235787e-01
,位于第一个位置。但通常你可以使用np.argmax
来获得这个值,它可以应用在NumPy数组上。
让你的代码按照你的期望工作的步骤:
- 将输出层的单元数设置为类别的数量(3)。
- 对类别进行编号,从0开始。通常,你可以使用整数编码来完成这个任务(在keras和其他ML库中都有工具可用,所以请查看这些工具),但由于这里的数据非常小,我们可以手动进行,将1替换为0,2替换为1,3替换为2。
- 对从
model.predict
得到的数组应用np.argmax
以获取预测的类别。
代码最终看起来像这样:
import numpy as np # 多元分类
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
X = np.array(
[[3, 7],
[3, 6],
[3, 7.2],
[6, 8],
[7, 7.5],
[7.9, 7.5]])
Y = np.array([0, 0, 0, 1, 2, 2])
model = Sequential([
Dense(units=25, activation="relu"),
Dense(units=15, activation="relu"),
Dense(units=3, activation="softmax")
])
from keras.losses import SparseCategoricalCrossentropy
model.compile(loss=SparseCategoricalCrossentropy())
model.fit(X, Y, epochs=100)
for prediction in model.predict([[3, 7.8]]):
print(prediction)
print(np.argmax(prediction))
输出的最后一部分是:
[0.916569 0.07700075 0.00643022]
0
因此,预测的类别是0(或者基于你最初提供的数据,可能是1),这符合我们基于训练数据和新数据的检查所期望的结果。
英文:
There's a lot to understand here here and I suggest that you work through some more tutorials on classification and follow the steps closely (keras documentation is quite good for this), but I'll attempt to talk you through enough to understand what you're seeing and get your basic example working.
The array of floating point numbers you get at the end is an array of probabilities for each class. There are 10 probabilities because you set the number of units in the output layer to 10, even though you only have 3 classes in your data. I'm guessing that you just want to get a classification for your new set of features ([3, 7.8]
), so you take the highest probability. In this case you can see just from inspection that the predicted class is 1
because the highest probability is 8.4235787e-01
which is in the 1st position, but in general you can get this using np.argmax
on a numpy array.
Steps to get your code working the way you expect:
- Set the number of units in the output layer to the number of classes (3)
- Number the classes starting from 0. In general you can use integer encoding for this task (there are tools for this in keras and other ML libraries so look into those), but since the data is very small here we can just do it by hand by replacing 1 with 0, 2 with 1, and 3 with 2.
- Apply
np.argmax
to the arrays you get frommodel.predict
to get the predicted class
The code ends up looking like this:
import numpy as np # mutivariate clasification
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
X =np.array(
[[3, 7],
[3, 6],
[3, 7.2],
[6, 8],
[7, 7.5],
[7.9, 7.5]])
Y =np.array([0, 0, 0, 1, 2, 2])
model = Sequential([
Dense(units = 25, activation = "relu"),
Dense(units = 15, activation = "relu"),
Dense(units = 3, activation = "softmax")
])
from keras.losses import SparseCategoricalCrossentropy
model.compile(loss = SparseCategoricalCrossentropy())
model.fit(X, Y, epochs = 100)
for prediction in model.predict([[3, 7.8]]):
print(prediction)
print(np.argmax(prediction))
The final part of the output is:
[0.916569 0.07700075 0.00643022]
0
So the predicted class is 0 (or 1 based on the original data you posted), which is what we'd expect based on inspection of the training data and new data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论