Python 在Tensorflow中维度是如何工作的?

Python 在Tensorflow中维度是如何工作的?,python,tensorflow,Python,Tensorflow,我对Python和Tensorflow一无所知。我想实现一种简单的CNN,这就是我到目前为止所做的: import tensorflow as tf import numpy as np from libs import utils import cv2 import glob from tensorflow.python.framework.ops import reset_default_graph reset_default_graph() # We first get the gra

我对Python和Tensorflow一无所知。我想实现一种简单的CNN,这就是我到目前为止所做的:

import tensorflow as tf
import numpy as np
from libs import utils
import cv2
import glob

from tensorflow.python.framework.ops import reset_default_graph
reset_default_graph()

# We first get the graph that we used to compute the network
g = tf.get_default_graph()

# And can inspect everything inside of it
[op.name for op in g.get_operations()]



X = tf.placeholder(tf.float32, [None,720000])
Y = tf.placeholder(tf.int32, [None])

X_data = []
files = glob.glob ("C:/Users/Maede/Desktop/Master Thesis/imlearning/*.jpg")

for myFile in files:
    print(myFile)
    image = cv2.imread (myFile)
    X_data.append (image)

print('X_data shape:', np.array(X_data).shape)

data=np.array(X_data)
data=np.reshape(data,(30,720000))
label=np.array([(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),
               (0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),
               (0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0)])

###########################################################
train_batch_size = 2
def random_batch():
    num_images = 30
    idx = np.random.choice(num_images,
                           size=train_batch_size,
                           replace=False)
    x_batch = data[idx,:]
    y_batch = label[idx, :]

    return x_batch, y_batch
######################
#

X_tensor = tf.reshape(X, [-1, 400,600,3])

filter_size = 5
n_filters_in = 3
n_filters_out = 32
W_1 = tf.get_variable(
    name='W',
    shape=[filter_size, filter_size, n_filters_in, n_filters_out],
    initializer=tf.random_normal_initializer())

b_1 = tf.get_variable(
    name='b',
    shape=[n_filters_out],
    initializer=tf.constant_initializer())

h_1 = tf.nn.relu(
    tf.nn.bias_add(
        tf.nn.conv2d(input=X_tensor,
                     filter=W_1,
                     strides=[1, 2, 2, 1],
                     padding='SAME'),
        b_1))

n_filters_in = 32
n_filters_out = 64
n_output = 2
W_2 = tf.get_variable(
    name='W2',
    shape=[filter_size, filter_size, n_filters_in, n_filters_out],
    initializer=tf.random_normal_initializer())
b_2 = tf.get_variable(
    name='b2',
    shape=[n_filters_out],
    initializer=tf.constant_initializer())
h_2 = tf.nn.relu(
    tf.nn.bias_add(
        tf.nn.conv2d(input=h_1,
                 filter=W_2,
                 strides=[1, 2, 2, 1],
                 padding='SAME'),
        b_2))

# We'll now reshape so we can connect to a fully-connected/linear layer:
h_2_flat = tf.reshape(h_2, [-1, 100*150* n_filters_out])

# NOTE: This uses a slightly different version of the linear function than the lecture!
h_3, W = utils.linear(h_2_flat, 400, activation=tf.nn.relu, name='fc_1')

# NOTE: This uses a slightly different version of the linear function than the lecture!
Y_pred, W = utils.linear(h_3, n_output, activation=tf.nn.softmax, name='fc_2')
y_one_hot = tf.one_hot( Y , 2 )


