【问题标题】:Tensorflow Increase Test AccuracyTensorflow 提高测试精度
【发布时间】:2020-12-17 02:03:24
【问题描述】:

伙计们,我是 tensorflow 的新手,刚开始学习 2 天。我遵循并制作了tensorflow的步骤并注意代码的含义。之后我尝试做类似的项目,就像我在 tensorflow 教程中所做的那样。

由于才2天,我试着做一个图像分类。但是测试结果的准确性太差,无法做出真实的评估。

能否请您指导,教我如何改进此代码或者我应该学习什么来改进此代码...

注意事项: -我在colabs.google中做了这个项目(你可以在这里复制代码试试) -我拿的数据集===> https://www.cs.toronto.edu/~kriz/cifar.html

这是我的代码:

import tensorflow as tf

from tensorflow import keras

import numpy as np

import matplotlib.pyplot as plt

(train_i, train_l), (test_i, test_l) = tf.keras.datasets.cifar10.load_data()

classnames = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

model = keras.Sequential([    
    keras.layers.Flatten(),
    keras.layers.Dense(100, activation='relu'), #burada kaç tane node olacağını belirtiyoruz yani mesela burada 108 se 108 tane node vardır. Node sayısını arrtırdıkça işlem hızımız düşüyor ama tahmin değerlerimiz gerçeğe daha yakın oluyor.
    keras.layers.Dense(10) #burada ise 10 diyoruz çünkü 10 tane class içinden seçecek.
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_i, train_l, epochs=100)

test_loss, test_acc = model.evaluate(test_i,  test_l, verbose=2)
print(test_acc)
prediction = tf.keras.Sequential([model, tf.keras.layers.Softmax()]).predict(test_i)

i = 90

prediction[i]

prediction_made= np.argmax(prediction[i])

f= train_l[i]

s=str(train_l[i])
print(str(s)[1:-1])

b = int(str(s)[1:-1])

y = classnames[b]

x = classnames[prediction_made]

img = train_i
plt.grid(False)
plt.xticks([]) 
plt.yticks([]) 
plt.imshow(img[i])

plt.xlabel('The True Label is ' + repr(y) + 
           ', and The Predicted Label is ' + repr(x) + '...') 

【问题讨论】:

    标签: python numpy matplotlib keras tensorflow2.0


    【解决方案1】:

    您的“模型”有两次合理性。

    1. models.Sequential() ...
    2. 模型 = keras.Sequential(...)

    所以第一部分不包含在最终的“模型”中。

    像这样修改模型零件代码

    model = keras.Sequential([  
       keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32 ,3)),
       keras.layers.MaxPooling2D((2, 2)),                     
       keras.layers.Flatten(),
       keras.layers.Dense(100, activation='relu'),
       keras.layers.Dense(10)
    ])
    

    【讨论】:

    • 我试过了,但它说:第 36 行]) ^ SyntaxError: invalid syntax
    • @Ö.ALPERENGÜL 我不知道你的第 36 行是什么。检查我制作的这个 colab 页面。它有效:colab.research.google.com/drive/…
    • 谢谢,我可以问其他人吗?为什么准确率这么低?我该如何改进它?
    • @Ö.ALPERENGÜL 您指的是哪种精度? Epoch 100/100 1563/1563 [==============================] - 36s 23ms/step - loss: 0.4011 - accuracy: 0.8601 在 epoch 100 时,模型的准确率是 86%,这并不低。
    猜你喜欢
    • 2020-06-04
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2020-06-01
    • 2021-05-14
    • 2020-04-14
    • 2020-12-22
    相关资源
    最近更新 更多