线性分类器Tensorflow2不训练(1个神经元模型)



我目前正在研究CIFAR-10数据集,这是一个有10个类的图像分类问题。

我已经开始用Tensorflow 2开发一个没有LinearClassifier对象的线性分类。

  • X 形状对应于 10 000 张 32*32 像素的图像 RBG = (10000, 3072(
  • Y_one_hot是一个热向量 = (10000, 10(

模型创建代码:

model = tf.keras.Sequential()
model.add(Dense(1, activation="linear", input_dim=32*32*3))
model.add(Dense(10, activation="softmax", input_dim=1))
model.compile(optimizer="adam", loss="mean_squared_error", metrics=["accuracy"])

训练代码:

model.fit(X, Y_one_hot, batch_size=10000, verbose=1, epochs=100)

预测代码:

img = X[0].reshape(1, 3072) # Select image 0
res = np.argmax((model.predict(img))) # select the max in output 

问题:

res 值始终相同。看来我的模型没有学习。

模型.总结

摘要显示 :

dense (Dense)                (None, 1)                 3073      
dense_1 (Dense)              (None, 10)                20        
Total params: 3,093
Trainable params: 3,093
Non-trainable params: 0

准确性和损失:

Epoch 1/100
10000/10000 [==============================] - 2s 184us/sample - loss: 0.0949 - accuracy: 0.1005
Epoch 50/100
10000/10000 [==============================] - 0s 10us/sample - loss: 0.0901 - accuracy: 0.1000
Epoch 100/100
10000/10000 [==============================] - 0s 8us/sample - loss: 0.0901 - accuracy: 0.1027

你知道为什么我的模型总是预设相同的值吗?

谢谢

一个评论:

您使用的损失loss="mean_squared_error"不用于分类。用于回归。两个截然不同的问题。尝试交叉熵。例如

`model.compile(optimizer=AdamOpt, 
      loss='categorical_crossentropy', metrics=['accuracy'])`

你可以在这里找到一个例子:https://github.com/michelucci/oreilly-london-ai/blob/master/day1/Beginner%20friendly%20networks/First_Example_of_a_CNN_(CIFAR10(.ipynb。是我给的培训用的笔记本。该网络是CNN,但您可以用自己的网络进行更改。

试试...

祝你好运,翁贝托

最新更新