ValueError:使用序列设置数组元素。在TensorFlow中sess.run()

时间:2018-10-10 10:13:55

标签: python numpy tensorflow machine-learning neural-network

将迭代馈送到会话ValueError: setting an array element with a sequence.时发生在_, err = sess.run([train, loss], feed_dict={image: epoch_x, label: epoch_y})

data是一个二维矩阵,第一列为图像,第二列为一个热标签。

代码片段:

for epoch in range(epochs):
    epoch_loss = 0

    # data = np.random.shuffle(data)
    data = shuffle(data) #sklearn.utils.shuffle

    ptr = 0
    for iter in range(int(n_examples/batch_size)): #batch_size = 10
        epoch_data = data[ptr : ptr + batch_size] # to get the batch
        epoch_x = epoch_data[:, 0] # fist col - images
        epoch_y = epoch_data[:, 1] # second col - labels
        ptr += batch_size

        _, err = sess.run([train, loss], feed_dict={image: epoch_x, label: epoch_y}) # error occurs here.
        epoch_loss += err
    print("Epoch: ", epoch, " - Completed out of: ", epochs, " - Loss: ", epoch_loss)

epoch_x是一批彩色图像:

[array([[[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.68235294, 0.6745098 , 0.67058824],
        [0.68627451, 0.68235294, 0.66666667],
        [0.69019608, 0.68627451, 0.67058824]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.68235294, 0.6745098 , 0.67058824],
        [0.67843137, 0.67058824, 0.66666667],
        [0.68235294, 0.67843137, 0.6627451 ]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.6745098 , 0.66666667, 0.6627451 ],
        [0.68235294, 0.6745098 , 0.67058824],
        [0.68235294, 0.67843137, 0.6627451 ]],

       ...,

       [[0.39607843, 0.30980392, 0.61568627],
        [0.4       , 0.30588235, 0.61176471],
        [0.39215686, 0.29411765, 0.60784314],
        ...,
        [0.24705882, 0.18431373, 0.49019608],
        [0.23921569, 0.17254902, 0.49019608],
        [0.23137255, 0.15294118, 0.47843137]],

       [[0.39215686, 0.29019608, 0.61568627],
        [0.38039216, 0.27843137, 0.60392157],
        [0.38431373, 0.28235294, 0.61176471],
        ...,
        [0.28235294, 0.21960784, 0.5254902 ],
        [0.27058824, 0.19607843, 0.51372549],
        [0.2745098 , 0.2       , 0.51764706]],

       [[0.39215686, 0.28235294, 0.63137255],
        [0.39607843, 0.28627451, 0.63529412],
        [0.39215686, 0.28235294, 0.63529412],
        ...,
        [0.29411765, 0.22745098, 0.54509804],
        [0.3254902 , 0.25098039, 0.56078431],
        [0.30588235, 0.23137255, 0.54117647]]])
 array([[[0.85098039, 0.89803922, 0.91372549],
        [0.85490196, 0.90196078, 0.91764706],
        [0.85490196, 0.90196078, 0.91764706],
        ...,
        [0.09411765, 0.10588235, 0.09803922],
        [0.10196078, 0.11372549, 0.10588235],
        [0.11372549, 0.1254902 , 0.11764706]],

       [[0.85882353, 0.90588235, 0.92156863],
        [0.85882353, 0.90588235, 0.92156863],
        [0.8627451 , 0.90980392, 0.9254902 ],
        ...,
        [0.10980392, 0.11764706, 0.11764706],
        [0.11764706, 0.1254902 , 0.1254902 ],
        [0.11372549, 0.12156863, 0.12156863]],

       [[0.85882353, 0.90588235, 0.92156863],
        [0.85882353, 0.90588235, 0.92156863],
        [0.8627451 , 0.90980392, 0.9254902 ],
        ...,
        [0.14901961, 0.14509804, 0.15294118],
        [0.15294118, 0.14901961, 0.16470588],
        [0.14117647, 0.1372549 , 0.15294118]],

       ...,

       [[0.55294118, 0.52941176, 0.47843137],
        [0.56470588, 0.54117647, 0.49019608],
        [0.58823529, 0.55686275, 0.50588235],
        ...,
        [0.12941176, 0.09803922, 0.10196078],
        [0.12941176, 0.09803922, 0.10196078],
        [0.1254902 , 0.09411765, 0.09803922]],

       [[0.60392157, 0.57647059, 0.51764706],
        [0.58823529, 0.56078431, 0.50196078],
        [0.60784314, 0.58039216, 0.52156863],
        ...,
        [0.13333333, 0.10196078, 0.10588235],
        [0.13333333, 0.10196078, 0.10588235],
        [0.13333333, 0.10196078, 0.10588235]],

       [[0.60784314, 0.58039216, 0.52156863],
        [0.60392157, 0.57647059, 0.50980392],
        [0.60392157, 0.57647059, 0.50980392],
        ...,
        [0.12941176, 0.09803922, 0.10196078],
        [0.12941176, 0.09803922, 0.10196078],
        [0.1254902 , 0.09411765, 0.09803922]]])
 array([[[0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        ...,
        [0.6627451 , 0.67058824, 0.6745098 ],
        [0.68627451, 0.69411765, 0.69803922],
        [0.68627451, 0.69411765, 0.69803922]],

       [[0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        ...,
        [0.67058824, 0.67843137, 0.67843137],
        [0.67058824, 0.67843137, 0.67843137],
        [0.69019608, 0.69803922, 0.70196078]],

       [[0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        [0.68627451, 0.69411765, 0.67058824],
        ...,
        [0.69019608, 0.70196078, 0.69411765],
        [0.69019608, 0.69803922, 0.69803922],
        [0.69411765, 0.70196078, 0.70196078]],

       ...,

       [[0.18431373, 0.1254902 , 0.41568627],
        [0.19607843, 0.1372549 , 0.42745098],
        [0.18431373, 0.1254902 , 0.41568627],
        ...,
        [0.03529412, 0.04313725, 0.17647059],
        [0.05098039, 0.05490196, 0.20392157],
        [0.08235294, 0.08235294, 0.23921569]],

       [[0.20392157, 0.14509804, 0.43529412],
        [0.23137255, 0.17647059, 0.45882353],
        [0.21960784, 0.15294118, 0.43921569],
        ...,
        [0.04705882, 0.05098039, 0.18431373],
        [0.08235294, 0.07843137, 0.23529412],
        [0.12156863, 0.11372549, 0.27843137]],

       [[0.23137255, 0.17647059, 0.45882353],
        [0.23921569, 0.18431373, 0.46666667],
        [0.19607843, 0.12941176, 0.41568627],
        ...,
        [0.0745098 , 0.0745098 , 0.21568627],
        [0.1254902 , 0.12156863, 0.27843137],
        [0.14509804, 0.13333333, 0.30980392]]])
 array([[[0.8627451 , 0.80392157, 0.41176471],
        [0.8627451 , 0.80392157, 0.41176471],
        [0.8627451 , 0.80392157, 0.41176471],
        ...,
        [0.77647059, 0.70980392, 0.32941176],
        [0.80784314, 0.72156863, 0.32156863],
        [0.82352941, 0.7372549 , 0.3254902 ]],

       [[0.85490196, 0.79607843, 0.40392157],
        [0.85490196, 0.79607843, 0.40392157],
        [0.85490196, 0.79607843, 0.40392157],
        ...,
        [0.77647059, 0.70588235, 0.33333333],
        [0.79215686, 0.71372549, 0.31764706],
        [0.82352941, 0.7372549 , 0.3254902 ]],

       [[0.85490196, 0.79607843, 0.40392157],
        [0.85490196, 0.79607843, 0.40392157],
        [0.85490196, 0.79607843, 0.40392157],
        ...,
        [0.76862745, 0.70196078, 0.3372549 ],
        [0.78431373, 0.70196078, 0.31372549],
        [0.82745098, 0.7372549 , 0.3372549 ]],

       ...,

       [[0.68627451, 0.61960784, 0.05098039],
        [0.68235294, 0.61568627, 0.04705882],
        [0.68235294, 0.61568627, 0.04705882],
        ...,
        [0.30588235, 0.31372549, 0.35294118],
        [0.40392157, 0.43529412, 0.48627451],
        [0.49411765, 0.5254902 , 0.59215686]],

       [[0.68627451, 0.61960784, 0.05098039],
        [0.68627451, 0.61960784, 0.05098039],
        [0.68235294, 0.61568627, 0.04705882],
        ...,
        [0.33333333, 0.34901961, 0.39215686],
        [0.42352941, 0.45490196, 0.52156863],
        [0.51764706, 0.56470588, 0.63529412]],

       [[0.68627451, 0.61960784, 0.05098039],
        [0.68627451, 0.61960784, 0.05098039],
        [0.68235294, 0.61568627, 0.04705882],
        ...,
        [0.39607843, 0.41960784, 0.4627451 ],
        [0.45490196, 0.49411765, 0.56078431],
        [0.54509804, 0.58823529, 0.66666667]]])
 array([[[0.71372549, 0.71764706, 0.70980392],
        [0.71372549, 0.71764706, 0.70980392],
        [0.71372549, 0.71764706, 0.70980392],
        ...,
        [0.36078431, 0.35686275, 0.34117647],
        [0.41960784, 0.41960784, 0.39607843],
        [0.46666667, 0.46666667, 0.44313725]],

       [[0.70980392, 0.71372549, 0.70588235],
        [0.71372549, 0.71764706, 0.70980392],
        [0.70980392, 0.71372549, 0.70588235],
        ...,
        [0.42745098, 0.42745098, 0.40392157],
        [0.48235294, 0.48235294, 0.45882353],
        [0.51764706, 0.51764706, 0.49411765]],

       [[0.70980392, 0.71372549, 0.70588235],
        [0.70980392, 0.71372549, 0.70588235],
        [0.70980392, 0.71372549, 0.70588235],
        ...,
        [0.45882353, 0.46666667, 0.44313725],
        [0.51764706, 0.5254902 , 0.50196078],
        [0.53333333, 0.54117647, 0.51764706]],

       ...,

       [[0.44313725, 0.34509804, 0.69803922],
        [0.44313725, 0.34509804, 0.69803922],
        [0.43921569, 0.34117647, 0.69411765],
        ...,
        [0.48235294, 0.36078431, 0.74901961],
        [0.48235294, 0.36078431, 0.74901961],
        [0.49019608, 0.36862745, 0.75686275]],

       [[0.44313725, 0.34117647, 0.69411765],
        [0.43921569, 0.3372549 , 0.69019608],
        [0.43529412, 0.33333333, 0.68627451],
        ...,
        [0.48235294, 0.36078431, 0.74901961],
        [0.47843137, 0.35686275, 0.74509804],
        [0.47843137, 0.35686275, 0.74509804]],

       [[0.43137255, 0.32941176, 0.68235294],
        [0.42745098, 0.3254902 , 0.67843137],
        [0.42745098, 0.31372549, 0.67843137],
        ...,
        [0.48627451, 0.36470588, 0.75294118],
        [0.48627451, 0.36470588, 0.75294118],
        [0.4745098 , 0.34901961, 0.74509804]]])
 array([[[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69803922, 0.69019608, 0.68627451],
        [0.70588235, 0.69803922, 0.69411765],
        [0.70980392, 0.70196078, 0.69803922]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69803922, 0.69019608, 0.68627451],
        [0.70196078, 0.69411765, 0.69019608],
        [0.70588235, 0.69803922, 0.69411765]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69803922, 0.69019608, 0.68627451],
        [0.69803922, 0.69019608, 0.68627451],
        [0.69803922, 0.69019608, 0.68627451]],

       ...,

       [[0.06666667, 0.04313725, 0.22745098],
        [0.09019608, 0.06666667, 0.25098039],
        [0.0745098 , 0.05098039, 0.24313725],
        ...,
        [0.39607843, 0.28627451, 0.63529412],
        [0.39215686, 0.29019608, 0.61960784],
        [0.38823529, 0.29411765, 0.6       ]],

       [[0.04705882, 0.03529412, 0.21176471],
        [0.0627451 , 0.04313725, 0.21960784],
        [0.07058824, 0.05098039, 0.22745098],
        ...,
        [0.39215686, 0.28235294, 0.63529412],
        [0.38431373, 0.27843137, 0.61568627],
        [0.38431373, 0.28235294, 0.61176471]],

       [[0.03529412, 0.02745098, 0.19215686],
        [0.03921569, 0.03137255, 0.19607843],
        [0.06666667, 0.04705882, 0.22352941],
        ...,
        [0.36078431, 0.25098039, 0.60392157],
        [0.36470588, 0.25490196, 0.60784314],
        [0.39215686, 0.28235294, 0.63529412]]])
 array([[[0.75294118, 0.75294118, 0.75294118],
        [0.74509804, 0.74509804, 0.74509804],
        [0.74509804, 0.74509804, 0.74509804],
        ...,
        [0.80784314, 0.82745098, 0.82352941],
        [0.80392157, 0.82352941, 0.81960784],
        [0.81960784, 0.83921569, 0.83529412]],

       [[0.74901961, 0.74901961, 0.74901961],
        [0.74509804, 0.74509804, 0.74509804],
        [0.74901961, 0.74901961, 0.74901961],
        ...,
        [0.82745098, 0.84705882, 0.84313725],
        [0.82352941, 0.84313725, 0.83921569],
        [0.81960784, 0.83921569, 0.83529412]],

       [[0.76078431, 0.76862745, 0.76862745],
        [0.76862745, 0.76862745, 0.76862745],
        [0.76862745, 0.76862745, 0.76862745],
        ...,
        [0.82352941, 0.84313725, 0.83921569],
        [0.81960784, 0.83921569, 0.83529412],
        [0.82352941, 0.84313725, 0.83921569]],

       ...,

       [[0.47058824, 0.45098039, 0.29803922],
        [0.47058824, 0.45098039, 0.29803922],
        [0.48235294, 0.45882353, 0.29803922],
        ...,
        [0.48235294, 0.47058824, 0.41176471],
        [0.44705882, 0.43529412, 0.37647059],
        [0.4627451 , 0.45098039, 0.39215686]],

       [[0.44705882, 0.43137255, 0.27058824],
        [0.45098039, 0.43529412, 0.2745098 ],
        [0.4627451 , 0.43921569, 0.27843137],
        ...,
        [0.4627451 , 0.45490196, 0.38431373],
        [0.45098039, 0.44313725, 0.37254902],
        [0.46666667, 0.45882353, 0.38823529]],

       [[0.45882353, 0.44313725, 0.28235294],
        [0.42745098, 0.41176471, 0.25098039],
        [0.42352941, 0.4       , 0.23921569],
        ...,
        [0.45098039, 0.44313725, 0.37254902],
        [0.46666667, 0.45882353, 0.38823529],
        [0.45098039, 0.44313725, 0.37254902]]])
 array([[[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69411765, 0.69803922, 0.68235294],
        [0.70196078, 0.70588235, 0.69019608],
        [0.69019608, 0.69411765, 0.67843137]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.67843137, 0.68235294, 0.66666667],
        [0.69411765, 0.69803922, 0.68235294],
        [0.69803922, 0.70196078, 0.68627451]],

       [[0.72156863, 0.7254902 , 0.70980392],
        [0.72156863, 0.7254902 , 0.70980392],
        [0.72156863, 0.7254902 , 0.70980392],
        ...,
        [0.66666667, 0.67058824, 0.6627451 ],
        [0.68235294, 0.68627451, 0.67058824],
        [0.69803922, 0.70196078, 0.68627451]],

       ...,

       [[0.20392157, 0.14117647, 0.39215686],
        [0.19607843, 0.12941176, 0.39215686],
        [0.09803922, 0.02745098, 0.29803922],
        ...,
        [0.27843137, 0.21960784, 0.50980392],
        [0.29411765, 0.21960784, 0.5372549 ],
        [0.29411765, 0.21568627, 0.54117647]],

       [[0.31764706, 0.23921569, 0.53333333],
        [0.30588235, 0.22745098, 0.52941176],
        [0.31764706, 0.23529412, 0.54509804],
        ...,
        [0.28627451, 0.22352941, 0.52941176],
        [0.30980392, 0.23137255, 0.55686275],
        [0.30980392, 0.22745098, 0.56078431]],

       [[0.37647059, 0.28235294, 0.62745098],
        [0.37254902, 0.27843137, 0.62352941],
        [0.35686275, 0.25882353, 0.61176471],
        ...,
        [0.30588235, 0.23921569, 0.55686275],
        [0.32156863, 0.23921569, 0.57254902],
        [0.34117647, 0.25490196, 0.6       ]]])
 array([[[0.80784314, 0.84313725, 0.85490196],
        [0.78431373, 0.83137255, 0.83921569],
        [0.79215686, 0.83529412, 0.85098039],
        ...,
        [0.57647059, 0.59607843, 0.59215686],
        [0.60784314, 0.63137255, 0.62745098],
        [0.65882353, 0.68235294, 0.67843137]],

       [[0.80784314, 0.84313725, 0.85490196],
        [0.8       , 0.83529412, 0.84705882],
        [0.8       , 0.84313725, 0.85882353],
        ...,
        [0.52156863, 0.54117647, 0.5372549 ],
        [0.61176471, 0.63137255, 0.62745098],
        [0.62352941, 0.64705882, 0.64313725]],

       [[0.80784314, 0.84313725, 0.85490196],
        [0.80784314, 0.84313725, 0.85882353],
        [0.80392157, 0.83921569, 0.85490196],
        ...,
        [0.5254902 , 0.53333333, 0.53333333],
        [0.54509804, 0.56470588, 0.56078431],
        [0.63529412, 0.65490196, 0.65098039]],

       ...,

       [[0.20784314, 0.15294118, 0.16862745],
        [0.22352941, 0.16862745, 0.18431373],
        [0.21176471, 0.16078431, 0.16862745],
        ...,
        [0.76470588, 0.76470588, 0.76470588],
        [0.76862745, 0.76862745, 0.76862745],
        [0.76078431, 0.76862745, 0.76862745]],

       [[0.21568627, 0.16078431, 0.17647059],
        [0.22352941, 0.16862745, 0.18431373],
        [0.21568627, 0.16470588, 0.17254902],
        ...,
        [0.75686275, 0.76470588, 0.76470588],
        [0.76078431, 0.76862745, 0.76862745],
        [0.76470588, 0.77254902, 0.77254902]],

       [[0.20392157, 0.14901961, 0.16470588],
        [0.21176471, 0.15686275, 0.17254902],
        [0.22745098, 0.17647059, 0.18431373],
        ...,
        [0.76078431, 0.76862745, 0.76862745],
        [0.76470588, 0.77254902, 0.77254902],
        [0.76862745, 0.77647059, 0.77647059]]])
 array([[[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69019608, 0.68235294, 0.67843137],
        [0.69803922, 0.69019608, 0.68627451],
        [0.70196078, 0.69411765, 0.69019608]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69019608, 0.69411765, 0.68627451],
        [0.69019608, 0.69411765, 0.68627451],
        [0.70196078, 0.69411765, 0.69019608]],

       [[0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        [0.7254902 , 0.72941176, 0.71372549],
        ...,
        [0.69411765, 0.69803922, 0.69019608],
        [0.69411765, 0.69803922, 0.69019608],
        [0.69411765, 0.69803922, 0.69019608]],

       ...,

       [[0.40392157, 0.29411765, 0.64705882],
        [0.40392157, 0.30588235, 0.65098039],
        [0.40784314, 0.30980392, 0.64705882],
        ...,
        [0.06666667, 0.03529412, 0.27058824],
        [0.09411765, 0.0627451 , 0.30196078],
        [0.21568627, 0.18039216, 0.42745098]],

       [[0.42745098, 0.32156863, 0.65882353],
        [0.39607843, 0.29019608, 0.62745098],
        [0.42745098, 0.3254902 , 0.65490196],
        ...,
        [0.10980392, 0.0745098 , 0.32156863],
        [0.20392157, 0.16078431, 0.43529412],
        [0.3254902 , 0.27843137, 0.56078431]],

       [[0.39215686, 0.27843137, 0.61176471],
        [0.40392157, 0.30196078, 0.63137255],
        [0.39215686, 0.29019608, 0.61568627],
        ...,
        [0.18823529, 0.14901961, 0.40784314],
        [0.29803922, 0.25098039, 0.53333333],
        [0.31764706, 0.2627451 , 0.56862745]]])]

和epoch_y是具有3类的标签批处理:

[list([0, 1, 0]) list([0, 0, 1]) list([0, 1, 0]) list([1, 0, 0])
 list([0, 1, 0]) list([0, 1, 0]) list([0, 0, 1]) list([0, 1, 0])
 list([0, 0, 1]) list([0, 1, 0])]

喂入这批图像和标签时,我得到ValueError: setting an array element with a sequence.

1 个答案:

答案 0 :(得分:0)

好的,我解决了。发生这种情况的原因是因为我将数据预处理到另一个文件中,然后将其保存为pickle格式。我的标签应该是[],而不是list()。

我已经将数据从pickle格式加载到模型文件中,然后使用data = np.asarray(data)将其转换为numpy数组。我不知道是什么导致了问题,但是删除此问题可以解决问题。我的epoch_y现在出现在[]中,而不是list()