Tensorflow 不训练 CIFAR - 100 数据

阿利德米尔

我正在尝试使用 TensorFlow 使用 CIFAR - 100 构建一个线性分类器。我从 Martin Gorner 的 MNIST 教程中获得了代码并做了一些更改。当我运行这段代码时,tensorflow 不会训练(代码正在运行,但准确度保持为 1.0 和损失(交叉熵保持为 4605.17),我不知道有什么问题,我实际上是 TF 的新手,任何帮助表示赞赏。

import pickle
import numpy as np
import os
import tensorflow as tf
from tensorflow.python.framework import tensor_util
import math

#imports data
def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict


cifar100_test = {}
cifar100_train = {}
labelMap = {}
labelNames = {}
# Load the raw CIFAR-10 data.
cifar100_test = unpickle('dataset/cifar-100-python/test')
cifar100_train = unpickle('dataset/cifar-100-python/train')
labelMap = unpickle('dataset/cifar-100-python/meta')
#tr for training data and te for testing data, X is data, Y is label
Xtr = cifar100_train[b'data']
Yr = cifar100_train[b'fine_labels']
Xte = cifar100_test[b'data']
Ye = cifar100_test[b'fine_labels']
classNames = labelMap[b'fine_label_names']



num_train = Xtr.shape[0]
num_test = Xte.shape[0]
num_class = len(classNames)

Ytr = np.zeros([num_train, num_class])
Yte = np.zeros([num_test, num_class])

Ytr[0:num_train, Yr[0:num_train]] = 1
Yte[0:num_test, Ye[0:num_test]] = 1


# As a sanity check, we print out the size of the training and test data.
print('Train data shape:', Xtr.shape)
print('Train Label shape:', Ytr.shape)
print('Test data shape:', Xte.shape)
print('Test Label shape:', Yte.shape)
print('Name of Predicted Class:', classNames[0]) #indice of the label name is the indice of the class.



Xtrain = Xtr#[:1000]
Xtest = Xte#[:100]
Ytrain = Ytr#[:1000]
Ytest = Yte#[:100]

print('Train data shape:', Xtrain.shape)
print('Train Label shape:', Ytrain.shape)
print('Test data shape:', Xtest.shape)
print('Test Label shape:', Ytest.shape)



Xtrain = np.reshape(Xtrain,(50000, 32, 32, 3)).transpose(0,1,2,3).astype(float)
Xtest = np.reshape(Xtest,(10000, 32, 32, 3)).transpose(0,1,2,3).astype(float)

Xbatches = np.split(Xtrain, 500); #second number is # of batches
Ybatches = np.split(np.asarray(Ytrain), 500);
                   
XtestB = np.split(Xtest, 100);
YtestB = np.split(Ytest, 100);                   

print('X # of batches:', len(Xbatches))
print('Y # of batches:', len(Ybatches))



# input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch
X = tf.placeholder(tf.float32, [100, 32, 32, 3])
# correct answers will go here
Y_ = tf.placeholder(tf.float32, [100, 100])
# weights W[784, 10]   784=28*28
W = tf.Variable(tf.zeros([3072, 100]))
# biases b[10]
b = tf.Variable(tf.zeros([100]))

# flatten the images into a single line of pixels
# -1 in the shape definition means "the only possible dimension that will preserve the number of elements"
XX = tf.reshape(X, [-1, 3072])

# The model
Y = tf.nn.softmax(tf.matmul(XX, W) + b)

# loss function: cross-entropy = - sum( Y_i * log(Yi) )
#                           Y: the computed output vector
#                           Y_: the desired output vector

# cross-entropy
# log takes the log of each element, * multiplies the tensors element by element
# reduce_mean will add all the components in the tensor
# so here we end up with the total cross-entropy for all images in the batch
cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0  # normalized for batches of 100 images,
                                                          # *10 because  "mean" included an unwanted division by 10

# accuracy of the trained model, between 0 (worst) and 1 (best)
correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# training, learning rate = 0.005
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)


# init
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)



for i in range(500):
    # the backpropagation training step
    t, Loss = sess.run([train_step, cross_entropy], feed_dict={X: Xbatches[i], Y_: Ybatches[i]})
    print(Loss)
    print(i)
    

for i in range(100):
    print('accuracy:', sess.run(accuracy, feed_dict={X: XtestB[i], Y_: YtestB[i]}))

阿利德米尔

抱歉发帖,结果证明这是一个基本错误。我改变了以下;

Ytr[0:num_train, Yr[0:num_train]] = 1

Yte[0:num_test, Ye[0:num_test]] = 1

Ytr[range(num_train), Yr_temp[range(num_train)]] = 1 Yte[range(num_test), Ye_temp[range(num_test)]] = 1

