标签: valueerror

ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1))

我尝试制作一个卷积神经网络来对狗和猫进行分类。我在标题中提到了错误。

根据我的搜索,有人说错误属于不同版本的tensorflow和keras库,有人说是语法错误。我会在这里留下我的代码,告诉我我哪里出错了。

#IMPORTING LIBRARIES
import tensorflow as tf
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator



#IMAGE DATA PREPROCESSING

#preprocessing the training set
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\training_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')

#preprocessing the test set
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\test_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')


 
#BULDING THE CNN
#
#
#initialising the cnn
cnn = tf.keras.models.Sequential()


#convolution
cnn.add(tf.keras.Input(shape=(64, 64, 3)))
cnn.add(tf.keras.layers.Conv2D(filters = 32 , …
Run Code Online (Sandbox Code Playgroud)

python label conv-neural-network valueerror logits

1
推荐指数
1
解决办法
1279
查看次数

Keras自定义图像预处理函数引发Value Error“输出数组为只读”

我想在 Keras 中使用一些自定义的图像预处理函数和 ImageDataGenerator 函数。例如,我的自定义函数如下所示:

def customizedDataAugmentation(x):
   choice = np.random.choice(np.arange(1, 4), p=[0.3, 0.3, 0.4])
   if choice==1:
       x = exposure.adjust_gamma(x, np.random.uniform(0.5,1.5))
   elif choice==2:
       ix = Image.fromarray(np.uint8(x))
       blurI = ix.filter(ImageFilter.GaussianBlur(np.random.uniform(0.1,2.5)))
       x = np.asanyarray(blurI)
   return x
Run Code Online (Sandbox Code Playgroud)

使用方法如下:

        self.train_datagen = image.ImageDataGenerator(
            rescale=1./255,
            zoom_range=0.15,
            height_shift_range=0.1,
            horizontal_flip=True,
            preprocessing_function=customizedDataAugmentation
        )
Run Code Online (Sandbox Code Playgroud)

但是,当我开始训练时,它跳出这个错误:

Traceback (most recent call last):
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/site-packages/keras/utils/data_utils.py", line 560, in data_generator_task
    generator_output = next(self._generator)
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/site-packages/keras/preprocessing/image.py", line 1039, in next
    x …
Run Code Online (Sandbox Code Playgroud)

python keras valueerror

0
推荐指数
1
解决办法
3398
查看次数

值错误:解压缩的值太多(预期3)

我试图实现值迭代算法.我有一个网格

grid = [[0, 0, 0, +1],
    [0, "W", 0, -1],
    [0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)

动作列表

actlist = {UP:1, DOWN:2, LEFT:3, RIGHT:4}
Run Code Online (Sandbox Code Playgroud)

还有奖励功能

reward = [[0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0,  0]] 
Run Code Online (Sandbox Code Playgroud)

我写了一个函数T,它返回3个元组的元组.

def T(i,j,actions):
if(i == 0 and j == 0):
    if(actions == UP):
        return (i,i,0.8),(i,i,0.1),(i,j+1,0.1)
    elif(actions == DOWN):
        return (i+1,j,0.8),(i,j,0.1),(i,j+1,0.1)
    elif(actions == LEFT):
        return (i,j,0.8),(i,j,0.1),(i+1,j,0.1)
    elif(actions == RIGHT):
        return (i,j+1,0.8),(i,i,0.1),(i+1,j,0.1)
elif (i == 0 and j == 1):
    if(actions == UP):
        return (i,i,0.8),(i,j-1,0.1),(i,j+1,0.1) …
Run Code Online (Sandbox Code Playgroud)

python artificial-intelligence tuples python-3.x valueerror

0
推荐指数
1
解决办法
488
查看次数

字符串到 int ValueError Python

如果字符串是 '007w',那么当它尝试将 '007w' 作为整数返回时,我希望它return Noneprint('Cannot be converted). 但不使用 Try 除外 ValueError:

import random

def random_converter(x):
    selection = random.randint(1,5)
    if selection == 1:
        return int(x)
    elif selection == 2:
        return float(x)
    elif selection == 3:
        return bool(x)
    elif selection == 4:
        return str(x)
    else:
        return complex(x)


for _ in range(50):
    output = random_converter('007w')
    print(output, type(output))
Run Code Online (Sandbox Code Playgroud)

python types valueerror

0
推荐指数
1
解决办法
144
查看次数

将文本文件转换为字典python

我有一个类似这样的文本文件:

banana
delicious
yellow

watermelon
big
red

orange
juicy
vitamin c
Run Code Online (Sandbox Code Playgroud)

我正在尝试将此文本文件转换为字典(水果名称作为键,它的几行描述作为各种值)。以下是我当前的代码。

f = open("filepath", 'w')
myplant = {}
for line in f:
    k, v = line.strip().split('\n\n')
    myplant[k.strip()] = v.strip()
f.close()
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

ValueError: not enough values to unpack (expected 2, got 1)
Run Code Online (Sandbox Code Playgroud)

谁能帮我调试我的问题。谢谢!

python dictionary text-files python-3.x valueerror

0
推荐指数
1
解决办法
110
查看次数

如何根据列表绘制列表列表?

x = [2000,2001,2002,2003]
y = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
for i in range(len(y[0])):
    plt.plot(x,[pt[i] for pt in y])
plt.show()
Run Code Online (Sandbox Code Playgroud)

我得到一个ValueError4, 3。我知道这一点,x而且y必须是平等的。我以为len(y[0])会工作。

对于 中的每个子列表y,我想生成一行,其x值对应于2000, 2001, 2002, 2003

python matplotlib valueerror

0
推荐指数
1
解决办法
113
查看次数