我想用not_mnist来训练vgg16。 train_datasets
形状为(batch_size,image_size,image_size,channels)
,train_labels
形状为(batch_size,10)
我的代码是
import tensorflow as tf
import tensorflow.contrib.slim.nets as nets
import pickle
import numpy as np
slim = tf.contrib.slim
with open('notMNIST.pickle', 'rb') as f:
save = pickle.load(f)
train_dataset = save['train_dataset']
train_labels = save['train_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels']
batch_size = 16
num_labels = 10
image_size = 28
num_channnels = 1
train_log_dir = './variables/'
train_dataset = np.reshape(train_dataset, (-1, image_size, image_size, num_channnels))
train_labels = (np.arange(num_labels) == train_labels[:, None]).astype(np.float32)
tf_train_labels = tf.constant(train_labels)
tf_train_dataset = tf.constant(train_dataset)
tf_train_dataset = tf.image.resize_images(tf_train_dataset, [224, 224])
with tf.Graph().as_default():
predictions, _ = nets.vgg.vgg_16(tf_train_dataset, 10, is_training=True)
slim.losses.softmax_cross_entropy(predictions, tf_train_labels)
total_loss = slim.losses.get_total_loss()
tf.summary.scalar('losses/total_loss', total_loss)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train_tensor = slim.learning.create_train_op(total_loss, optimizer)
slim.learning.train(train_tensor, train_log_dir)
但是这段代码无法运行,错误是
Traceback (most recent call last):
File "/Users/szp/Documents/github/deeplearning_example/not_mnist_cnn/Inception_v1_cnn.py", line 33, in <module>
total_loss = slim.losses.get_total_loss()
File "/Users/szp/Documents/github/deeplearning_example/venv/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 117, in new_func
return func(*args, **kwargs)
File "/Users/szp/Documents/github/deeplearning_example/venv/lib/python3.5/site-packages/tensorflow/contrib/losses/python/losses/loss_ops.py", line 264, in get_total_loss
return math_ops.add_n(losses, name=name)
File "/Users/szp/Documents/github/deeplearning_example/venv/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 1861, in add_n
raise ValueError("inputs must be a list of at least one Tensor with the "
ValueError: inputs must be a list of at least one Tensor with the same dtype and shape
我跟踪代码,
在math_ops.py
和get_total_loss
函数中,get_losses()
无法返回任何值。
你的问题的答案可能并不总是你想要的,但这并不意味着它是错的。确定答案并非总是可行。如有疑问,请让人们引用他们的消息来源,或解释他们在何处/何处学到某些东西。即使我们不同意你的看法,或者告诉你你想要听到的内容,也要记住:我们只是想提供帮助。
谢谢,在询问时我会记住这些提示