Tensorflow不会影响输入和权重。 “尺寸必须相等”

时间:2018-02-07 16:10:58

标签: tensorflow machine-learning neural-network computer-vision valueerror

我一直在研究一个简单的张量流神经网络。我的输入占位符是

x = tf.placeholder(tf.float32, shape=[None, 52000, 3])

我的权重矩阵初始化为全零(

W = tf.Variable(tf.zeros([52000, 10]))

我尝试使用不同颜色通道3的不同组合,但我想我只是不理解维度,因为我收到了错误:

  

Traceback(最近一次调用最后一次):文件   “C:\用户\大家\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ tensorflow \ python的\框架\ common_shapes.py”   第686行,在_call_cpp_shape_fn_impl中       input_tensors_as_shapes,status)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ errors_impl.py”,   第473行,退出       c_api.TF_GetCode(self.status.status))tensorflow.python.framework.errors_impl.InvalidArgumentError:Shape   必须是等级2,但对于'MatMul'(op:'MatMul')的输入是3级   形状:[?,52000,3],[52000,10]。

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近一次呼叫最后一次):文件“rating.py”,第65行,in          y = tf.matmul(x,W)+ b#“假”输出到训练/测试文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ OPS \ math_ops.py”   1891年,在matmul       a,b,transpose_a = transpose_a,transpose_b = transpose_b,name = name)文件   “C:\用户\大家\应用程序数据\本地\程序\ Python的\ Python35 \ LIB \站点包\ tensorflow \ python的\ OPS \ gen_math_ops.py”   第2436行,在_mat_mul中       name = name)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ op_def_library.py”,   第787行,在_apply_op_helper中       op_def = op_def)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”,   第2958行,在create_op中       set_shapes_for_outputs(ret)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”,   第2209行,在set_shapes_for_outputs中       shapes = shape_func(op)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”,   第2159行,在call_with_requiring中       return call_cpp_shape_fn(op,require_shape_fn = True)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ common_shapes.py”,   第627行,在call_cpp_shape_fn中       require_shape_fn)文件“C:\ Users \ Everybody \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ common_shapes.py”,   第691行,在_call_cpp_shape_fn_impl中       提高ValueError(err.message)ValueError:Shape必须是等级2,但对于输入形状为'MatMul'(op:'MatMul')的等级为3:[?,52000,3],   [52000,10]。

起初,我认为我的next_batch()函数是罪魁祸首,因为我必须自己创建,因为我使用scipy.misc.imread()“手动”上传了我的图像,其定义为:

q = 0
def next_batch(batch_size):
    x = images[q:q + batch_size]
    y = one_hots[q:q + batch_size]
    q = (q + batch_size) % len(images)
    return x, y

然而,经过深思熟虑后,我看不出这有什么问题,所以我想我只是对维度感到困惑。它应该是一个“扁平”的200x260彩色图像。我刚刚想到也许我还要把颜色通道弄平?如果好奇,我会在下面放置我的完整代码。我对Tensorflow有点新意,谢谢,所有。 (是的,它还不是CNN,我决定开始简单,只是为了确保我正确导入我的数据集。而且,我知道它很小,我的数据集也很小。)

############# IMPORT DEPENDENCIES ####################################

import tensorflow as tf
sess = tf.InteractiveSession() #start session
import scipy.misc
import numpy as np

######################################################################


#SET UP DATA #########################################################

images = []
one_hots = []

###########  IMAGES ##################################################

#put all the images in a list
for i in range(60):
    images.append(scipy.misc.imread('./shoes/%s.jpg' % str(i+1)))
    print("One image appended...\n")

#normalize them, "divide" by 255
for image in images:
    print("One image normalized...\n")
    for i in range(260):
        for j in range(200):
            for c in range(3):
                image[i][j][c]/=255

for image in images:
    tf.reshape(image, [52000, 3])


########################################################################


################# ONE-HOT VECTORS ######################################

f = open('rateVectors.txt')
lines = f.readlines()

for i in range(0, 600, 10):
    fillerlist = []
    for j in range(10):
        fillerlist.append(float(lines[i+j][:-1]))
    one_hots.append(fillerlist)
    print("One one-hot vector added...\n")

########################################################################3

#set placeholders and such for input, output, weights, biases
x = tf.placeholder(tf.float32, shape=[None, 52000, 3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

W = tf.Variable(tf.zeros([52000, 10]))  # These are our weights and biases
b = tf.Variable(tf.zeros([10]))         # initialized as zeroes.


#########################################################################


sess.run(tf.global_variables_initializer()) #initialize variables in the session

y = tf.matmul(x, W) + b # "fake" outputs to train/test


##################### DEFINING OUR MODEL  ####################################


#our loss function
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))

#defining our training as gradient descent
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)


######################  TRAINING #############################################


####################  OUR CUSTOM BATCH FUNCTION ##############################

q = 0
def next_batch(batch_size):
    x = images[q:q + batch_size]
    y = one_hots[q:q + batch_size]
    q = (q + batch_size) % len(images)
    return x, y

#train
for i in range(6):
  batch = next_batch(10)
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})
  print("Batch Number: " + i + "\n")

print("Done training...\n")

################  RESULTS  #################################################

#calculating accuracy
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#print accuracy
print(accuracy.eval(feed_dict={x: images, y_: one_hots}))

1 个答案:

答案 0 :(得分:0)

您的占位符应具有维度[None, 200, 260, 3],其中None是批量大小,200, 260是图片大小,3是渠道。

您的体重应为[filter_height, filter_width, num_channels, num_filters]

您的偏见应为[num_filters]

标签的尺寸应为[None, num_classes],其中None是批量大小,num_classes是图片所具有的类数。

这些只是为了确保数学运作。

我从here

获取了这些代码