Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:所有输入数组的维数必须相同-对于Tensorflow Manual MSINT_Python_Numpy_Tensorflow - Fatal编程技术网

Python ValueError:所有输入数组的维数必须相同-对于Tensorflow Manual MSINT

Python ValueError:所有输入数组的维数必须相同-对于Tensorflow Manual MSINT,python,numpy,tensorflow,Python,Numpy,Tensorflow,我正在尝试使用我在tensor flow中制作的随机梯度下降代码,并训练25112张类似于MINST数据集的图像(这些文件看起来非常像它)。我很抱歉,如果这是一个简单的问题,但我不知道如何继续。谢谢大家! 我遇到了以下错误: “ValueError:所有输入数组的维数必须相同” 在这一行代码中: x=np.c.[np.ones(n),图像张量2]#第75行 我无法确定为什么这不起作用-我想这与我如何读取图像文件有关,但我不能确定。这是我的密码 import numpy as np import

我正在尝试使用我在tensor flow中制作的随机梯度下降代码,并训练25112张类似于MINST数据集的图像(这些文件看起来非常像它)。我很抱歉,如果这是一个简单的问题,但我不知道如何继续。谢谢大家!

我遇到了以下错误:

“ValueError:所有输入数组的维数必须相同”

在这一行代码中:

x=np.c.[np.ones(n),图像张量2]#第75行

我无法确定为什么这不起作用-我想这与我如何读取图像文件有关,但我不能确定。这是我的密码

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import argparse 

#load the images in order
vector = [] #initialize the vector

filenames = tf.train.match_filenames_once("train_data/*.jpg")
filename_queue = tf.train.string_input_producer(filenames)

image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
image_orig = tf.image.decode_jpeg(image_file)
image = tf.image.resize_images(image_orig, [28, 28])
image.set_shape((28, 28, 3))
images = tf.image.decode_jpeg(image_file)
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    image_tensor = sess.run([images]) 
    #print(image_tensor)
    #coord.request_stop()
    #coord.join(threads)

image_tensor2 = np.array(image_tensor)
n_samples = image_tensor2.shape[0]
lossHistory=[]
ap = argparse.ArgumentParser()
ap.add_argument("-b", "--batch-size", type = int, default =32, help = "size of SGD mini-batches")
args = vars(ap.parse_args())

  # Create the model
x = tf.placeholder(tf.float32, [None, 784]) #784=28*28
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b

y_ = tf.placeholder(tf.float32, [25112, 10])


sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
  # Train

def next_batch(x, batchSize):
    for i in np.arange(0, x.shape[0], batchSize):
        yield (x[i:i + batchSize])

def gradient_descent_2(alpha, x, y, numIterations):
    m,n = (784, 25112) # number of samples
    theta = np.ones(n)
    theta.fill(0.01)
    x_transpose = x.transpose()
    losshistory=[]
    count = 0
    batchX = 50
    for (batchX) in next_batch(x, args["batch_size"]):
        for iter in range(0, numIterations):
            hypothesis = np.dot(x, theta)
            loss = hypothesis - y
            J = np.sum(loss ** 2) / (2 * m)  # cost
            lossHistory.append(J)
            print( "iter %s | J: %.3f" % (iter, J))      
            gradient = np.dot(x_transpose, loss) / m         
            theta = theta - alpha * gradient  
    return theta

if __name__ == '__main__':


    m, n = (784, 25112)
    x = np.c_[ np.ones(n), image_tensor2] # insert column
    alpha = 0.001 # learning rate
    theta = gradient_descent_2(alpha, image_tensor2, y_, 50)
    fig = plt.figure()
    print(theta)

我的直觉是图像张量是三维数组,而np.ones(n)是创建一维数组

但你的目标是插入一个偏见栏(我猜),一个快速的方法是:

b = np.ones((n + n+1))
x = b[:, 1:] = image_tensor2
示例

c = 
array([[7, 5, 6, 8, 7],
       [5, 8, 7, 9, 7],
       [9, 5, 6, 5, 8]])

b = np.ones((3, 6))
d = b[:, 1:] =c

d =
array([[ 1.,  7.,  5.,  6.,  8.,  7.],
       [ 1.,  5.,  8.,  7.,  9.,  7.],
       [ 1.,  9.,  5.,  6.,  5.,  8.]])

好。图像张量2(图像张量2.形状)的尺寸是多少?