【问题标题】:Deep learning with Tensrorflow and cifar10使用 Tensrorflow 和 cifar10 进行深度学习
【发布时间】:2020-12-13 11:33:31
【问题描述】:

我正在尝试使用 softmax 对 cifar10 图像进行分类,但模型没有学习任何东西。

下面的代码打印

 0 None
 1 None 
 2 None 

等等。如何修复我的代码或弄清楚为什么它总是无?

import tensorflow as tf
import numpy as np
from tensorflow.keras.datasets import cifar10, mnist
(x_train,y_train),(x_test,y_test) = cifar10.load_data()
x = tf.placeholder(tf.float32,[None,3072])
y_ = tf.placeholder(tf.float32,[None])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
#reshaping the y_train and x_train
y_train = y_train.reshape((5000,10))
x_test =x_test.reshape(10000,3072)
x_train = x_train.reshape(50000,3072)
y_test =y_test.reshape(1000,10)

# Data normalization
x_train = x_train/255
y_train = y_train/255
W1 = tf.Variable(tf.zeros([3072,10]))
b1 = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W1)+b1)
# this is a cross entropy
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))
# let's train to get the minimum loss using back+forward propagation
train_step = tf.train.AdamOptimizer(0.5).minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
for i in range(1000):
    for j in range(50):
       print(j,sess.run(train_step,feed_dict={x : x_train , y_ : y_train[j]}))

【问题讨论】:

  • 请不要将您的文本格式化为引号(已编辑)。

标签: python tensorflow deep-learning


【解决方案1】:

当然它会返回 None,因为 docs 说它会返回一个更新 var_list 中变量的操作

如果你想查看损失值,你可以这样写:

_, loss = sess.run([train_step, cross_entropy], feed_dict={x : x_train , y_ : y_train[j]})
print(j, loss)

【讨论】:

  • 没问题,很高兴为您提供帮助
  • 我正在尝试打印我的准确性,但我不知道如何,您有什么想法吗?谢谢
  • tf.compat.v1.metrics.accuracy 是你想要的,试着先在文档中找到你需要的东西,这样你就不必问每一个性质相似的问题:)
  • 我也非常感谢您的帮助,但我是 tensorflow 和深度学习的新手
  • 我添加了 for loop print(i,sess.run(accuracy,feed_dict={x:x_test,y_:y_test[i]})) 并将cross_entropy更改为correct_prediction = tf.equal( tf.argmax(y,-1), tf.argmax(y_,-1)) 但每次都得到 0.0 的准确度。但我不明白为什么
猜你喜欢
  • 2021-08-30
  • 1970-01-01
  • 2017-11-08
  • 2018-10-25
  • 2016-09-02
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多