Python Forum
How to change input data set for character recognition program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change input data set for character recognition program?
#1
I am new to Python and machine learning. Trying to learn by working on tested examples. Tried the character recognition program (from Chollet) using MNIST data set, later tried to change the input data set (numbers instead of character images).

I cannot run it with this error:
Error:
train_images=((1,1,0,0,1) TypeError: 'tuple' object is not callable
How can I correct this? Maybe it is more complicated than this in changing the input data set. Thanks.

# from keras.datasets import mnist
# (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images=((1,1,0,0,1)
              (0,0,1,1,1)
              (1,1,0,1,1))
train_labels=[[10,10,0,0,10]
              [0,0,10,10,10]
              [10,10,0,10,10]]
test_images=[[0,1,0,1,1]
              [0,0,1,0,1]
              [1,1,1,1,1]]
test_labels=[[0,10,0,10,10]
              [0,0,10,0,10]
              [10,10,10,10,10]]

from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

#from keras.utils import to_categorical
from tensorflow.keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
network.fit(train_images, train_labels, epochs=5, batch_size=128)
Reply
#2
I have made some changes to try out the program with my data. See below. Ignore the one above.

# from keras.datasets import mnist
# (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Instead of using MNdist data, I use my own numeric data as below.

train_images = [[0, 1],
                [1, 0],
                [0, 1]]
train_labels = [[0, 10],
                [10, 0],
                [0, 10]]
test_images = [[0, 1],
               [0, 1],
               [0, 1]]
test_labels = [[0, 10],
               [0, 10],
               [0, 10]]

from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(2 * 2,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])

train_images = train_images.reshape((3, 2 * 2))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((3, 2 * 2))
test_images = test_images.astype('float32') / 255

# from keras.utils import to_categorical
from tensorflow.keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
network.fit(train_images, train_labels, epochs=5, batch_size=128)
Error:
2022-04-12 19:16:17.774817: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-04-12 19:16:18.457248: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1401 MB memory: -> device: 0, name: NVIDIA GeForce GT 730, pci bus id: 0000:01:00.0, compute capability: 3.5 Traceback (most recent call last): File "C:\Users\xxxxx", line 29, in <module> train_images = train_images.reshape((3, 2 * 2)) AttributeError: 'list' object has no attribute 'reshape' Process finished with exit code 1
Reply
#3
Add commas.
When you define your train_images (and the others) it should be like
train_images=((1,1,0,0,1),
              (0,0,1,1,1),
              (1,1,0,1,1))
Reply
#4
(Apr-17-2022, 12:21 PM)jefsummers Wrote: Add commas.
When you define your train_images (and the others) it should be like
train_images=((1,1,0,0,1),
              (0,0,1,1,1),
              (1,1,0,1,1))
Thanks.
I have tried that, with comma, ( ), [ ], see my second program.

No success. About to give up learning from this example. Ha.
Reply
#5
reshape is a methode under numpy's library and as the error printed in your terminal the object list has no method defined as reshape

import numpy as np
w_train=np.array(x_train)

then you can simply use the reshape function without any error :

w_train=x_train.reshape((2404,28,224,224,3))

you can check the reshape characteristics in this link https://www.geeksforgeeks.org/reshape-numpy-array/ for more understanding of the method
Reply
#6
I made a variety of modifications. This runs
# from keras.datasets import mnist
# (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Instead of using MNdist data, I use my own numeric data as below.
import pandas as pd
import numpy as np
import tensorflow as tf

train_images = [[0, 1],
                [1, 0],
                [0, 1]]
train_labels = [9,0,9]
test_images = [[0, 1],
               [0, 1],
               [0, 1]]
test_labels = [[0, 10],
               [0, 10],
               [0, 10]]

train_images = np.array(train_images) 
train_labels = np.array(train_labels)
test_images = np.array(test_images)
test_labels = np.array(test_labels)

from keras import models
from keras import layers
 
network = models.Sequential()
network.add(layers.Dense(512, activation='relu'))
network.add(layers.Dense(10, activation='softmax'))
 
network.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
 
#train_images = train_images.reshape((3, 2))
#train_images = train_images.astype('float32') / 255
#test_images = test_images.reshape((3, 2))
#test_images = test_images.astype('float32') / 255
 
# from keras.utils import to_categorical
from tensorflow.keras.utils import to_categorical
 
#train_labels = to_categorical(train_labels)
#test_labels = to_categorical(test_labels)
network.fit(train_images, train_labels, epochs=5)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how do change data in a database CompleteNewb 5 2,164 Mar-20-2022, 11:31 PM
Last Post: Larz60+
  MergeSort data input Silvioo 0 1,707 Oct-11-2018, 06:53 AM
Last Post: Silvioo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020