ValueError: 数据基数不明确:x 大小:150000 y 大小:50000 请确保所有数组包含相同数量的样本

你好,我在使用这段代码时遇到了错误

ValueError: Data cardinality is ambiguous: x sizes: 150000y sizes: 50000Make sure all arrays contain the same number of samples.

我尝试更改了reshape选项,甚至使用了numpy.transpose,但都没有用,有人能帮我吗?

import numpy as npimport tensorflow as tffrom tensorflow.keras import datasets, layers, modelsfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D(x_train, y_train) , (x_test, y_test) = datasets.cifar10.load_data()#x_train.shape #(50000, 32, 32, 3) #x_test.shape  #(10000, 32, 32, 3)x_train = x_train.reshape(-1, 32, 32, 1)x_test = x_test.reshape(-1, 32, 32 ,1)x_train = x_train.astype('float32')         # change integers to 32-bit floating point numbers x_test = x_test.astype('float32')x_train /= 255.0              x_test /= 255.0model = tf.keras.models.Sequential() model.add(tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2))) model.add(tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2))) model.add(tf.keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))model.add(tf.keras.layers.Conv2D(256, (3, 3), padding='same', activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))model.add(tf.keras.layers.Conv2D(512, (3, 3), padding='same', activation='relu')) model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(512, activation=tf.nn.relu)) model.add(tf.keras.layers.Dense(512, activation=tf.nn.relu))model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax)) model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.build(input_shape=(512,32,32,1)) model.summary() model.fit(x_train, y_train, batch_size=1000, epochs=1) score = model.evaluate(x_test, y_test) print('Test loss:', score[0]) print('Test accuracy:', score[1])predictions = model.predict([x_test])#print(predictions)print(np.argmax(predictions[0]))img_path = x_test[0]print(img_path.shape)if(len(img_path.shape) == 3):plt.imshow(np.squeeze(img_path))elif(len(img_path.shape) == 2):plt.imshow(img_path)else:print("Higher dimensional data")

回答:

你需要做一些更改。我会为你写一个例子

更改内容:

  1. 你不需要重塑x_train, x_test,因为它们已经是正确的形状。
  2. 使用tf.keras.layers.InputLayer总是一个好主意,而不是后来构建模型。
  3. 我没有做这个更改,但只要有可能,你应该使用tf.keras.Sequential来构建模型。(更易读,更不容易出错)。Functional api(或你当前的方法)适用于需要构建复杂架构的情况。
  4. 你现在可以增加模型(更多层)。我只用了几个层来给你展示一个例子。
  5. InputLayer中的input_shape被认为是(batch_size, img_width, img_height, img_channels),由于batch_size可能是非统一的,因此默认被视为None,所以我们不提供它,因此我们只传递(img_width, img_height, img_channels),因为你的输入具有32宽32高和3个通道,我们传递(32, 32, 3)

如果这解决了你的问题,请投票或给一个绿色勾选。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注