Linked Questions

Popular Questions

I'm a first grader in computer science, and I'm trying to make a 56x56 image from 4 mnist images and make a model that can distinguish numbers in the 56x56 image.

I wrote the 56x56 images part, but I don't know how to pre-process the targets (answers).

The error I get is:

ValueError: Data cardinality is ambiguous 

My code is:

import tensorflow as tf
import matplotlib.pyplot as plt

mnist = tf.keras.datasets.mnist.load_data()
(x_train, x_target), (y_train, y_target) = mnist

x_train = x_train / 255.0
y_train = y_train / 255.0

x_train_1 = x_train[:30000]
x_train_2 = x_train[30000:]
y_train_1 = y_train[:5000]
y_train_2 = y_train[5000:]

x_1 = []
x_2 = []
y_1 = []
y_2 = []

for i in range(0, len(x_train_1), 2):
    x_1.append(tf.concat([x_train_1[i], x_train_1[i + 1]], axis=1))
    x_2.append(tf.concat([x_train_2[i], x_train_2[i + 1]], axis=1))

xtrain2d = tf.concat([tf.concat([x_1[i], x_2[i]], axis=0) for i in range(len(x_1))], axis=0)

xtrain2d = tf.reshape(xtrain2d, (15000, 56, 56))

for i in range(0, len(y_train_1), 2):
    y_1.append(tf.concat([y_train_1[i], y_train_1[i + 1]], axis=1))
    y_2.append(tf.concat([y_train_2[i], y_train_2[i + 1]], axis=1))

ytrain2d = tf.concat([tf.concat([y_1[i], y_2[i]], axis=0) for i in range(len(y_1))], axis=0)

ytrain2d = tf.reshape(ytrain2d, (2500, 56, 56))

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(56, 56)))
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer = 'adam',
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy'])

model.fit(xtrain2d, x_target, epochs = 5)

test_loss, test_accu = model.evaluate(ytrain2d, y_target)

Related Questions