The default image data set for handwritten numbers is 1 to 10.
I would like to take out only 1 and 2 of them so that I can only categorize 1 and 2.
We followed the steps below.
·Take out image data and labels for 1 and 2
·Replace the extracted image data with a list, combine the image data and the image label into one, and then return them to the numpy array
·Create an appropriate learning model and set the output to (2) to execute the learning
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras.layers import Conv2D, MaxPooling2D, Flatten
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
from keras.datasets import mnist
(x_train, t_train), (x_test, t_test) = mnist.load_data()
# Take out only handwritten images of 1 and 2
x_train1, x_train2 = x_train [np.where(t_train==1)], x_train [np.where(t_train==2)]
t_train1, t_train2 = t_train [np.where(t_train==1)], t_train [np.where(t_train==2)]
Convert an array of #1 and 2 handwritten numbers into a list
x_trainone=[]
x_traintwo=[ ]
for n1 in range (x_train1.shape[0]):
x_trainone.append(x_train1[n1])
for n2 in range (x_train2.shape[0]):
x_trainwo.append(x_train2[n2])
t_trainone=[ ]
t_trainwo=[ ]
for n3 in range(t_train1.shape[0]):
t_trainone.append(t_train1[n3])
for n4 in range(t_train2.shape[0]):
t_trainwo.append(t_train2[n4])
add a list of #1 and a list of 2
x_trainall2 = x_trainone + x_trainwo
t_trainall2 = t_trainone + t_trainwo
Return the list of learning data mixed with #1 and 2 to the array
x_trainall, t_trainall = np.array(x_trainall2), np.array(t_trainall2)
#Do the same for test data
x_test1, x_test2 = x_test [np.where(t_test==1)], x_test [np.where(t_test==2)]
t_test1, t_test2 = t_test [np.where(t_test==1)], t_test [np.where(t_test==2)]
Convert an array of #1 and 2 handwritten numbers into a list
x_testone=[ ]
x_testtwo=[ ]
for n1 in range (x_test1.shape[0]):
x_testone.append(x_test1[n1])
for n2 in range (x_test2.shape[0]):
x_testtwo.append(x_test2[n2])
t_testone=[ ]
t_testtwo=[ ]
for n3 in range (t_test1.shape[0]):
t_testone.append(t_test1[n3])
for n4 in range (t_test2.shape[0]):
t_testtwo.append(t_test2[n4])
add a list of #1 and a list of 2
x_testall2 = x_testone + x_testtwo
t_testall2 = t_testone + t_testtwo
Return the list of learning data mixed with #1 and 2 to the array
x_testall, t_testall = np.array(x_testall2), np.array(t_testall2)
model=keras.sequential()
model.add(Conv2D(28,(3,3), strides=1, activation='sigmoid', input_shape=(28,28,1))))
model.add(Activation("relu"))
model.add(Flatten())
model.add (Dense(28))
Activation ("sigmoid")
model.add (Dense(28))
Activation ("relu")
model.add(Dense(2))
Activation ("softmax")
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoryCrossentropy(from_logits=False),
metrics=["accuracy"])
model.summary()
After creating the code like this, we learned as follows.
model.fit(history=model.fit(x_trainall, t_trainall, epochs=10, batch_size=100))
Then,
Epoch 1/10
2023-03-1101:34:38:Wtensorflow/core/framework/op_kernel] OP_REQUIRES failed at sparse_xent_op: INVALID_ARGUMENT: Received a label value of 2 which is outside the valid range of [0, 2]. Label values: 1 2 2 1 1 1 2 1 2 2 2 2 2 1 2 2 1 2 2 1 2 2 1 2 1 2 2 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
Cell In [15], line 1
---->1model.fit(history=model.fit(x_trainall, t_trainall, epochs=10,
2 validation_data=(x_testall, t_testall)))
File...<locals>.error_handler(*args,**kwargs)
67 filtered_tb =_process_traceback_frames(e.__traceback__)
68# To get the full stack trace, call:
69#`tf.debugging.disable_traceback_filtering()`
--- >70 raise e.with_traceback (filtered_tb) from None
71finally:
72delfiltered_tb
File....in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
50try:
51ctx.ensure_initialized()
--- >52 sensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
53 inputs, attrs, num_outputs)
54 except core._NotOkStatusException as:
55 if name is not None:
InvalidArgumentError: Graph execution error:
I got this kind of error.When I looked it up, it said it was likely a computer capacity or processing power issue, but when I changed the last Dense of sequential from 2 to 10, I returned it so that the original 10 handwritten numbers could be classified, and there was no error.So I think it's not a computer performance problem, but something in the code is wrong, but I don't know the cause of the error.Where should I fix it?
python machine-learning tensorflow deep-learning keras
Received label value of 2 which is outside the valid range of [0, 2).
From the above error message, the label value should be "0" or "1".
© 2024 OneMinuteCode. All rights reserved.