我试图用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]}))发布于 2020-12-13 02:23:25
当然,它将不返回任何变量,因为文档表示它将返回一个更新var_list中变量的操作
如果您想查看损失值,可以写以下内容:
_, loss = sess.run([train_step, cross_entropy], feed_dict={x : x_train , y_ : y_train[j]})
print(j, loss)https://stackoverflow.com/questions/65269606
复制相似问题