第一个使所有值都为 1,但我只想对真正的类 1 和其他元素进行索引 0。感谢您的时间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

tensorflow cifar10从检查点文件恢复训练

来自分类Dev

加载CIFAR10训练数据时出现内存错误

来自分类Dev

如何重塑输入以在 CIFAR 数据集上进行训练?

来自分类Dev

目标的Tensorflow形状不匹配(cifar10)

来自分类Dev

TensorFlow MLP不训练XOR

来自分类Dev

如何提高cifar-100数据集的准确性?我目前的准确度是10%

来自分类Dev

在PyTorch中使用预训练的ResNet50解决CIFAR10数据集的问题

来自分类Dev

TensorFlow模型不执行任何训练

来自分类Dev

使用PyTorch将Traininng数据集分为CIFAR10的训练和验证集后,如何增加数据?

来自分类Dev

使用TensorFlow训练不平衡数据

来自分类Dev

无法在简单的数据集上训练Tensorflow

来自分类Dev

在Tensorflow RNN中使用CSV训练数据

来自分类Dev

Keras不训练整个数据集

来自分类Dev

如果CNN模型同时在keras的cifar10 / 100上训练,如何在一个图中绘制精度/损耗图?

来自分类Dev

批量读取Cifar10数据集

来自分类Dev

使用VGG网络微调Cifar数据

来自分类Dev

Tensorflow-TextSum模型:如何创建自己的训练数据

来自分类Dev

tensorflow将训练数据存储在GPU内存中

来自分类Dev

在Tensorflow中读取大型训练/验证/测试数据集

来自分类Dev

如何使用Tensorflow数据集进行CNN模型训练

来自分类Dev

如何使用Tensorflow数据集进行CNN模型训练

来自分类Dev

keras tensorflow2获得训练数据的结果

来自分类Dev

TensorFlow仅在提供的训练数据的1/32上运行

来自分类Dev

使用Galaxy Zoo数据集,TensorFlow和Keras训练GAN

来自分类Dev

如何在医学数据上训练tensorflow.js

来自分类Dev

在Tensorflow中读取大型训练/验证/测试数据集

来自分类Dev

tensorflow将训练数据存储在GPU内存中

来自分类Dev

Tensorflow-TextSum模型:如何创建自己的训练数据

来自分类Dev

Tensorflow:对 JSON 数据进行训练以生成类似的输出

Related 相关文章

  1. 1

    tensorflow cifar10从检查点文件恢复训练

  2. 2

    加载CIFAR10训练数据时出现内存错误

  3. 3

    如何重塑输入以在 CIFAR 数据集上进行训练?

  4. 4

    目标的Tensorflow形状不匹配(cifar10)

  5. 5

    TensorFlow MLP不训练XOR

  6. 6

    如何提高cifar-100数据集的准确性?我目前的准确度是10%

  7. 7

    在PyTorch中使用预训练的ResNet50解决CIFAR10数据集的问题

  8. 8

    TensorFlow模型不执行任何训练

  9. 9

    使用PyTorch将Traininng数据集分为CIFAR10的训练和验证集后,如何增加数据?

  10. 10

    使用TensorFlow训练不平衡数据

  11. 11

    无法在简单的数据集上训练Tensorflow

  12. 12

    在Tensorflow RNN中使用CSV训练数据

  13. 13

    Keras不训练整个数据集

  14. 14

    如果CNN模型同时在keras的cifar10 / 100上训练,如何在一个图中绘制精度/损耗图?

  15. 15

    批量读取Cifar10数据集

  16. 16

    使用VGG网络微调Cifar数据

  17. 17

    Tensorflow-TextSum模型:如何创建自己的训练数据

  18. 18

    tensorflow将训练数据存储在GPU内存中

  19. 19

    在Tensorflow中读取大型训练/验证/测试数据集

  20. 20

    如何使用Tensorflow数据集进行CNN模型训练

  21. 21

    如何使用Tensorflow数据集进行CNN模型训练

  22. 22

    keras tensorflow2获得训练数据的结果

  23. 23

    TensorFlow仅在提供的训练数据的1/32上运行

  24. 24

    使用Galaxy Zoo数据集,TensorFlow和Keras训练GAN

  25. 25

    如何在医学数据上训练tensorflow.js

  26. 26

    在Tensorflow中读取大型训练/验证/测试数据集

  27. 27

    tensorflow将训练数据存储在GPU内存中

  28. 28

    Tensorflow-TextSum模型:如何创建自己的训练数据

  29. 29

    Tensorflow:对 JSON 数据进行训练以生成类似的输出

热门标签

归档