gpt4 book ai didi

python - 使用 CNN、tensorflow 的 CIFAR 数据集 y_pred 的形状问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:48 25 4
gpt4 key购买 nike

数据来自 CIFAR-10我编写了以下代码最初,这段代码仅适用于 2 个卷积层和一个全连接层。我又添加了一个带有 128 4X4 滤波器的转换层。我定义了一个用于提取训练集批处理的类。我使用的批量大小为 100但现在当我试图找出我的 y_pred 时,它的形状正在变成 [200,10],而它应该是 [100,10],因为我的批量大小本身是 100 而不是 200。如果我删除添加的最后一个转换层,那么我的代码工作得很好,但我不想这样做,请告诉我应该做什么所以,请帮忙

def one_hot_encode(vec, vals = 10):
n = len(vec)
out = np.zeros((n,vals))
out[range(n), vec] = 1
return out


class CifarHelper():

def __init__(self):
self.i = 0

self.all_train_batches = [data_batch1, data_batch2, data_batch3, data_batch4, data_batch5]
self.test_batch = [test_batch]

self.training_images = None
self.training_labels = None

self.test_images = None
self.test_labels = None

def set_up_images(self):

print('setting up Training images and labels')

self.training_images = np.vstack([d[b'data'] for d in self.all_train_batches])
train_len = len(self.training_images)

self.training_images = self.training_images.reshape(train_len, 3, 32, 32).transpose(0, 2, 3, 1)/255
self.training_labels = one_hot_encode(np.hstack([d[b"labels"] for d in self.all_train_batches]))

print('Setting up test images and labels')

self.test_images = np.vstack([d[b'data'] for d in self.test_batch])
test_len = len(self.test_images)

self.test_images = self.test_images.reshape(test_len, 3, 32, 32).transpose(0, 2, 3, 1)/255
self.test_labels = one_hot_encode(np.hstack([d[b"labels"] for d in self.test_batch]))

def next_batch(self, batch_size):
x = self.training_images[self.i:self.i+batch_size].reshape(batch_size, 32, 32, 3)
y = self.training_labels[self.i:self.i+batch_size]
self.i = (self.i + batch_size) % len(self.training_images)
return x, y


ch = CifarHelper()
ch.set_up_images()


x = tf.placeholder(tf.float32, [None, 32, 32, 3])
y_true = tf.placeholder(tf.float32, [None, 10])
hold_prob = tf.placeholder(tf.float32)


def init_weights(shape):
init_random_dist = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init_random_dist)

def init_bais(shape):
init_bais_vals = tf.constant(0.1, shape = shape)
return tf.Variable(init_bais_vals)

def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2by2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

def convolutional_layer(input_x, shape):
W = init_weights(shape)
b = init_bais([shape[3]])
return tf.nn.relu(conv2d(input_x, W) + b)

def normal_full_layer(input_layer, size):
input_size = int(input_layer.get_shape()[1])
W = init_weights([input_size, size])
b = init_bais([size])
return tf.matmul(input_layer, W) + b


convo_1 = convolutional_layer(x, [4, 4, 3, 32])
convo_1_pooling = max_pool_2by2(convo_1)

convo_2 = convolutional_layer(convo_1_pooling, [4, 4, 32, 64])
convo_2_pooling = max_pool_2by2(convo_2)

convo_3 = convolutional_layer(convo_2_pooling, [4, 4, 64, 128])
convo_3_pooling = max_pool_2by2(convo_3)

convo_3_flat = tf.reshape(convo_2_pooling, [-1, 4*4*128])

full_layer_one = tf.nn.relu(normal_full_layer(convo_3_flat, 1024))

full_one_dropout = tf.nn.dropout(full_layer_one, keep_prob = hold_prob)

y_pred = normal_full_layer(full_one_dropout, 10)


batch = ch.next_batch(100)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
a = sess.run(y_pred, feed_dict = {x:batch[0], y_true:batch[1], hold_prob:.5})
a.shape

y_pred 的预期形状是 [100,10],但实际上,它是 [200,10]

最佳答案

平整图层时存在问题,我猜是在 reshape 时。使用 tf.layers.flatten 而不是制作自己的展平层。那会起作用的。

关于python - 使用 CNN、tensorflow 的 CIFAR 数据集 y_pred 的形状问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com