cross_entropy = -tf.reduce_sum(y_one_hot  * tf.log(Y_pred + 1e-12))
optimizer = tf.train.AdamOptimizer().minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(Y_pred, 1), tf.argmax(y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

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

batch_size = 2
n_epochs = 5
for epoch_i in range(n_epochs):
    for batch_xs, batch_ys in random_batch():
        sess.run(optimizer, feed_dict={
                X: np.array(batch_xs).reshape([1,720000]),
                Y: batch_ys
        })
    valid = data   #### DATA haie validation
    print(sess.run(accuracy,
                   feed_dict={
                       X: data,
                       Y: label
                   }))
输入是30幅400*600*3维的图像,我想把它们分为两类。问题是当我使用此命令时:
X:np.array(batch_xs).重塑([1720000]),
错误如下所示: ValueError:无法将大小为2的数组重塑为形状(1720000)

当我使用:

X:batch_xs

错误是:

ValueError:无法为具有形状“(?,720000)”的张量“占位符:0”提供形状(720000,)的值

我完全搞不清楚什么是batch_xs维度,以及为什么它会在不同的情况下发生变化。

np。数组(batch_xs)
与图像的大小不同

对于batch_xs,在random_batch()中使用batch_ys也是一种稍微奇怪的运行代码的方式,我想这也会导致您的问题。您通常使用
for
来迭代某个iterable

在您的例子中,iterable就是您的函数返回的,一个带有
batch_xs,batch_ys
的元组。但在同一步骤中,您将
将元组的第一个(!)值解压缩为两个变量
batch_xs
batch_ys

replace=False
在您的情况下不起任何作用,因为您只调用了函数
random\u batch()
。在下一次迭代中,它将再次拥有完整的数据集

以下是一个简单的案例:

import numpy as np

# I removed a dimension from the arrays
data = np.array([[1.0, 1.0, 1.0],
                 [2.0, 2.0, 2.0],
                 [3.0, 3.0, 3.0]])

label = np.array([[10.0, 10.0, 10.0],
                   [20.0, 20.0, 20.0],
                   [30.0, 30.0, 30.0]])


def random_batch():

    idx = np.random.choice(3, size=2)
    x_batch = data[idx,:]
    y_batch = label[idx, :]

    return x_batch, y_batch


# the outer variable names x_batch and y_batch are not related at all to the ones
# inside random_batch()
# iterate over whatever random_batch() returns

# for x_batch, y_batch in random_batch() is equivalent to
# for (x_batch, y_batch) in random_batch()

# in the first iteration the iterable is `x_batch`, in the second one`y_batch`.
# and each of the iterable is "unpacked", basically in the first iteration 
# your are assigning

# (x_batch, y_batch) = x_batch

# in the second iteration 

# (x_batch, y_batch) = y_batch

# When unpacking you are splitting the two elements created by `size=2`
# in `random_batch()`

for (x_batch, y_batch) in random_batch():

    print(x_batch)
    print(y_batch)
这是基本的Python基础知识,要熟悉它,请查找
元组解包
iterable
for循环

用这个替换内部for循环,它应该可以工作。这可能不是您所期望的,但却是您的代码应该做的

batch_xs, batch_ys = random_batch()
sess.run(optimizer, feed_dict={
      X: np.array(batch_xs).reshape([1,720000]),
            Y: batch_ys
      })
如果你想用100个批次进行训练,可以这样做

for k in range(100):
    batch_xs, batch_ys = random_batch()
    sess.run(optimizer, feed_dict={
          X: np.array(batch_xs).reshape([1,720000]),
                Y: batch_ys
          })
通常,您会尝试删除尽可能多与问题无关的代码,以便更容易找到问题。找到尽可能少的代码,仍然显示您的问题。
您的问题与tensorflow无关,因此您可以删除与tensorflow相关的所有内容,以便于查找。您的问题与numpy和数组形状有关。

实际上这不是我的真实数据,因为它们还不可用。但是我想用很少的假数据来构造分类器。我用train\u batch\u size=2选择两个图像进行训练,“for”循环就是为了这个目的,你建议的命令帮不了我。第一个:
np.array(batch_xs).resporate([train_batch_size,720000])
有一个错误:ValueError:无法将大小为720000的数组重塑为形状(272000),第二个数组的大小为1440000,这同样不能用[None,720000]输入占位符X维度我想我需要这个
循环来为两个批处理图像重复优化。(我选择批量大小为2)是的,我想用一些随机图像来训练我的网络。您是否要在
sess.run(…)之前添加行
print('shape',batch_xs.shape)
?这将超出当前批次的形状。试着告诉我形状。我在答案的末尾加了一个例子和一些文字。请试试看是的,我明白。我应该尽量减少我的代码以显示我的问题。谢谢你的建议。我试着打印('shape',batchxs.shape)
,它的(272000)。实际上,我想在每个历元中随机选取两张大小为(400*600*3=720000)的图像,并训练我的网络。在您建议的代码中,由于消除了
for
循环,因此尺寸错误更改为:ValueError:无法将大小为1440000的数组重塑为形状(1720000),因为它试图将两个图像(720000*2=1440000)作为占位符(无720000)尺寸馈送到
y