Tensorflow 提高测试准确性

伙计们,我是新手,距离我开始学习已经两天了。我按照tensorflow的步骤做了,并记下代码的含义。之后,我尝试做类似的项目。

由于才两天,我尝试做一下图像分类。但测试结果的准确性较差,无法做出真实的评价。

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

这是我的代码:

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) + '...') 


慕盖茨4494581
浏览 125回答 1
1回答

芜湖不芜

你的“模型”有两次合理性。models.Sequential() ...模型 = 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)])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python