Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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 Tensorflow我不明白model.fit的输入应该是什么_Python_Tensorflow_Keras - Fatal编程技术网

Python Tensorflow我不明白model.fit的输入应该是什么

Python Tensorflow我不明白model.fit的输入应该是什么,python,tensorflow,keras,Python,Tensorflow,Keras,我是一个初学者,我正在尝试将一些图像分为20类 这是我尝试使用的代码: from tensorflow.python.keras import Sequential from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D x=x/255.0 model=Sequential() model.add(Conv2D(64,(3,3), input_shap

我是一个初学者,我正在尝试将一些图像分为20类

这是我尝试使用的代码:

from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
x=x/255.0
model=Sequential()
model.add(Conv2D(64,(3,3), input_shape=x.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64,(3,3), input_shape=x.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))

model.add(Dense(20))
model.add(Activation('softmax'))




loss = tf.keras.losses.CategoricalCrossentropy()

lr = 1e-3
optimizer = tf.keras.optimizers.Adam(learning_rate=lr)

metrics = ['accuracy']

model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
Y= np.asarray(y)

model.fit(x,Y,batch_size=32,validation_split=0.1)
但我收到了这个错误:

ValueError: You are passing a target array of shape (1554, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:
x、 形状返回 1554,50,50,1

及 Y形返回

谢谢你的帮助!
1554,

您可能忘记了对标签数据进行热编码。如果标签是表示不同类的0到19之间的整数,则可以在keras.utils中使用to_category来相应地转换标签