为了账号安全,请及时绑定邮箱和手机立即绑定

Tensorflow 提高测试准确性

Tensorflow 提高测试准确性

慕盖茨4494581 2023-06-27 13:35:51
伙计们,我是新手,距离我开始学习已经两天了。我按照tensorflow的步骤做了,并记下代码的含义。之后,我尝试做类似的项目。由于才两天,我尝试做一下图像分类。但测试结果的准确性较差,无法做出真实的评价。您能否指导,教我如何改进这段代码或者我应该学习什么来改进这段代码......这是我的代码:import tensorflow as tffrom tensorflow import kerasimport numpy as npimport 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 = 90prediction[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_iplt.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) + '...') 
查看完整描述

1 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

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

  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)

])


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信